EnableBehavior Allocation

Quasimodem

New member
Hello, I have seen this question posed a few times on the forums but still haven't found the proper resolution.

I'm looking for an alloc-free way of Enabling/Disabling my BehaviorTrees. Each of my BehaviorTrees has its own ExternalBehavior, which has been instantiated and Init()ed up front. All the best-practices seem to be in place, yet I still see a little garbage created every time I call EnableBehavior() to pull my object back out of its pool.

If I turn on the deep profiler, I can see a call to Activator.CreateInstance() down deep in the stack, which seems to be the main culprit.

Is there hope for my situation? I've tried playing around with all combinations of PauseWhenDisabled, BehaviorManager.instance.RestartBehavior(), etc, but I seem ultimately unable to avoid a call to EnableBehavior().

If there's a way to have a GameObject activate and then either restart each of its BehaviorTrees or have them pick back up where they left off, without allocating, I'd love to find it!

Thanks in advance -
 
You can call EnableBehaviour once, and then DisableBehavior(true) to pause the behavior tree. When it is paused and you call EnableBehavior you don't need to allocate any new objects.
 
Hey Justin, thanks for the reply.

Some further profiling today has lead me to believe it's not the EnableBehavior() call that's allocating, but something in the Restart logic. If I create a simple behavior with RestartWhenComplete = true, I see small allocations every time my behavior restarts. However if as you suggested, I just DisableBehavior(true) and later EnableBehavior(), I don't see allocations.

So really what my pooling system needs is a way to restart a tree without allocating. I've been experimenting with writing a Decorator task that can sit on top of the whole tree and simply interrupt its child when unpaused, effectively prevent the tree from naturally restarting, much as an infinite Repeater would. Alas, it seems that calling BehaviorManager.instance.Interrupt() causes an allocation as well and I can't think of another way to "reset the playhead", so I think I've just moved the problem around.

Is there any way through in this direction?
 
Internally the tree uses an object pooling system with the objects so assuming you have restarted enough times you should not get any more allocations. Instead of calling interrupt you could use a Self conditional abort at the root of the tree.
 
Top