Timers for bullets

Surprisingly I haven’t found an immediate answer to a simple question: How do I make a projectile self-destruct and trigger an explosion after a limited amount of time? I found an Explosion Force extension, but it only seems to address generating the physics force when called.

Main issue is timers: I spawn my bullets using the Fire Bullets extension and behavior, but that only covers spawning the object not what it does afterward. Apparently I need a timer of sorts, but how do you make one for each individual bullet accordingly? Since I don’t want to further increase my scene events with this, I’ll probably wish to make a custom behavior for what bullets do once colliding.

Whatever events you use to spawn the bullet, you should add an action (immediately after the creation, in the same event) to start or reset an object timer, named “explode”, on your bullet object.

Actions immediately following creation of an object will target that specific instance, so you will have a unique object timer per bullet instance.

Then you’d have a separate event entirely that checks the value of that object timer (timer “explode” of bullet => 3 seconds), and you can do whatever actions you want to it.

Not really sure I get the “don’t want to add more to event sheets” comment, as anything you do logic wise will add to the event sheets or behind the scene code that is running. Behaviors and extensions are all running events in the background, the only difference is that you don’t visually see them in the sheet. There is no performance difference. That said, if you are meaning visually on your event sheet, then yes a behavior or function extension should be fine.

1 Like

Thanks! Forgot objects themselves have timers, wasn’t sure how to keep track of individual ones. For some reason this approach causes all projectiles to disappear at the same time once I stop firing, even when created at different rates… not sure why but it looks like the timer is being (re)set globally for all objects of that type, I’ll look into it and maybe it goes away once I move that to a new behavior.

Might also be due to the fact that I Pick All projectiles at the end to check it. Though that doesn’t make sense since the timer is started per projectile and should be different for each one.

The issue I could see happening here is that you’re resetting the timer every frame. Every event is evaluated every frame, so since the player is of type ‘Normal’ could happen more than one frame, the reset of the timer is happening rapidly.

I would add a trigger once to that event.

As far as the other event, “Pick all projectiles” isn’t needed, and could be causing oddities.

1 Like

Possibly. I’ll keep in mind for the future. Just gave bullets and explosions their own behavior with an internal timer, everything works just as intended now :slight_smile: