libgdx / gdx-ai

Artificial Intelligence framework for games based on libGDX or not. Features: Steering Behaviors, Formation Motion, Pathfinding, Behavior Trees and Finite State Machines
Apache License 2.0
1.21k stars 244 forks source link

Shared behavior trees #70

Closed NemesisMate closed 8 years ago

NemesisMate commented 8 years ago

When using the same behavior tree in many objects is a waste of resources to have the tree instantiated for all of them. Instead, it could be great to have them updated like:

public void start(T object); public Status execute(T object);

where the object is the blackboard object being updated like:

tree.step(object);

This way, the same tree could be used for as many objects as they are needed. In lot of games it is normal to use the same ai tree for lot of entities instead of having a different one for every of them.

This is an easy enhancement that I think is very useful.

jacksondus commented 8 years ago

I believe that BehaviorTree has a setObject(T) method to set the instance of your BlackBoard. You generally would do something like the following;

public void updateAI()
{
    for(GameObject obj: objects)
    {
        tree.reset();
        tree.setObject(obj);
        tree.step();
    }
}

This way you can use the same tree for multiple objects. Does this achieve what you want? If not could you clarify?

NemesisMate commented 8 years ago

Not exactly. If this is done and the AI's are executed on multiple threads this would lead to unexpected bugs.

davebaol commented 8 years ago

Yes, setObject does not allow you to share trees properly and that's not its purpose.

Sharing the tree model means that there's a shadow tree for the status of each execution of the model, so memory saving is not so big as you would expect. Also, this would involve huge API-breaking changes and I'm not sure they are worthwhile.