Download

Executing every second

I’m trying to get something to execute every second but feel like the method I used isn’t proper. People have mentioned using a timer to achieve this but I don’t really understand how to use the timer nodes.

This is the method I’ve ended up using
http://puu.sh/erau5/0a09f8720b.png

Is there a better way to go about doing this? I understand blueprint logic but not how to use a lot of the nodes correctly.

is this good?

Or create a custom Loop with a delay
so that you can have control.

Timers are kind of sloppy code in unreal. They are not worth the hassle. First you reference them by string name, you can create one with actor reference, but destroying it is anyway with string reference. This is hmm, not epic quality. You change your timer event name, it will update nodes referenced by actor, will not update nodes that are referenced by string. Gl remembering where you placed all those clear timer, or stop timer nodes that use string name as reference.

Timers are also quite unstable: For eg. you set up timer, you restart level, you need to take care of timer and stop it. It gets more complicated when you have more timers. Turns out that they are not fire and forget, they need to be carefully maintained, or you get crashes.
When your game crashes (due to timer) your timer may still tick. I am sure my game started crashing after i added timers, I also got weird split second freezes. All that is gone when i removed timers. Also game runs smooth now.

Do not create loops with delays this is bad, and was bad since first multitasking os.

Instead use event tick, and interpto as timer/counter.
Interpto float from 0 to some max value.
Check with “Nearly Equal” node if your goal is meet. You can give it tolerance of 1-2%.
When goal is meet reset counter, and do your timed task (i call events), and at that point everything is equal to timers.

I have my whole game logic that requires timers done in this way. Each timer has its own “sequence” branch.
Everything uses same variable for speed. I just add different target values for different counters. So I have easy way to speed up or slow down whole game logic.

There is also one big benefit to this, all timer “code” is in single macro, when i need to add or fix some timer issues all its there, not in 10 different spawn timer events.
Not timer related, but it happened few times that i forgot where my code was, or wasted hour looking where is that forgotten print to screen node that i used for debug.
So keeping all similar functionality code in same place may save time wasted on looking.

PS. I may sound a bit angry here, but i am, i wasted whole evening trying to find cause for crashes. And it was that reference by string that did not worked anymore, because it did not updated when i changed event name. And i could not find them all, missed some nodes and fun began.

Thanks! I should have realized it was that simple.

Thanks for the heads up, I’ll try my best to heed your advice. I like your idea of putting all the timers in a single macro.

I am very intriged about this, do you have links with proofs?, I don’t say that was false, It is real curiosity. I use a lot.

I am also interested in this. I use timers at the moment and have noticed some random freezes that last for a second or so. This is not all the time but I could see it being from timers. Ive never had a hard time clearing timers but perhaps the timer is heavier than your method? Is the event tick/interpto commonly thought of as a better method? Would like to hear more reasoning to not use timers.

Sorry For necro bump, but i was asked to explain my idea better, so here it is:

Code is in game mode because this is easiest to reference from anywhere.
And you can assign your events to dispatchers in any place you want they do not need to be in game mode. Only calling dispatcher need to be there.

1 Like

Loops are frame bound. Meaning the next tick cannot start until the loop finishes. Thus they can decrease fps.

Simple exercise. Run a For Loop (0-1000) print string on loop body. Then do it again on 5,000 then 10,000.

Frame will Freeze, then dump the prints.

hahaha, I just found out that he means loop without delay

very helpful! Thanks