Timers in Behavior Tree Tasks Override Each Other in C++

I’ve created a Behavior Tree Task that moves an AI, and while its TickTask() is running, I set a Timer that ticks every 0.25 seconds and checks if the player is in direct line of sight. It all worked fine until I added another AI that uses the same Behavior Tree.

Turns out the Timer only works on the AI that set it last. For some reason the last set timer overrides all the previous ones and they don’t tick anymore.

If I create the same task but in a blueprint, everything works fine, all the timers tick separately with no interference. So why isn’t it the case in C++?

This is how I set the timer:

GetWorld()->GetTimerManager().SetTimer(PlayerSightTimerHandle, this, &UBTTask_ChasePlayerFly::CheckIfPlayerSeen, 0.25f, true);

At first I thought it was because I used GetWorld()->GetTimerManager() instead of GetWorldTimerManager() available in Actors, but that’s not the case. GetWorld()->GetTimerManager() works fine in other types of objects, and even if I get the reference to the AI in the BTTask and use MyAIReference->GetWorldTimerManager(), the issue still persists.

Does anyone know why it happens and how to fix it?

Thank you in advance!

You’ll need to set the following property in the constructor:

bCreateNodeInstance = true;

Here’s another thread discussing this topic:

“Nodes created in c++ are shared for all running behavior tree instances. If you need to store any runtime data, you can either use memory block or switch node to instanced mode and use properties of class.”

Yes, that’s exactly what I needed. Thank you for this valuable information!