Article
i/2007/07/sequence

The Power of Sequences for Hierarchical Behaviors

Alex J. Champandard on July 2, 2007

Sequences of behaviors may be nothing new, but they are very powerful. For decades, programmers have been assembling statements together as instructions that run one after the other. For game AI, it makes even more sense:

  • It’s such a simple idea that everyone understands it very quickly: from designers to testers, including producers!

  • Sequences of behaviors are very easy to customize, either using a configuration file or an editor.

  • Using only sequences avoids the clutter of unnecessary features in a traditional scripting language.

So you can trivially build more complex behaviors out of smaller building blocks using sequences. In fact, once you’ve implemented this, it’ll probably be the most common primitive you use for creating behaviors.

Sequence of Behaviors

A sequence of four atomic behaviors.

Implicit Dependencies

As well as being easy to edit, sequences have many benefits for the AI engine also:

  • Since a sequence respects its order, it implicitly expresses dependencies between its child behaviors.

  • Dependencies are easy to model this way, you don’t have to explain “why” — just sequence the behaviors together.

Most interesting game behaviors have dependencies, so by using a sequence you can express them easily using the same construct as for building hierarchical behaviors.

Robustness

It’s important for all your behaviors to have a termination status, that indicates if everything went well, if the behavior completed, or if there was a fatal problem. It’s also equally important to deal with these termination status codes everywhere in the AI. But in practice with C++ or Lua, this can be rather difficult to achieve if you have to implement this everywhere.

The solution is to implement the sequence once to deal with these termination status codes, and then re-use this primitive everywhere as necessary. This gives you the basic elements of a search:

  • If a child behavior succeeds, the sequence continues its execution.

  • If a child behavior fails cleanly, then the sequence code backtracks to find other candidate behaviors.

  • If a child behavior has an unexpected error, the sequence also returns that error since the search cannot continue.

Such sequences provide the core functionality of any AI engine. But admittedly, they don’t work so well alone. For that, you’ll need selectors — which are described in the next article.

Discussion 1 Comments

FuriCuri on November 11th, 2007

Do I understand it correctly: 1. If child behaviour node for current sequence selector success then selector continue execution for the next child. If it was the last child then selector return success. 2. If child node failes cleanly then selector still continue to the next child in his sequence. If it was the last child then selector return clean failure (if there were no child successfully completed before) or dirty failure otherwise. 3. If child fails dirty then selector return fail dirty too.

If you'd like to add a comment or question on this page, simply log-in to the site. You can create an account from the sign-up page if necessary... It takes less than a minute!