Article

Using Composite Tasks to Make Hierarchies

Alex J. Champandard on June 9, 2007

Conceptually, making hierarchical AI is as simple as giving certain tasks other child tasks. This can be done using the composite pattern. Essentially, a composite task is just another task that can store pointers to its children.

Here’s how it can be done in C++:

class CompositeTask : public Task
{
public:
    void addChild(Task&);

    // Inherited Task API
    virtual Status execute() = 0;
protected:
    std::vector m_Children;
};

Now each composite will be different. The Sequence may behave differently from the Selector or the Parallel, but each of them implement their own execute() function. In that function, the composites are expected to use the scheduler to manage the child tasks. This makes the code for your composite tasks much simpler than if you had to manage children manually.

For example, a Sequence would use the interpreter’s run() function to start the first child task, then wait until it’s finished with an observer. Then in the observer itself, the second task would be run() also, if the first task was successful.

Discussion 0 Comments

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!