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.
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.
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
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.
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
Maintain a list of actors
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.