Godot Version
4.3
Context
I call a function from the script that is not attach to any node on scene. So i can’t use get_tree to create timer like this:
Question
Is there any way that func can await amount of time without get_tree()?
4.3
I call a function from the script that is not attach to any node on scene. So i can’t use get_tree to create timer like this:
Is there any way that func can await amount of time without get_tree()?
If your player is a node then you can use their scene tree
await player.get_tree().create_timer(player.DASH_TIME).timeout
But no I don’t think the GDscript scheduler tracks time on it’s own and needs a supplimental timer, which so far only exists on the scene tree.
Oh, thank you. I just wonder if there is the way not using get_tree().
Thank you so much for solution anyway.
Use Engine.get_main_loop()
to get the scene tree outside of a node script
Oh, i didnt know it exited, thank you
func wait_time_method1(seconds: float) → void:
var scene_tree = Engine.get_main_loop()
if scene_tree:
await scene_tree.create_timer(seconds).timeout
func wait_time_method2(seconds: float) → void:
var scene_tree = get_tree() if “get_tree” in self else Engine.get_main_loop()
if scene_tree:
await scene_tree.create_timer(seconds).timeout
func some_function() → void:
print(“Starting”)
await wait_time_method1(2.0) # Waits 2 seconds
print(“After delay”)
func wait_time_method3(seconds: float) → void:
var scene_tree = Engine.get_main_loop()
if scene_tree:
var timer = scene_tree.create_timer(seconds)
timer.connect(“timeout”, Callable(self, “_on_timer_timeout”))
func _on_timer_timeout() → void:
print(“Timer finished!”)
Make sure to format your code pastes