Observers for Individual Tasks

Alex J. Champandard on June 3, 2007

Having a way to monitor all tasks in a scheduler is particularly useful from a software architecture perspective. But behaviors most often are only interested in specific tasks rather than every one. Having an individual task observer is a great way to accomplish this.

These observers are most often used by composite tasks that have to manage multiple children. The composites ask the interpreter to run a task, and pass an observer with it to get notified when it’s done. The code looks like this:

class TaskObserver
    virtual void onTaskFinished(Status) = 0;
    virtual ~TaskObserver();

A custom composite task should derive from this class, and implement the pure virtual function. Then, when requesting child tasks to be executed, it would pass itself as the observer. The scheduler’s run function would change to have an extra optional parameter for the observer.

// class Scheduler
bool run(Task&, TaskObserver* = 0);

Note that it’s particularly useful for these observers to get the final return status of a task, as they can take action according to the outcome. Dealing with return status codes is very important for the robustness of the AI logic.

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!