#Infinint loop problem with signal and "for i in" stantment.

4 messages · Page 1 of 1 (latest)

mint bolt
#

Okay, so the goal is for each enemy in the array Global.enemies

Main node code:


func begin_enemies_turn():
    var enemy_team_size = Global.enemies.size()
    for i in enemy_team_size:
        Global.enemies[i]._current_enemy_turn()
        await Global.enemies[i]._current_enemy_turn_ended
        print("signal recived")
    get_node("Player Screen")._begin_player_turn()

Enemy Scene code:

func _current_enemy_turn():
    print(enemy_target_number_local, " began it's turn!")
    _end_current_enemy_turn()
    
    
func _end_current_enemy_turn():
    print(enemy_target_number_local, " ended it's turn!")
    _current_enemy_turn_ended.emit()
    ```

Clearly something is wrong because I get a "infinite loop" alert and the whole thing crashes.  I can only assume this is either a problem with the ``await``  command in the ``begin_enemies_turn():`` function, or something wonky with the connection in ``new_enemy._current_enemy_turn_ended.connect(self.begin_enemies_turn)``

My intention was to run each enemy action, wait for it to finish, then run the next enemy action (using the array ``Global.enemies.size()``) and then after every enemy had 1 action run the ``_begin_player_turn`` function and exit the`` begin_enemies_turn():`` function.  And I THINK the infinit loop is just playing the first enemy's turn over and over again instead of moving onto the next one, though it's hard to tell with the crash. 

How am I creating an infinite loop, and how do I stop it?
trail ledge
#

are you comfortable with the way await works? i would suggest maybe make it simpler? if you keep track of current enemy taking a turn and a list of enemies that has not taken a turn. you can just shift to the next one each time the signal (enemy turn done) is sent

mint bolt
#

I just switched in await because it said yield is not supported and honestly only guessing it still works the same. yeild is supposed to work by waiting until a function finishes, but I have no idea if that's true about await. I just know it pauses out the for i in loop without a signal, and goes infinantly with one.

mint bolt