I don't quite get Set Timer By Function Name and Get Timer Elapsed Time By Handle

Hi. I’m using blueprints to build a pretty basic sniper type game.
An “enemy” appears on the screen and I want to do two things:

  1. Determine how quickly it takes the “sniper” to shoot the enemy eg. 1.3 seconds
  2. If after three seconds the enemy isn’t shot then the sniper “loses”.

I’ve had a look at the Unreal docs below to try and figure out how to use the timer.

https://docs.unrealengine.com/en-us/Gameplay/HowTo/UseTimers/Blueprints

In the image below from the docs the “PlayerFire” is a custom event and when the Player walks into the fire the function “FireDamage” is called and this reduces the player health.

What I don’t quite get is how the timer is started and stopped and what the time value of “.75” is doing? And also what the return value is on Set Timer by Function Name. And whether the white execute pin in Set Timer By Function Name can be started by any executable action or does it have to be an “event”?

The other node I don’t quite get is “Get Timer Elapsed Time by Handle”. Is the idea that we use “Set Time by Function Name” and the return this value to a “Timer Handle” variable and then we can get the timer handle with “Get Timer Elapsed Time by Handle”?

Thanks.

.75 is the time in seconds until the function with the name “FireDamage” will be called.

Since Looping is checked then “FireDamage” will be called over and over again every 0.75 seconds.

The timer is started when it is Set.

You can save the timer handle as a variable (promote the return value to a variable) and if you clear it it is stopped.

If you don’t want to save a variable you can also simply “Set Timer by Function Name” again but with a Time of 0 (0 = stop)

The reason that your “Clear timer by Handle” in your screenshot isn’t working is because of “Scope”. “PlayerInFire” and “PlayerNotInFire” are two separate execution flows so the Return value you are using from above will vanish before it is used in “Clear Timer By Handle”.

Execution pins can come from anywhere it is the input and output pins you have to worry about as they go out of scope when the execution flow is discontinued.

Thx. So that explains the .75 seconds. Is there not a simple timer that can be started on an event and then stopped when another event occurs eg.

enemy spawned -> start timer -> enemy killed -> stop timer

the timer I used I set up as an overlap event

The simple way would be to call “SetTimerByEvent” with a time set and again “SetTimerByEvent” with a time of 0 when you want to stop it. So just 2 nodes.

I’ve attached an image of the Set Timer by Event node. If i’m right then based on how its set up after 5 seconds the event will fire (whatever that event is). What I don’t quite get is what the top white exec pin into the Set Timer does - for example if BeginPlay is hooked up to Set Timer - does that just trigger the timer to start when BeginPlay happens? And what does the return value represent?

Execution pins are what makes things happen. If you add the node in the picture like that it will never be “Set” and if it is never “Set” it will never start the timer so nothing happens. If you hook it up to BeginPlay it will be “Set” at the beginning and after 5 seconds the Event is triggered.

The return value represent kind of a stopwatch attached to your Event. You can save a reference to the “Stopwatch” by promoting the return value to a variable. This way you will be able to later “pick up” the Stopwatch and either press cancel on it, press pause on it or look at it how much time is left until the event is triggered etc.

The Stopwatch is named “Timer Handle Structure”

Thx for answering my question.

So I understand the second paragraph re: the timer handle structure and that the return value represents a stopwatch attached to the event.

But I don’t quite get though why the node needs to first count down to trigger the event before the stopwatch begins.

For example if I just wanted a stopwatch to start when an “enemy” is spawned so that I can then determine how long it took the player to shoot the enemy - in this particular scenario I feel like I don’t really need a countdown timer to trigger an event (which then starts the stopwatch).

Are we saying I can’t just start a stopwatch by the execute pin firing eg. being hooked up to begin play? The stopwatch only starts when the timer countdown and then triggers an event?

Thanks

A timer always needs a time but if you want the function or event to trigger right away and then again by the timer you simply have to call the Function/Event directly AND Set the timer for the next Function/Event call.

So… call the Event plus SetTimerByEvent
or… call the Function plus SetTimerByFunctionName.

Sounds like you want a countdown that if interrupted reports back time elapsed and “win”, and if not interrupted hits 3 seconds and reports “lose” ?
So a 2 step approach is required, surprisingly easy, when you first define exactly what you want, 2 timed events, one for the count down, one for total time, plus a event to stop timers when enemy is hit (using events instead of functions so it all fits in one image).

the float conversions are a bit sloppy, could use a ToText(float) node to set the max fractal digits to something nice for umg.

What’s the best alternative to an event tick if you want multiple execution pins firing off in a loop? Basically, what’s the alternative to an event tick with a sequence node?

Set Timer by Event/Function, custom event/function -> Sequence node or chain of events. Set the timer to looping. Time for loop should be average of delta time per 1 second interval.

Thank you! I used a simple timeline and a delay-loop to get it all working without needing to create functions :slight_smile:

The delay loop was only to provide a small delay I needed in code, to the looping timeline