How do I call a global wait function? Can I pause my code until the global function stops?

Godot Version

4.3

Question

Sorry if this is a stupid question or if I misuse the terminology; I’m pretty new to Godot.

I’m aware that I can make a wait function with await get_tree().create_timer(float).timeout, but I wanted to use it as a global function (it is a singleton) in Global.gd, so I could simply use Global.wait(float) anywhere else in the same scene. I made this code in Global.gd:

func wait(timex):
. print("So timex equals to: ", timex)
. timex = timex * 1.0
. print("Now it is ", timex)
. await get_tree().create_timer(timex).timeout
. print("The await command is supposed to have worked")

And in this other script I had:
Global.wait(2.0)
[more code]

However, as soon as the function is called, the rest of the code is executed. I have noted, though, that the function is indeed called, but the wait function only works in Global.gs (the last print message is sent 2 seconds later). How can I do a global function so that I can call it anywhere in the scene and make the calling code wait until the global code stops?

Thank you

You have to call it like this: await Global.wait(2.0)
Otherwise the function you’re calling it from is not a coroutine.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.