0 votes

Hi! I'd like add Timer to my tiny project (v3.2.1). Instead of creating node "Timer" and then using comand get_Node("Timer") (this code works), I thought i can use timer = Timer.new(). But while running my project jump out error "Timer was not added to the SceneTree"
What am I doing wrong?

extends Node2D

onready var timer = Timer.new()

func ready():
timer.connect("timeout",self,"time
passed")

func process(delta):
tap_counter()

func tapcounter():
if Input.is
actionjustpressed("click"):
timer.setwaittime(1)
timer.start()

func time_passed():
print("wololo")

asked May 31 in Projects by TitanicJoe (14 points)

2 Answers

+1 vote

Exactly what the error is saying. A Timer is a node and can't function if it's not in the scene tree. You need to add_child() it to the tree - in your _ready() function would be a likely place.

There's really no advantage to doing it in code like this. Just add a Timer to the scene and be done. Also you don't need to set the wait time (1 is the default), and you don't need to start it again, because timers repeat by default. You enable "one shot" if you don't want that.

answered May 31 by kidscancode (17,132 points)
0 votes

It depends on which position you want the timer to be in the scenetree. If you want it to be a child of your root node and your root node is Node2D and the script is attached to this root node, do add_child(timer) in _ready(). Then it would look like this:

Node2D

Timer

answered Jun 4 by MaaaxiKing (291 points)
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 How to use this Q&A? before posting your first questions.