Code only triggers when I'm watching it debug?

I started experimenting with AI behavior trees. The first few nodes work fine, but then I see some very strange behavior on the last node in the sequence.

I bound left click to a C++ function called FireWeapon. This sets a bool IsFiringWeapon equal to true. That bool is read by the Handgun animation blueprint, and when it’s true, it plays the firing animation. The bool is set to false again once the animation is finished.

In the behavior tree, when I debug, I can clearly see that the full tree is being traversed over and over again. Within the behavior tree task, where I call FireWeapon, I can clearly see via print statements that it’s setting IsFiringWeapon to true. However, the gun only fires once. He draws the weapon, he tracks my movements by rotating to face me, and he only fires the one time. Here’s the kicker: once I shift+F1 and open the animation blueprint to debug, it fires over and over again like it’s supposed to. It makes no sense to me.

I realize some of this may be very specific to my project and nearly impossible to debug without seeing the entire project, but I do think it’s a weird enough bug that maybe someone else saw it before? I found this thread:

but while my project does use replication, I can’t really find a rhyme or reason as to how to apply that to my project. I’m always updating the value on the server and not the client, and their solution seemed to use more of a workaround than a solution.

Any ideas?

My guess is it resets the animation too fast. Try some delay when firing animations, give some time for the animation to complete before calling it over again.

That delay is already built in to anim events and the anim blueprint. Like I said above, it runs normally only when I pull up other tabs to start debugging.

can u upload a screenshot where you call the animation ?

This is the graph where I get the character’s IsFiringWeapon and set the anim blueprint’s IsFiringWeapon? bool. I can print out either IsFiringWeapon variable in real time and see that it’s true.

And here’s the handgun state machine.

234780-statemachine1.png

234791-statemachine2.png

the second transition is IsFiringWeapon - NOT - Can Enter Transistion. can u check ? Other wise it just infinitely loops and keeps going back to idle.

No, the second transition is Time Remaining (ratio) < 0.1. An anim event during the firing animation sets IsFiringWeapon back to false. When the player uses this same animation logic, it works perfectly (and I’ve tested it with multiple players in the same session). It’s only via AI characters that I’m seeing this strange behavior.

Can u check by placing a return node in your shoot task if “is firing weapon” is true. Make sure to print something below, so you can see whether it returns or calls the function.

So here’s the disconnect. I put print nodes in both the handgun anim blueprint and in the FireAtTarget task called by the decision tree. In the anim blueprint, the print statement shows the transition between true and false states for IsFiringWeapon very rapidly; exactly as quickly as it should be making those transitions. In the AI task, it reflects exactly what I’m seeing when testing the game, in that it’s only switching to true once and then immediately switching to (and staying as) false. If it was a replication issue, I’d expect the value to never change rather than only changing once.