How do you properly handle turn-based delays?

Question

Regarding the code snippet: I’m developing a classic turn-based roguelike,
and I want to wait for the delay during the enemy’s turn before proceeding to my turn, but as shown in the log, the processing is failing.
What could be causing this?

Problem Occurring

The game proceeds on its own, as shown in the log.
The player moves, and then the enemy keeps moving on its own—I can’t control it.

What I’ve Checked and Investigated

Asked a question on Gemini
As shown in the log below, the issue is caused by this part of the provided file.

Reference: git repo scripts/Enemy.gd

    print(“Enemy: isMoving    ”,isMoving)

		#
        # var delay = create_tween()
        # delay.tween_interval(0.7)
        # await delay.finished;

        await get_tree().create_timer(0.7).timeout

Background

The sequence is: player first, then enemy, then back to the player.

What I Want to Know/Do

After my turn ends and before the opponent acts, I want to wait about 0.7 seconds, then execute a movement, and then proceed to the player’s turn.

Environment

Godot: 4.6
Language: GDscript

Github repo(full source code)

git repo: GitHub - amagi-dev/roguelike · GitHub

Player: isMoving	true
Next turn:	Enemy
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Next turn:	Player
Enemy: end
Next turn:	Enemy
Enemy: end
Enemy: isMoving	true
Next turn:	Player
Enemy: end
Next turn:	Enemy
Enemy: end
Enemy: isMoving	true
Next turn:	Player
Enemy: end
Next turn:	Enemy
Enemy: end
Enemy: isMoving	true
Next turn:	Player
Enemy: end
Next turn:	Enemy
Enemy: end
Enemy: isMoving	true
Next turn:	Player
Enemy: end
Next turn:	Enemy

What movement.Player side or enemy side?

Don’t use await. Instead use a regular signal handler in which you switch the game state from enemy turn to player turn.

await inside of _process is almost always a terrible idea, including this case. You should avoid await (in general), instead emit a signal once the turn is complete, then you can handle this signal by processing the next action.

func _process(_delta: float) -> void:

	if isActive == false:
		return;

	move_direction = Vector2i(0,1)
	isMoving = true;

	if(isMoving == true):

		print("Enemy: isMoving	",isMoving)

		#
		# var delay = create_tween()
		# delay.tween_interval(0.7)
		# await delay.finished;

		await get_tree().create_timer(0.7).timeout

		# get current move direction from input
		var c = get_cell_from_position(global_position);
		var target_cell = c + move_direction;
		var pos = get_position_from_cell(target_cell);

		# reset move direction adn set isMoving to true
		move_direction = Vector2i.ZERO
		isMoving = false;

		# moveing in tween
		var tween = create_tween()
		tween.tween_property(self,"global_position",pos,0.15).set_trans(Tween.TRANS_SINE);
		tween.finished.connect(event_finished);
		await tween.finished;

		print("Enemy: end")
		signal_turn_finished.emit() # turn end

And by the rest of your code it looks like you are using a signal_turn_finished, but for some reason put all of this logic into _process, I’d be you could move all of this into it’s own function. Instead of setting “isActive” to true, call the function.

Hello! First of all I would advise you against use of generative AI / large language models like Gemini, ChatGPT or Claude when trying to learn, as they will rob you of your learning process.

About your problem, I am unsure why you’re waiting for a fixed time for the enemy to move, can’t you use Godot’s signal system for this?

What you would do with signals is:

  • Player turn starts
  • Player turn ends → emits a “turn_finished” signal.
  • When signal is emitted, the enemy starts its turn.
  • Enemy turn ends ->emit “turn_finished” signal
  • When signal is emitted, the player starts its turn

Depending on where the turn logic is handled (in a central manager or in the player/enemy scripts) the implementation will vary a little.

Thank you.
Instead, please send a signal when the turn is complete, process that signal, and then execute the next action.
However, I want the enemy to move 0.7 seconds after the player moves—why is it necessary to wait?

Should the player just wait after moving?
I’d like to know how to edit that part.

use deepL

diff --git a/scripts/Enemy.gd b/scripts/Enemy.gd
index 072cb7e..9751125 100644
--- a/scripts/Enemy.gd
+++ b/scripts/Enemy.gd
@@ -22,6 +22,7 @@ func _process(_delta: float) -> void:
 	if(isMoving == true):
 
 		print("Enemy: isMoving	",isMoving)
+		isActive = false
 
 		#
 		# var delay = create_tween()
@@ -37,13 +38,13 @@ func _process(_delta: float) -> void:
 
 		# reset move direction adn set isMoving to true
 		move_direction = Vector2i.ZERO
-		isMoving = false;
 
 		# moveing in tween
 		var tween = create_tween()
 		tween.tween_property(self,"global_position",pos,0.15).set_trans(Tween.TRANS_SINE);
 		tween.finished.connect(event_finished);
 		await tween.finished;
+		isMoving = false;
 
 		print("Enemy: end")
 		signal_turn_finished.emit() # turn end
diff --git a/scripts/PlayerController.gd b/scripts/PlayerController.gd
index c8b77c1..745016f 100644
--- a/scripts/PlayerController.gd
+++ b/scripts/PlayerController.gd
@@ -39,6 +39,7 @@ func _process(_delta: float) -> void:
 	if (isMoving == true):
 
 		print("Player: isMoving	",isMoving)
+		isActive = false;
 
 
 		# get current move direction from input
@@ -48,13 +49,13 @@ func _process(_delta: float) -> void:
 
 		# reset move direction adn set isMoving to true
 		move_direction = Vector2i.ZERO
-		isMoving = false;
 
 		# moveing in tween
 		var tween = create_tween()
 		tween.tween_property(self,"global_position",pos,0.15).set_trans(Tween.TRANS_SINE);
 		tween.finished.connect(event_finished);
 		await tween.finished;
+		isMoving = false;
 
 
 		signal_turn_finished.emit()	# turn end

Move your isMoving = false after the await tween.finished. That is when they aren’t moving any more.

Set isActive = false earlier, once the action is started.

Thank you. However, I’m encountering a bug.
After the player moves, a large number of enemies move all at once.
They move a few times and then stop, but when I move the player again,
the enemies might move just once—the behavior is irregular, and I can’t figure out the cause.
As mentioned in another comment, I suspect the await statement might be the cause. What should I do?

Also, is this even correct to begin with?

Log

Player: isMoving	true
Next turn:	Enemy
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Enemy: isMoving	true
Next turn:	Player
Enemy: end
Next turn:	Enemy
Enemy: end
Enemy: isMoving	true
Next turn:	Player
Enemy: end
Next turn:	Enemy
Enemy: end
Enemy: isMoving	true

source code



# Enemy
func _process(_delta: float) -> void:

	if isActive == false:
		return;


	move_direction = Vector2i(0,1)
	isMoving = true;

	if(isMoving == true):

		print("Enemy: isMoving	",isMoving)

		isMoving = false;

		# get current move direction from input
		var c = get_cell_from_position(global_position);
		var target_cell = c + move_direction;
		var pos = get_position_from_cell(target_cell);

		# reset move direction adn set isMoving to true
		move_direction = Vector2i.ZERO

		# moveing in tween
		var tween = create_tween()
		tween.tween_property(self,"global_position",pos,0.15).set_trans(Tween.TRANS_SINE);
		tween.finished.connect(event_finished);
		await tween.finished;

		isMoving = false;

		print("Enemy: end")
		signal_turn_finished.emit() # turn end
	pass
pass

# Plaeyr
func _process(_delta: float) -> void:

	# ESC to quit ## Debug only
	if Input.is_action_just_pressed("esc"):
		get_tree().quit()
	pass

	# check if my turn
	if isActive == false:
		return;



	if Input.is_action_just_pressed("arrow_up"):
		move_direction = Vector2i(0,-1)
		isMoving = true;
	elif Input.is_action_just_pressed("arrow_down"):
		move_direction = Vector2i(0,1)
		isMoving = true;
	elif Input.is_action_just_pressed("arrow_left"):
		move_direction = Vector2i(-1,0)
		isMoving = true;
	elif Input.is_action_just_pressed("arrow_right"):
		move_direction = Vector2i(1,0)
		isMoving = true;
	pass

	if (isMoving == true):

		print("Player: isMoving	",isMoving)

		isMoving = false;

		# get current move direction from input
		var c = get_cell_from_position(global_position);
		var target_cell = c + move_direction;
		var pos = get_position_from_cell(target_cell);

		# reset move direction adn set isMoving to true
		move_direction = Vector2i.ZERO

		# moveing in tween
		var tween = create_tween()
		tween.tween_property(self,"global_position",pos,0.15).set_trans(Tween.TRANS_SINE);
		tween.finished.connect(event_finished);
		await tween.finished;

		isMoving = false;

		signal_turn_finished.emit()	# turn end
	pass
pass

Solved.

The _process function runs every frame, even when it’s not that object’s turn.
As a result, even if you modify a variable inside an if statement,
the variable gets reset every frame because the function runs every frame.

Is it because the AI’s answer is wrong?
Should I check the official documentation?

AI is typically wrong, but there isn’t official documentation for turn-based systems. Avoid using await in general, it hurts more than helps.

What’s the actual solution?

not use await

Sounds like there should be one, no. With FSM (is it needed?) and tips how to organize the project to avoid it being killed by complexity.

Turn based systems are complicated and opinionated, there is an official tutorial for “Your First 2D Games” is also complicated and opinionated but serves the purpose of being an introduction to the engine as a whole. I don’t think opinionated systems should have official documentation, if there are dozens of correct ways to do something depending on what you want from your game, then the docs shouldn’t point at- and maintain one solution to the problem.

At it’s core turn based systems only do two things

  1. Maintain a list of actors
  2. Progress through each actor’s action(s)

An open ended tutorial only focusing on these two aspects would cover Arrays and maybe signal handling. It’s probably best left to 3rd parties to make tutorials on the subject, or let users explore with the right tools to make what they want.

Before the gang of four came up with their 23 design patterns, I guess people kept reinventing the wheel over and over. And over time from those 23 some appeared more practical. Godot uses Observer and maybe others - I am not sure.

So having a dozen of opinionated patterns for building turn-based games is a logical evolution. A necessary step to find the most useful practices and give them catchy names.