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