timer that when the time runs out change the scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Juliang2000

I want a timer to have a countdown that changes the scene when it reaches 0

:bust_in_silhouette: Reply From: wombatstampede

The question is? :wink:

I guess you a searching for a hint how to do it. Here it is:

There is a Timer node. Just use it. Put it in a scene that you load as a singleton. (See Project settings Autoload)

Just like you would do with background music that runs independent of scene changes.

If the question was “Do the all the programming for me, I don’t want to read.” then sorry, that this wasn’t the answer.

That was so sarcastically useful. signal aren’t as trivial as an experimented user of godot might think. And using a singleton is even more complex for something as functionally simple as “change scene after a timer” (eg : a splash/title screen).

so i added an actual “i’ll do your homework” (with code) answer, because this isn’t a competition.

Laurent Laborde | 2020-03-30 19:17

Sorry for being sarcastic at times. I just think that when people ask a favor of other people it might be justified to put some more effort in writing the question than just saying “I want”.

I understand that englisch isn’t the mother tongue of every writer (it isn’t mine as well) and I understand that you sometimes need a pointer where to start.

Thank you for posting an answer with details & code. I also think (and forgot it beforehand) that this will be helpful for other people who are searching for an answer to that question.

wombatstampede | 2020-03-31 06:19

Thank you for your understanding :slight_smile:

Laurent Laborde | 2020-03-31 11:36

Questions like this would certainly get a downvote if it was posted on StackOverflow.
Beginners need to learn how to spend some time and effort before asking a question.
I agree with your answer, as you have provided a high level solution to OP’s question.

dirtsea | 2020-08-22 04:11

Aren’t we glad this is not stackoverflow then ? :smiley:
Considering the current state of godot, the simple fact that the OP is using it instead for unity is, imho, already a show of goodwill to learn.
If one wanted their homework done for them, they would unity because their homework has already been done and posted multiple time everywhere. That’s what make unity the industry standard of indy game.

Laurent Laborde | 2020-08-22 09:09

:bust_in_silhouette: Reply From: Asthmar

Doing this will be simple!

Get the timer node

Connect the on timed out signal

and under the timed out function (from the signal ) put your line of code that changes the scene

That’s what I did, but It doesn’t do anything

and I dont even know If 1 stands out 1 second or 1 minutes, or 1 hour idk they just dont explain you a THING

I don’t think 1 stand for 1 second as i put 0.01 as a timer and it still does nothing u heard meh

Ongpol999 | 2023-03-15 22:44

The 1 stands for 1 second. Also, did you call start() on your timer?

Asthmar | 2023-03-15 23:34

:bust_in_silhouette: Reply From: Laurent Laborde

Since it have a good rank on google i’ll answer it, the reference documentation doesn’t have an exemple and it require to understand signaling.

extends Control

var timer = null

# Called when the node enters the scene tree for the first time.
func _ready():
	timer = Timer.new()
	timer.set_one_shot(true)
	timer.set_wait_time(2)
	timer.connect("timeout", self, "on_timeout")
	add_child(timer)
	timer.start()

func on_timeout():
	get_tree().change_scene("res://01_main_menu.tscn")

in timer.connect() the first argument is the signal (the only signal Timer is emitting is “timeout”. Self it the receiver of the signal, and on_timeout is a function name of your choice, i called it “on_timeout” but could have been anything.

And in this function call the change_scene code.

This just gave me such an error

the way how to this is just use signal and just type "get_tree().change_scene(“blabla whatever scene you made.tscn”) underneath the automated made code inside that timer script I think there’s just something wrong with what im doing i guess but this is the code im sure of it

Ongpol999 | 2023-03-15 22:49

oh actually I think 1 just stand as 1 second my bad

check both one shot and auto start in the right small corner when clicked Timer
and write the
“func _on_Timer_timeout():
get_tree().change_scene(“res://main menu.tscn”)” on anywhere in current scene that you are looking at and write a script anywhere like yer playermodel or map idk and type that thing above it and set any time you want like 20 or 50 second when clicked Timer and
it just work i think

Ongpol999 | 2023-03-15 22:58