timer not working in script without scene

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

I created a script, blink .gd, without a scene, the essence of which is to teleport an object to the mouse

player.gd

onready var _blink = preload("res://src/characters/skills/blink/blink.gd").new()
...
func blink():
	_blink.skill(self)

blink.gd

var _timer    : Timer = null
var _cooldown : bool  = false

func _init():
	_timer = Timer.new()
	_timer.set_timer_process_mode(0)
	_timer.set_wait_time(1)
	_timer.set_one_shot(true)
	add_child(_timer)
	_timer.connect("timeout", self, "_reload")
	
func skill(_target: KinematicBody2D) -> void:
        ...
	if !(_cooldown):
		_timer.start()
		_cooldown = true
	else:
		return
        ...

func _reload():
	_cooldown = false

and it doesn’t work. I checked _timer.is_connected and it returns true.
I did something similar in scripts attached to scenes and the timer worked.

UPD
I added to player.gd code add_child(_blink), and timer work, but the script no longer teleported the player.
Sorry for any mistakes, English is not my native language.

:bust_in_silhouette: Reply From: Feanor

Okay, I found the answer.Maybe someone will need it

add_child(_blink) 

really help me. I suspect this is because the timer doesn’t work without an active scene attached.

I have found that too. For timers to work, you need it to be part of the scene tree.

godot_dev_ | 2022-09-21 13:48