Oh, Behave (III) – Decorators

As we’ve seen before, composites let us build complex behavior trees by combining smaller ones and executing them in several different ways. Still, we’re in need of a way to add new features to existing behaviors, like inverting its result or execute it multiple times or until a given condition is met.

Modularity and reusability are key when working with behavior trees (actually, that applies to pretty much all in software engineer). Therefore, we should avoid reimplementing a behavior whenever possible. So the question is: how can we add these new features and maximize reusability at the same time?

That’s when decorators are added to the mix.

behave

Decorators

In contrast to composites, decorator behaviors have one and only one child behavior. Although technically doable, there is no gain in adding a decorator as leaf node to our tree, since their strength lies in modifying existing behaviors.

Decorator behaviors add new functionalities to their child behavior without necessarily knowing its internal execution. A decorator behavior may transform the result provided by their child behavior and/or change the way it’s executed in some other way.

For example, a common use for decorators is to repeat the execution of its child behavior until a given condition is satisfied, independently of what the child behavior actually does.

Available Decorators

At the time of this writing, the following decorators are already available in Crimild:

Repeat

A Repeat behavior will execute its child behavior multiple times. There are several variations of the Repeat behavior, implementing different looping conditions. For example, we might need to loop only a limited number of times or do so until the child behavior fails or succeeds.

If, for example, you need a character that should run from one location to another, you’ll probably be using a Repeat behavior along with a Sequence or Selector that moves the character and check if the destination has been reached in every loop. Such a behavior tree should like this:

RepeatUntilFail
  |-> Sequence
        |-> AwayFromTarget
        |-> Move

The behavior tree above will move a character (or any Node) until AwayFromTarget fails. When that happens, the Sequence behavior will fail too and RepeatUntilFail itself will terminate (you might be surprised to know that RepeatUntilFail will return a SUCCESS result even when the inner behaviors failed. That’s because the loop itself did succeeded).

Another use of a Repeat behavior is to just execute the same behavior over and over again, like and idle behavior.

Inverter

As the name suggests, an Inverter behavior will change the result of its child behavior once it has been executed. Then, if the child returns SUCCESS, the Inverter will return FAILURE, and vice versa.

Inverter behaviors are commonly used to check conditions inside a loop. The behavior tree shown earlier could be rewritten as follows:

RepeatUntilFail
  |-> Sequence
      |-> Inverter
            |-> ArrivedAtTarget
      |-> Move

Basically, move the character as long as we’re not at the target location. In this case, the Inverter behavior allows to reuse other behaviors. Otherwise, we might need to implement behaviors for all cases.

Succeeder

A Succeeder behavior always returns SUCCESS, disregarding of whether its child behavior returned SUCCESS or FAILURE. This behavior is useful when we need to branch the executing of a tree.

Please not that there is not “Failer” behavior since that can be accomplished by using an Inverter behavior on a Succeeder.

Other uses

Decorators are quite powerful and could radically change the execution of a branch in order behavior tree. Here are some other uses for Decorators:

  • Avoid executing the child node based on a predefined condition
  • Avoid executing a branch in our tree based on a timer
  • Deal with error status
  • Pause execution of a tree (like a breakpoint)

By combining Sequence and Decorator behaviors in our tree we can create really complex logics for virtually any logic we would like to achieve.

It’s show time!

Enough theory. The time has come for us to see behavior trees in action.

So, in the next episode, we’re going to create at a live demo to better understand how behavior trees work in practice, their strengths and limitations.

Stay tuned.

 

 

Advertisements

Oh, Behave (II) – Composites

Welcome to another entry in this series of articles talking about Crimild’s support for Behavior Trees. If you haven’t done so already, I recommend reading my previous article where the most important concepts were introduced.

As we discussed before, a behavior tree can be as simple as a single action, like printing a value in standard output. But what if we would like to do something more complex, like calculating the sum of two values stored in the behavior tree’s context?

In this article we’ll be describing Composite nodes and how they can be used to execute one or more actions for a given behavior.

What are composites?

A composite node is one that may contain one or more child nodes, executing them in serial, random or whatever fashion is required, returning a single result based on how the child nodes behave. Which reminds me…

behave

(Yes, this image will appear in every article. Non negotiable. Keep reading)

At the time of this writing, Crimild implements three different composites: Sequence, Parallel and Selector. We’ll be describing each of them below, comparing both the way in which they execute their children and how the resulting state is calculated.

Sequence

A Sequence node is a composite that executes its child nodes one by one in a serial way. We’ve seen a sequence in action in the previous article when we made the analogy between a behavior tree and adding two numbers in Assembly language:

Behavior Tree - Intro (1)

The nodes are executed as follow: Sequence -> Mov(x, 5) -> Mov(y, 10) -> Add(x, y) -> PrintContextValue

The resulting state (SUCCESS, FAILURE or RUNNING) depends only on the resulting states of the children. A Sequence itself will succeed only when all of its children succeed. In other words, if any children in the sequence returns a FAILURE, then the sequence itself will return FAILURE and the remaining nodes, if any, will not be executed at all.

A Sequence returning a RUNNING state indicates that there have been no FAILURE so far and that there is at least one running child node which execution has not been completed, so we need to keep updating the sequence in the next frame.

You can think about sequences as the AND operator in a programming language. In order to better understands this, let’s represent a sequence as a for loop:

void Sequence::execute( void ) {
    for ( int i = 0; i < getChildCount(); i++ ) {
        auto state = getChild( i )->execute();
        if ( state != SUCCESS ) {
            return state;
        }
    }
    return SUCCESS;
}

Parallel

A Parallel composite executes all of its child nodes at the same time.

Please note that the term parallel here does not mean that the child nodes are executed concurrently. Child nodes are executed without the use of any additional thread or parallelism at all. Crimild does not support concurrency while traversing a behavior tree at the time of this writing.

What do we mean by executing nodes at the same time? It means that a Parallel composite will execute child nodes without waiting for them to succeed. In other words, it will execute the first node and then it will move to the next one and so on, regardless of wether the execution of the previous child was successfully completed or if it’s still running.

void Parallel::execute( void ) { 
    State result = SUCCESS;
    for ( int i = 0; i < getChildCount(); i++ ) { 
        auto state = getChild( i )->execute(); 
        if ( state == FAILURE ) { 
            return state; 
        }        
        else if ( state == RUNNING ) {
            result = RUNNING;
        }
    } 
    return RESULT; 
}

(Please keep in mind the actual implementation is a bit different than the code above)

Just like a sequence, a parallel composite will succeed only when all of its children finish their executing with a SUCCESS state. Whenever a child fails, the parallel itself fails, leaving the rest of the nodes in an undefined state (they could’ve finished already or they might still be running).

A Parallel node is typically used to execute two or more things at the same time, like when we need to update a character animation at the same time he’s running towards a goal.

Selector

A selector will execute child nodes just as a sequence, one after the other in serial fashion. Unlike sequences, a selector will succeed whenever any of its children does. On the other hand, it will fail only if all of its children do so. The RUNNING state indicates that there have been no successes so far and there are more child nodes to be evaluated.

We can make an analogy between selectors an the OR operator in a programming language. They are used to branch the executing of a behavior tree based on wether or not nodes are executed successfully.

For example, take a look at the following behavior tree:

Behavior Tree - Selectors

Instead of just printing the result of the X + Y, now we want to output wether the result is good or bad. If the value of X after the addition is 15, then the first node in the selector will succeed, and the string “Good” will appear. The second action in the selector won’t be executed because we already have a good result.

On the other hand, if we modify the values of X and Y in order make the first node of the selector to fail (that is X != 15), the “Good” result will be skipped and the selector’s second child will be executed, producing the output “Bad”.

This is how a Selector is implemented in pseudocode

void Selector::execute( void ) {
    for ( int i = 0; i < N; i++ ) {
        auto state = getChild( i )->execute();
        if ( state != FAILURE ) {
            return state;
        }
    }
    return FAILURE;
}

If we compare the above pseudocode with the one show for Sequences earlier, we’ll noticed that the main difference is the condition for breaking the loop.

About Random Sequence/Selector Composites

At the time of this writing Crimild does not provide an implementation for randomized composites, although I believe that it will in the near future.

A randomized sequence, for instance, may help to add more variation to a behavior. Think about an NPC for a game, doing some chores in his/her house. By using a sequence, the chores will be done always in the way, which is pretty boring. Now, by using a randomize sequence, instead, the NPC will feel more alive doing different things at different points in time.

Conclusion

I hope that you can see the power of composite nodes by now, since they are probably the most used kind of nodes in behavior tree.

They can also be used to modularize our behaviors. By defining sub-trees for adding and multiplying numbers, for example, we can end up with behaviors for calculating complex math formulas by just reusing entire sequences of nodes.

And just imagine what could be accomplished by nesting composites…

In the next episode

In the next article we’ll deal with Decorator nodes and why they are useful to manipulate behavior results.

Stay tuned

 

 

Oh, Behave (I) – Introduction

Time to talk about Austin Powers.

No, wait…

Behavior Trees.

Time to talk about behavior trees.

behave

This will be the first in a series of articles describing what behavior trees are and how Crimild implements them.

Throughout the next articles I’ll be talking about what are the different components in a Behavior Tree, how to create simple behaviors and how to combine them into more complex ones.

Before we begin…

Keep in mind that Behavior Trees were recently introduced to Crimild and, as any new feature, they might change during the course of the writing of this series (actually, that applies to pretty much the entire engine). Whenever that happens, I’ll try and update the already existing articles as much as possible so they don’t get too far behind.

What are Behavior Trees?

A Behavior Tree is a tree-like structure where each node is a self-contained action that executes and terminates independently of the other nodes. In contrast, a Finite State Machine requires that each state also provides the necessary transitions to other states, avoiding reusability of states and therefore limiting its scalability.

Due to their self-contained nature, behaviors can be easily grouped together to represent advanced logics in a modular way.

Since each behavior does not explicitly define a transition to the next one, it’s the parent behavior’s responsibility to execute its children. If we make the analogy with functions (for example, in C++), each function contains one or more instructions executed sequentially. A parent behavior may execute each one of its children in the same way (and the parent itself may be a child of another behavior). But the parent behavior might need to execute its children in a different way, say, only until one of them fails or succeeds. And thats were the true power of Behavior Trees is shown.

The next image shows the simplest Behavior Tree: one that contains only an action that prints the value of a variable:

Behavior Tree - Simple

A single-node behavior tree that prints a value from the context

In order to work, each Behavior Tree has an associated context that stores values which are accessible from every node in the tree. The context is used a way to communicate behaviors with one another, like registers in a low-level programming language. In the example above, the PrintContextValue action will look for the value of “x” (the argument) stored in the context and displays it on the screen.

A little bit more complex tree would look like this:

Behavior Tree - Intro (1)

A behavior tree to add two values

Making an analogy with the x86 Assembly programming language, the above graphic shows a parent node that will execute each of its children in sequence. Then, we have three operations for setting and adding the values of X and Y together and storing the result in X (that’s how ADD operates). Finally, we output the result stored in X.

Due to its modular nature, Behavior Trees can be as powerful as any programming language.

Implementing Behavior Trees in Crimild

The Behavior class represents a single node in the Behavior Tree. It’s an abstract class defining the basic interface for all behaviors and implementing the common code for all of them. Probably its most important method is the step() one, that is executed every frame for the active behavior in the tree. When a behavior is executed, the step() method will return one of three possible results:

  • Behavior::State::SUCCESS: The behavior has completed its execution and has a valid result
  • Behavior::State::FAILURE: There was a problem when executing the behavior and there is no result
  • Behavior::State::RUNNING: The behavior has not completed its execution yet, so it should be executed at least one more time.

Simple behaviors, like the ones in the diagram above, perform basic operations but they can still fail if the arguments do not match with context values. More complex behaviors, like a character walking towards a destination will require several updates before completing (either by succeeding or failing) and therefore will make use of the RUNNING state while they’re being processed.

The BehaviorContext class contains the data that is shared among all nodes in a behavior tree. The context is usually passed as an argument to the step() function in behaviors. It includes methods for getting and setting values directly and converting them to the correct data types.

The BehaviorContext also stores a reference an agent, which is a Node object that is linked to the behavior tree. In addition to the agent, the context also stores one or more Node objects that serve as targets for some behaviors. For example, if you implement an attack behavior for you game, the agent would be the character attacking while the target would be the victim.

Finally, the BehaviorController component is used to store the behavior tree and execute it each frame. This controller may contain more than one tree, switching them by using messages or other mechanisms based on your app’s logic.

In practice, you’ll end up seeing something like this:

auto node = crimild::alloc< Node >();
auto sequence = crimild::alloc< Sequence >();
sequence->attachBehavior( crimild::alloc< MOVBehavior >( "x", 5 ) );
sequence->attachBehavior( crimild::alloc< MOVBehavior >( "y", 10 ) );
sequence->attachBehavior( crimild::alloc< ADDBehavior >( "x", "y" ) );
sequence->attachBehavior( crimild::alloc<PrintMessage >( "x" ) );
auto controller = crimild::alloc< BehaviorController >();
controller->getBehaviorContext()->setValue( "x", 0 );
controller->getBehaviorContext()->setValue( "y", 0 );
controller->attachBehavior( BehaviorController:SCENE_STARTED_BEHAVIOR_NAME, sequence );
node->attachComponent( controller );

When attaching behaviors to a controller, you need to take into account when that behavior should be triggered. In the example above, we use SCENE_STARTED_BEHAVIOR_NAME to indicate that we want to execute our behavior only once at the very beginning of our program. In contrast, by using DEFAULT_BEHAVIOR_NAME we would end up trigger the behavior every frame, leading to the value of X being constantly incremented by 10 until de program stops.

We’ll talk more about behaviors and events in later posts. For now, let’s just say that, during an update process, the controller will traverse down the current behavior tree (set by an event) following the path described by those nodes that are still running. This will make more sense once we’ve seen sequences in the next article.

At the time of this writing there are several generic behaviors already available in Crimild.

Data driven design

Hopefully, by now you should have enough knowledge about the very basics of behavior trees to see that they can be easily setup in a data driven fashion. Crimild already includes a set of builders for Lua to define behavior trees like this:

local bt = {
    type = 'crimild::behaviors::composites::Sequence',
    behaviors = {
        {
            type = 'crimild::behaviors::actions::PrintMessage',
            message = 'Hello World!',
        }
    }
}

This construct leads to a very expressive solution and now it’s possible to easily write logic for actors in a simulation using scripting without having to actually code it. In practice, I usually group behaviors together in small modules that are later reused by different actors.

It’s a really powerful tool.

In the next episode…

Now that we have a basic idea of Behavior Trees, in the next article we’ll continue talking about sequences and how they can be used to execute behaviors one after the other.

References

If you want to know more about behavior trees, I recommend the following websites: