+1 vote

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.isconnected and it returns true.
I did something similar in scripts attached to scenes and the timer worked.

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

Godot version v3.5.stable.official [991bb6ac7]
in Engine by (22 points)

1 Answer

+1 vote
Best answer

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.

by (22 points)

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

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.