Very confuzzled right now

This is what im using to get enemy attackers first positions
def getPosofEnemyAttackers(self,game_state):
Enemy_Attackers = []
if game_state.turn_number!=0:
for unit in all_turn_states[game_state.turn_number -1][0][“p2Units”][3]:
Enemy_Attackers.append([unit[0],unit[1]])
for unit in all_turn_states[game_state.turn_number -1][0][“p2Units”][4]:
Enemy_Attackers.append([unit[0],unit[1]])
for unit in all_turn_states[game_state.turn_number -1][0][“p2Units”][5]:
Enemy_Attackers.append([unit[0],unit[1]])
gamelib.debug_write(Enemy_Attackers)

and it all works well, i used gamelib.debug_write(enemy_attackers) after the for loops to see. It outputted this, which is what I wanted:
[[15, 26], [15, 26], [15, 26], [15, 26], [15, 26], [15, 26], [15, 26], [15, 26]]

Then i called it in my defense algorithm:
def defenseAlgorithm(self,game_state):
self.getPosofEnemyAttackers(game_state)
units_to_place = []
gamelib.debug_write(“If next is [] then i confused”)
gamelib.debug_write(Enemy_Attackers)

and the debug_write at the bottom says this:

[]


yeah im confused
is there something stupid i am doing in here?

My guess is that your variable named Enemy_Attackers in getPosofEnemyAttackers() is a local variable, so modifying it has no effect on the rest of your program.

There are many solutions to this:

  • declaring Enemy_Attacker as a global variable (probably a bad idea)
  • using a class attribute self.Enemy_Attackers (probably what you want to do)
  • returning Enemy_Attacker at the end of getPosofEnemyAttackers()
  • passing Enemy_Attacker as an argument of getPosofEnemyAttackers(). You would then also need to replace Enemy_Attackers = [] by Enemy_Attackers.clear()

Thank you so much!