Version 4.6:
I’m attempting to make it so in my RPG there’s an action select phase before the battle executes, in the state for that phase of the battle the code I have so far is shown below.
extends State
var battle_manager:Node2D;
@export var move_state: State
var active_hero: BattlePlayer;
var ready_to_fight = false;
func enter_state()->void:
print("DEBUG: PHASE 4 //////////////////////////////")
battle_manager = _set_battle_manager();
await select_actions();
func select_actions()->void:
while !ready_to_fight:
for hero in battle_manager._return_heroes():
active_hero = hero[gl_battle.AQ_SCENE_INDEX]
while !active_hero._is_battle_ready():
pass
Problem:
I’m not exactly sure how to go about making it so I can wait for an action to be selected before advancing to the next battler. Any advice is appreciated.
Use signals for these kind of things.
When the battle starts, make your battle manager to select which hero is first and send signal (for example activate) to it. Could be a function call too.
Now the hero is active and the player moves/attacks/whatever with the hero.
When the hero’s actions are used, the hero sends a signal (for example actions_done) to the battle manager. The hero’s actions_donesignal should have been connected to the battle manager, probably when the hero was created or when the battle manager was created. Or you could use a global event bus.
Now the battle manager selects the next hero and sends the activation signal to it.
The main idea is that you don’t have a loop in the battle manager that activates the heroes. The battle manage just reacts to the signals sent by the heroes.
So the battle manager would be something like this:
var current_hero_idx := 0
func start():
# put heroes to an array in correct order
...
# connect signals
for h in heroes:
h.actions_done.connect(hero_done)
# lets start with the first hero
activate_next_hero()
func activate_next_hero():
var hero = heroes[current_hero_idx]
hero.activate() # using function call here
# reacting to hero's "actions_done" signal
func hero_done():
current_hero_idx += 1
if current_hero_idx >= heroes.size():
# all heroes have acted
return
activate_next_hero()