Observing the Scheduler

Alex J. Champandard on June 2, 2007

There are many advantages to having a scheduler for managing tasks, notably allowing the AI to reason about other behaviors, as well as improved debugging and logging. The observer pattern is a good way to provide a generic hook for such features.

Observers are simple objects that get called when a specific things happens in the implementation of an algorithm. A scheduler observer, for example, would be notified when the scheduler processes any tasks. The most useful thing to have an observer for is a task finishing:

class SchedulerObserver
    virtual void onTaskFinished(Task&) = 0;
    virtual ~SchedulerObserver();

Code for custom features derives from this class, and implements whatever functionality is necessary without affecting the core scheduling algorithm. The derived observers are then registered in the scheduler with a new addObserver() function. You can support multiple observers if necessary by implementing composite observers (one big observer that notifies its children), or by making a simple linked list within the scheduler itself.

Getting notified of the tasks finishing is the most useful, but observing tasks starting is easily supported by adding new functions to the base class, such as onTaskStarted(). This allows you to implement interesting meta-behaviors, selective logging of tasks, or providing an up-to-date display of what tasks are active.

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!