libgdx/gdx-ai

How to correctly pool behavior trees (and tasks)?

Quillraven opened this issue · 0 comments

Hi,

I am wondering how to correctly implement pooling with behavior trees and their tasks. I couldn't find an example anywhere.

From the documentation you should set the PooledBehaviorTreeLibrary to your manager. It also mentions that you should call disposeBehaviorTree to free the tree. Finally, it mentions something about a TASK_CLONER where I am not sure how that should be used.

What I got so far:

// create manager
private val bTreeManager: BehaviorTreeLibraryManager = BehaviorTreeLibraryManager.getInstance()

// set the pooled library
bTreeManager.library = PooledBehaviorTreeLibrary()

// create a tree instance with a custom blackboard which gets also pooled
bTreeManager.createBehaviorTree(treeFilePath, blackboardPool.obtain())

The code above is working when getting called for the first time. The second time it throws an exception because the tree's root task is null after calling following code as a cleanup to the first call:

blackboardPool.free(behaviorTree.`object`)
bTreeManager.disposeBehaviorTree(treeFilePath, behaviorTree)

The reason is, that the reset method of a BehaviorTree simply sets it root to null. When making now a second call to createBehaviorTree it returns the previous instance but doesn't initialize the root correctly in my opinion.

Is this intended?

Also, from my understanding any tasks created by createBehaviorTree are not managed by a Pool and I don't see any logic in BehaviorTree which would reset or free its tasks. How is it inteded to be used?