Waiting for timers to finish

Godot Version

4.2.2

Question

Hello, I’m new to Godot and struggling with the creating a timer that will wait for a timeout before executing the next line of code. I’ve read through the Timer documentation. I’ve also found online lots of suggestions on how to use the Timer. it seems the simplest suggestion is:

await get_tree().create_timer(2).timeout

According to what I’m reading this will create a 2 second time_out and the engine will wait for it to finish before processing the next line of code (is that wrong?). I understand that the process and physics process functions are running repeatedly in the background regardless of this line of code. I don’t use the physics function but in my process function I have a variable called executing _timer. if it’s set to false, none of my code is executed in the process function. so what I’m really doing in my code is this:

    executing_await = true
print("I'm waiting here++++++++++++++++")
await get_tree().create_timer(2).timeout
print("I'm done waiting")
executing_await = false

This function is called from another function which is executing turns for the number of enemies present in a separate for loop:

for n in mob_count:

	print ("\n", "enemy number ", n)
	
	current_enemy_tile = tile_map.local_to_map(mob[n].global_position) 
	print ("current mob tile", current_enemy_tile)
	enemy_distance = [(current_enemy_tile.x -current_player_tile.x),current_enemy_tile.y - current_player_tile.y]

	if abs(enemy_distance[0]) + abs(enemy_distance[1]) == 1:
		print("Enemy should attack! ")
		mob_attack(n, current_enemy_tile)
	else:
		move_enemy(n, get_mob_direction_choices(n))
		print("enemy should move")
		#get_mob_direction_choices(n)
	
	        print("done counting for enemy ", n)

Inevitably, when I run my code it doesn’t wait for the timer and continues with the for loop. So my debug output looks like this:

I’m waiting here++++++++++++++++
done counting for enemy 3
turncount = 9
I’m done waiting

What am I doing wrong? thanks for the help

Which

Which function in this for loop has the awaiting code in it?

If you want to wait for a (yielding) function to finish you must also await the function call

func my_await_function() -> void:
    print(2)
    await get_tree().create_timer(2).timeout
    print(3)


func _ready() -> void:
    print(1)
    await my_await_function() # await here
    print(4)

OK thanks for the reply. I did this and it worked. It seems counterintuitive to me though. If I put an await statement on a timer, why continue with the function that called it before finishing the timer? Like I said I’m new to Godot so I just need to orient myself to the way it works. Appreciate the help.

It sort-of solves a long standing issue with asyncronous functions “infecting” other functions.

https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/

If you do not care when the function finishes this is great, and often the case with void functions. If the function does return something then you must await it to get the value.

func my_await_function() -> bool:
    print(2)
    await get_tree().create_timer(2).timeout
    print(3)
    return true

func _ready() -> void:
    var waited_value = await my_await_function() # required await
    my_await_function() # not required, not using the returned value

yeah great point and that makes sense. thank you again for the help on this!