0 votes

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

Godot version 3.2.1
in Engine by (16 points)

1 Answer

+1 vote

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.connect("timeout", self, "on_button_timeout")

func on_button_timeout() -> void:
#do whatever you want in this signal

func on_button_click() -> void:
#this is the click the button function

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:

func button_trigger_method_wherever() -> void:
#in the button method where it is

# when the timer times out in the global script it will trigger its timeout method where you can do what you need to inside

by (284 points)
edited by

Thanks, it works :D

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 [email protected] with your username.