How to track the time elapsed between events firing?

I’m trying to figure out how you can track the time elapsed between an event firing and firing again. More specifically, an event sets the NPC’s physics to “simulate” (IE the NPC is dead and ragdolls) - counter starts. Next time the same event fires (in another instance of that NPC), the counter stops and I can grab the amount of time that has passed and check to see if it’s less than X.

I don’t want to use a Timer because I’m not counting down, and I don’t want to trigger an event after X. I just want to know how much time has passed since something happened.

I initially misread your question so I changed my answer, but I think something like this could do what you want:

1 Like

I think my answer on this thread How to stop spam input for shooting? - Programming & Scripting - Unreal Engine Forums could be of some use to you.

Hey Fotosynthesis, thanks for the detailed reply. I’m missing some info though… what are the default values for Time and Death Timer?
If you’re using game time to set the value of Time, and then subtracting it from itself, aren’t you always going to be left with a value of 0?
I’m not sure I follow your logic here.

I’m not trying to force a delay between events, I’m trying to figure out what the elapsed time between events is. IE player presses Left Mouse - Start counting. Player presses left mouse again (6 seconds later) - Stop Counting. Time elapsed is 6 seconds. But every time the time elapsed might be a different value.

Default Time value would be 0 (it will just get set when the event fires)

You said: “I can grab the amount of time that has passed and check to see if it’s less than X.”

The Death Timer would be your “X”, so put whatever you want there. It will only control the flow in the Branch that comes after it.

This logic works because the moment the function gets called, it saves the current game time as the “Time” variable. Then, when it gets called again, it looks again at the current game time and subtracts the saved “Time” (from the previous time it triggered), which will give you the elapsed time between the two events, expressed in seconds. Then, it saves the current time again as the “Time” variable, to be used the next time the event gets called.

Everything after that is just to print the time on the screen and to make the comparison you mentioned in the first post (is the elapsed time less than X).

This way of reading a saved value (in this case Time), manipulating it and then saving it to be used in the next frame/call, is s a very common way to manipulate variables in blueprints and once you understand it you will find yourself using it all over the place.

Ok. So essentially what you’re doing is storing the current game time as a variable at the moment the event is fired (lets say 30 seconds) and then subtracting it from the real game time (31 seconds) and as long as that value (1 second) is less than your Death Timer (3 seconds) then it’s all good. Cool! That’s exactly what I need. Thank you.
Although, shouldn’t the Time value be stored BEFORE you set the Time Since Last Death Value? Or does it not matter which order?

No problem :slight_smile:

The key here is to make the subtraction using your old value before you SET it to the current value.

Thats actually what this does as well, it checks the time between two events. I just thought mine was alittle neater than the previous example. As long as you get what you need done in the end.

How would I track the time between kills in real time?
Right now, we’re only getting the info when the NPC death event is called so I don’t know if the bool fails until the next NPC is killed. I need to know the moment the bool fails.

I’m not understanding what it is that you’re doing in your example actually.

Ok, I actually figured out how to track this in realtime and I also simplified things a bit. There really wasn’t a need to store the Time Since Last Death value when I’m checking it on tick. Also, I changed the name of your “Time” node to be called “Moment of Death” as it makes it read more clearly. I didn’t understand what was happening before when it was just called “Time”

My new Setup:

From the NPC Death event I run this:

And on Event tick I run this:

The default value for the Moment of Death needs to be a negative value greater than the value of your “Combo Time”. This way it forces the number to always be greater than your Combo Time" until it’s “reset” when you kill an NPC.

Thanks for the help Fotosynthesis, I wouldn’t have figured this out without you.

This is fantastic! Thank you! A great method for setting the frequency of any event.