Best Way to Use Timers

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tiernich
:warning: Old Version Published before Godot 3 was released.

I’m using a lot of Timers in a current project. My approach is:

TimerState = false

func fixed_process( delta ):

   if TimerState == false:
       get_node( "/timer" ).start()
       TimerState = true
       #do stuff
...
On_TimerOut():
     TimerState = false

I do that because the Timer is in fixed_process, every time this function is executed, timer is restarted withou even finish. Assuming that i must have this timer in fixed process, there is a way to check if the Timer is runnig, to not restart? Something like if timer.is_runnig() == true?

Or i am using timer wrong
Or the only way is having a variable to check has i did above?

Thanks folks!

Why do you need to have the timer in _fixed_process though? Typically you could just have the timer restart itself in its timeout callback, instead of checking at each frame if it’s running and restarting it when it’s not.

Akien | 2016-04-22 07:40

I forgot that timers exist. I always manage myself timed events with a variable and _fixed_process, since it costs almost nothing to execute for the CPU.

Gokudomatic2 | 2016-04-22 08:46

I’m developing an AI, and need check many states every frame, and this states are functions like shoot, and i have timers for reloading, firerate, even turn around take some time, the timers are in thoose functions in fixed process, because they need be executed every frame… well, maybe there is a better approach to this. Is my first serious AI and i am learning, maybe i will change the process in the future…

tiernich | 2016-04-22 15:21

@goku haha, i never forget than, i think they are so powerfull, when timer.is_active() be implemented, there will be a great improvement!

tiernich | 2016-04-22 15:23

:bust_in_silhouette: Reply From: SupToasty

Timer.is_active() has been added to the development branch for godot. So if you want to make a custom build visit the git hub here. If you don’t want to do this then, continue to use the method you have been. I personally have asked this question before and I feel that timers should have the option to reset when Timer.start() is called as well as the option to prevent this like you are wanting. You could request this as a feature if you still don’t like Timer.is_active() at the git hub issues here.

Times.is_active() looks just fine. Will wait for next release. Thanks for the info

tiernich | 2016-04-21 22:26

Not a problem.

SupToasty | 2016-04-21 22:38