Hello there,
I built a program that is supposed to manage a battle, wait for one of the players to get its turn to play, and then should execute a tween (movement and animation) for this player before resuming the "who's turn is it" calculation.
Basically it looks like this :
BattleManager has a child that is an instance of player. For a battle I instantiate more than one player, and keep track of them in a dictionary
After setting up the battle in my battleManager, I have got a bunch of code like this :
func _process(delta):
yield(get_tree().create_timer(0.3),"timeout")
call_deferred("turn_preparation")
func turn_preparation():
set_process(false)
var counter = 0
while counter !=1:
for i in range(battle_teams.size()):
battle_teams[i].Init=battle_teams[i].Init+battle_teams[i].Speed
if battle_teams[i].Init >= 100:
print("A player is ready : ",battle_teams[i].Name)
battle_teams[i].Init=0
call_deferred("player_turn",i)
yield(battle_teams[i].Battler,"finished_moving")
counter =1
break
func player_turn(player):
print("Current Player: ",player)
yield(battle_teams[player].Battler.move_battler($FightingPosition.position,"chosen_attack"),"completed")
print("Battler is done moving ?")
call_deferred("set_process",true)
So what I expect it to do is to go through my dictionary of players, increment their "initiative" and if someone reachs a limit, that's is turn to move in the player_turn().
I am expecting the thing to go back to their natural states when the player has finished his turn and set the process back to true.
I also tried something like this :
battle_teams[player].Battler.call_deferred("move_battler",$FightingPosition.position,"chosen_attack")
yield(battle_teams[player].Battler,"finished_moving")
with little success.
On the Battler side, I have a move_battler method that looks like this :
func move_battler(new_position,new_animation):
var initial_position = Vector2()
initial_position = self.rect_position
$Character/Hp_bar.hide()
print("Origin / Target: ", initial_position," / ", new_position)
$Tween.interpolate_property(self,"rect_position", initial_position,new_position,0.5, Tween.TRANS_LINEAR, Tween.EASE_IN)
$Tween.call_deferred("start")
yield($Tween, "tween_completed")
$Character/Skin/Battler.animation= "Blue"
#some random animation
$Tween.interpolate_property(self,"rect_position",new_position,initial_position,0.5, Tween.TRANS_LINEAR, Tween.EASE_OUT,2)
$Tween.call_deferred("start")
yield($Tween, "tween_completed")
$Character/Hp_bar.show()
emit_signal("finished_moving")
So what it is supposed to do is to get the target position, hide some UI around the deisgnated player, have him to move, execute and animation, move back to his initial position and emit the signal that would call the end of his turn. Pretty basic, and probably badly written.
But here is the things, despites all the yields and call_deferred, all the players are moving while the tween is not finished for the first player. All in all, the players do not even have the time to go back to their initial position and end up barely moving around the target (temporary position) instead of moving one by one.
I tried many things (and my current code is not correct, I am still trying...) like pausing everything except the battler, or yield(Battler) for the "finishedmoving" signal instead of "completed", yield the $Tween for tweencompleted or tweenallcompleted etc.
Yield on tweens does not work for me (and it can wait for a signal that is never emitted and that I do not know how to force...) and I feel that my code is not executed as a sequence as my output looks like this :
A player is ready : Player 1
Current player : 0
Origine / Target : (x,y) / (x',y')
A player is ready : Player 3
Current player : 2
Origine / Target : (x,y) / (x',y')
A player is ready : Player 4
Current player : 3
Origine / Target : (x,y) / (x',y')
A player is ready : Player 2
Current player : 1
Origine / Target : (x,y) / (x',y')
(...)
and then
Battler is done moving ?
Battler is done moving ?
Battler is done moving ?
Battler is done moving ?
Battler is done moving ?
Battler is done moving ?
Battler is done moving ?
Battler is done moving ?
I tried to make my problem as clear as possible. Do not hesitate to mention all the things I have done wrong (I am clearly a beginner) or ask for more detailed information.
I am a little bit stuck here so any help would be greatly appreciated :)
Best regards !
Bk