+1 vote

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?

in Engine by (521 points)

Is the levelsscreen() method being called? I tried your code on the root node in the scene and it worked for me. I suspect levelsscreen 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

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

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

Please log in or register to answer this question.

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.