Timer signal not working?

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

Hi there!

I’m trying to create a timer using this code:

func levels_screen():
	var timer = Timer.new()
	
	timer.set_one_shot(true)
	timer.set_wait_time(1)
	get_parent().add_child(timer)
	timer.connect("timeout", self, "exit_level")
	timer.start()

func exit_level():
	get_tree().change_scene("res://Scenes/UI/Levels.tscn")

But it doesn’t work. If I change the target from self to get_parent() and copy/paste the exit_level() on this node parent script, it works just fine.

I also tried removing get_parent() from add_child(timer) but it did not work as well.

What am I doing wrong?

Is the levels_screen() method being called? I tried your code on the root node in the scene and it worked for me. I suspect levels_screen isn’t called, or maybe it’s being called on a node that isn’t added to the scene?

This is what I used and it printed “TIME” as expected:

extends Node2D

func _timeout():
	print("TIME")
	
	
func _ready():
	var t = Timer.new()
    add_child(t)
	
	t.set_one_shot(true)
	t.set_wait_time(1)
	t.connect("timeout", self, "_timeout")
	t.start()

I’m using Godot 3.1 BETA 1

i_love_godot | 2019-01-15 00:53

Yes, it was beeing called, I even printed some stuff just to make sure.

fpicoral | 2019-01-15 00:55

It’s also working on my pc. Could you share the project so we can test with it?

p7f | 2019-01-15 02:51