blueprint logic problem (using delays for a turret that is shooting)

Hi!

I am working on a turret that shoots players as they enter an area.
Its working except for one bug:

If the player enters the area, I give them a grace period to leave the area. (when he leaves the area “is shooting” is set to false)

After the player does not leave in the time the turret goes on a loop to continiously shoot the target.

Now the problem is: If the player enters the area, stays long enough to get hit, then leaves the area just to go in again during the time the “firerate” delay is still active he will take 2 shots.

One from reentering and one from the time he was in the turret range last time, the one that the delay loop kept.

I thought about a few ways to do it (first, giving every shooting phase an incrementing variable at the first delay, and compare that to a second variable taht will be set at this first delay, then compare them going out of the second delay. but there another problem arises, the whole loop thing could get messed up.

The problem is that Delay nodes queue the graph behind it to be executed and you don’t have a way to prevent it once its queued. If the Delay node is reached twice, it will execute twice which is the case now. You should check out timers: check the blueprint nodes SetTimer/ClearTimer. Timers can be used for a one-off delay (just like the Delay node) and for for looping at a specified rate. Most importantly for your case, they can be cleared and setting the timer a second time will override the existing timer. To use a timer, check out the SetTimer/ClearTimer nodes. The function name that you pass to the timer can be any function in your current blueprint or any custom event you create using Add Custom Event in your blueprint’s main Event Graph.

So for your case try this with two timers:

  • When player enters the area, use a timer that executes a function StartShooting after the grace time delay. The function StartShooting starts another timer that executes your existing function SpawnProjectile at your known fire rate.
  • When player leaves the area, clear both timers using ClearTimer.

The reason why I’m suggesting two timers is to take into account all cases: stopping the turret if its already shooting, and stopping the grace delay if it wasn’t shooting yet. Try it out, timers should work great for you.

ohhhh that sounds promising. I thought all time “If I could just have more control over the delay node”

Ill look into timers.

Edit: works like a charm. ty!

timer is being cleared when enemy leaves turret range.