How to stop a code generated timer from another function?

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

Hi all,

i have this timer:

var freedTimer = Timer.new()
freedTimer.set_timer_process_mode(1)
freedTimer.connect("timeout",self,"_on_freedTimer_timeout")
freedTimer.set_wait_time(.3)
add_child(freedTimer) #to process
freedTimer.start() #to start

In another function, how can I stop it? I tried get_node("freedTimer").stop() but got an error that it doesn’t exist (null error), so I’m obviously referencing its path incorrectly.

I’m sorry for such a basic question but I do struggle with this aspect. I know I’ll get my head around it soon, just need a nudge for now.

Any and all help much appreciated.

Unrelated, but for clarity you should write freedTimer.set_timer_process_mode(Timer.TIMER_PROCESS_IDLE) instead of 1.

Zylann | 2016-11-29 01:55

show us your project hierarchy

Jatz | 2016-11-29 07:54

:bust_in_silhouette: Reply From: volzhs

Because the Timer node is named automatically, not "freedTimer".
Give a name to Timer node.

var freedTimer = Timer.new()
freedTimer.set_name("freedTimer")
...

This worked, as did the answer by Zylann. So for anyone in the future, both are legit. :slight_smile:
Thank you user volzhs. Much appreciated. This is a useful answer for future users also as it describes the problem, hence marking as preferred answer. Thanks again

Robster | 2016-11-30 01:13

:bust_in_silhouette: Reply From: Zylann

An alternative way is to make your timer available through a variable:

Node A

var freedTimer = null

func your_function():
    freedTimer = Timer.new()
    freedTimer.set_timer_process_mode(1)
    freedTimer.connect("timeout",self,"_on_freedTimer_timeout")
    freedTimer.set_wait_time(.3)
    add_child(freedTimer) #to process
    freedTimer.start() #to start

Node B

func somewhere():
    nodeA.freedTimer.stop()

Thank you, but strangely after trying that it doesn’t give the error, but stop() doesn’t occur. My print statements just keep on showing the timer ticking away. So yes, I think you solved the issue, but now there’s another. I’ll plug away at that though for a bit to not muddle up this thread.

Thanks again.

Robster | 2016-11-30 01:12

Then it’s probably something else you didn’t write in this post. Also notice I made a mistake in mine, it should be nodeA.freedTimer.stop(). I hope you didn’t copy/paste^^

Zylann | 2016-11-30 22:30