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.
A sequence of four atomic behaviors.
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.
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.