0 votes

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.

in Engine by (825 points)

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

show us your project hierarchy

2 Answers

+1 vote
Best answer

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

var freedTimer = Timer.new()
freedTimer.set_name("freedTimer")
...
by (9,754 points)
selected by

This worked, as did the answer by Zylann. So for anyone in the future, both are legit. :-)
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

+1 vote

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()
by (28,835 points)
edited by

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.

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^^

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.