How to get the function to wait

Godot Version

4.6.1 stable

Question

I am trying to get functions to wait until the other one is done, this is an example that i found online, it prints. Also in my real project, I am calling the function as a callable, such as (callable).call()

Started
Done
After timeout

But I want it to print

Started
After timout
Done
func _ready():
	print("Started")
	try_await()
	print("Done")

func try_await():
	await get_tree().create_timer(1.0).timeout
	print("After timeout")

functions that use await must also be awaited for such behavior, or to use their return value

func _ready():
	print("Started")
	await try_await()
	print("Done")
3 Likes

What for?

I tried but for some reason it does not work, unfortunately

I have arrays of callables to execute, and some of those callables execute their own arrays of callables

1 Like

Can you share your actual code? I can always await on callable’s .call

2 Likes

Please don’t tell people to ask LLMs. That is against our forum guidelines. This forum is for people to help people - not for people to be told to ask LLMs - of which they will more often than not get an incorrect answer. LLMs are notoriously worse at giving correct answers for Godot because of its age, and the fact that a new version comes out every 4 months or so.

Also, using callables in any language is not a simple issue. It is a complex architectural decision to be avoided when possible.

Like @gertkeno said, you should show us your actual code on here instead of pseudocode. There’s a LOT of nuance lost in Godot when trying to use pseudocode because code always has a relationship to nodes somewhere.

As @wchc asked, it would be good to perhaps take a step back and describe the problem you are trying to solve with arrays of arrays of callables. There is likely a Godot way that would not only be simpler, but likely more performative. Your question indicates that you are an experienced programmer trying to leverage your knowledge of programming without really understanding how the Godot engine works. It appears what you are doing is what is known as the XY Problem. I would recommend you present the problem instead of the solution in this case.

3 Likes

So I found the issue, in my real project, some called functions had even more called funtions in them, which did not have the await keyword

This is a great reason as to why you should present your actual code instead of pseudocode.

I still recommend you present your actual problem, as you are very likely complicating things for yourself down the road.

1 Like