Topic was automatically imported from the old Question2Answer platform.
Asked By
muncuss
Hello, i just start creating my first game in godot, i have beginner question.
So I have a MainScene with some node2d as its children. I want to create something like this, “when A button is clicked, it will start a timer then on timeout, add var B value by x”. it sounds easy but i can’t make it work. tried using autoload scene but it just won’t start. place the timer in mainscene but the timer get paused when i change scene.
So where i need to place the timer node?
i can probably using “traditional” way by calculating timer variable in delta, but i have three global variable with different time interval to add value, so i think using timer node may be more manageable.
kindly comment if you know and sorry for this beginner question and my bad grammar
A general solution to create a timer programmatically and tie it whatever script you want. Then set it’s values and add it to the scene tree in the ready function. Once you have this you can just start and stop when you want. Some code below to help explain a little better.
var buttonTimer:Timer
func _ready(): buttonTimer = Timer.new() #the time you want to wait to trigger in seconds buttonTimer.set_wait_time(5) buttonTimer.connect("timeout", self, "on_button_timeout") add_child(buttonTimer)
func on_button_timeout() -> void: #do whatever you want in this signal buttonTimer.stop()
func on_button_click() -> void: #this is the click the button function buttonTimer.start()
Hope this helps. I forgot to mention this can be done with global script as well. You will just have to set the timer in that script and use it and trigger it from the other code. Such as listed below, you should create methods to hook into starting and stopping
func starttimer() -> void: buttonTimer.start()
func button_trigger_method_wherever() -> void: #in the button method where it is GlobalNodeName.starttimer()
# when the timer times out in the global script it will trigger its timeout method where you can do what you need to inside