Recursive function generates null values

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rifen

Hi, I was doing a recursive function to do the combat system of my turn base game.The main problem I have during the second cycle of recursion the function for some reason takes null values ​​instead of the original values.Even variables declared locally like Player_1_Turn_Done become null.

  func combat_Loop (Turn_Order):
	
	var Player_1_Turn_Done = 0
	var Player_2_Turn_Done = 0
	var Player_3_Turn_Done = 0
	var Player_4_Turn_Done = 0
	var Player_5_Turn_Done = 0
	var Enemy_1_Turn_Done = 0
	var Enemy_2_Turn_Done = 0
	var Enemy_3_Turn_Done = 0
	var Enemy_4_Turn_Done = 0
	var Enemy_5_Turn_Done = 0
	
	for i in Turn_Order.size():
		if (Turn_Order[i] == Player_1_initiative) and (Player_1_Turn_Done == 0):
			star_player_turn(Player.Player_1)
			Player_1_Turn_Done = 1
			#This part of code visually indicates the current character turn
			Select_Character_1.show(); 
			Select_Character_2.hide(); 
			Select_Character_3.hide(); 
			Select_Character_4.hide(); 
			Select_Character_5.hide();
			yield(Player, "end_turn")
		if (Turn_Order[i] == Player_2_initiative) and (Player_2_Turn_Done == 0) and (Player.Player_2 != null):
			star_player_turn(Player.Player_2)
			Player_2_Turn_Done = 1
			Select_Character_1.hide(); 
			Select_Character_2.show(); 
			Select_Character_3.hide(); 
			Select_Character_4.hide(); 
			Select_Character_5.hide();
			yield(Player, "end_turn")
		if (Turn_Order[i] == Player_3_initiative) and (Player_3_Turn_Done == 0) and (Player.Player_3 != null):
			star_player_turn(Player.Player_3)
			Player_3_Turn_Done = 1
			Select_Character_1.hide(); 
			Select_Character_2.hide(); 
			Select_Character_3.show(); 
			Select_Character_4.hide(); 
			Select_Character_5.hide();
			yield(Player, "end_turn")
		if (Turn_Order[i] == Player_4_initiative) and (Player_4_Turn_Done == 0) and (Player.Player_4 != null):
			star_player_turn(Player.Player_4)
			Player_4_Turn_Done = 1
			Select_Character_1.hide(); 
			Select_Character_2.hide(); 
			Select_Character_3.hide(); 
			Select_Character_4.show(); 
			Select_Character_5.hide();
			yield(Player, "end_turn")
		if (Turn_Order[i] == Player_5_initiative) and (Player_5_Turn_Done == 0) and (Player.Player_5 != null):
			star_player_turn(Player.Player_5)
			Player_5_Turn_Done = 1
			Select_Character_1.hide(); 
			Select_Character_2.hide(); 
			Select_Character_3.hide(); 
			Select_Character_4.hide(); 
			Select_Character_5.show();
			yield(Player, "end_turn")
		if (Turn_Order[i] == Enemy_1_initiative) and (Enemy_1_Turn_Done == 0):
			star_enemy_turn(Enemy_1)
			Enemy_1_Turn_Done = 1
			#yield(Current_Enemy, "end_turn")
		if (Turn_Order[i] == Enemy_2_initiative) and (Enemy_2_Turn_Done == 0) and (Enemy_2.Enemy != null):
			
			star_enemy_turn(Enemy_2)
			Enemy_2_Turn_Done = 1
			#yield(Current_Enemy, "end_turn")
		if (Turn_Order[i] == Enemy_3_initiative) and (Enemy_3_Turn_Done == 0) and (Enemy_3.Enemy != null):
			star_enemy_turn(Enemy_3)
			Enemy_3_Turn_Done = 1
			#yield(Current_Enemy, "end_turn")
		if (Turn_Order[i] == Enemy_4_initiative) and (Enemy_4_Turn_Done == 0) and (Enemy_4.Enemy != null):
			star_enemy_turn(Enemy_4)
			Enemy_4_Turn_Done = 1
			#yield(Current_Enemy, "end_turn")
		if (Turn_Order[i] == Enemy_5_initiative) and (Enemy_5_Turn_Done == 0) and (Enemy_5.Enemy != null):
			star_enemy_turn(Enemy_5)
			Enemy_5_Turn_Done = 1
			#yield(Current_Enemy, "end_turn")
		check_for_end()
	if Lose == 1 or Win == 1:
		print ("combate terminado")
	else:
		combat_Loop (Turn_Order)

look at arrays and loops plz

Ternarycat | 2019-11-24 13:08

I’m sorry but that comment doesn’t help at all

Rifen | 2019-11-25 14:36

Hi Rifen, that sounds strange. Could you upload a minimal working project that shows the error?

adabru | 2019-12-06 09:45

Thanks for the answer, although it didnt solve the problem I avoid it changing the recursive function for a while.

func combat_Loop (Turn_Order):
var Turn_counter = 0
var Player_1_Turn_Done 
var Player_2_Turn_Done 
var Player_3_Turn_Done 
var Player_4_Turn_Done 
var Player_5_Turn_Done 
var Enemy_1_Turn_Done 
var Enemy_2_Turn_Done 
var Enemy_3_Turn_Done 
var Enemy_4_Turn_Done 
var Enemy_5_Turn_Done 
Lose  = 0
Win = 0
while Lose == 0 and Win == 0:
	Turn_counter += 1
	Player_1_Turn_Done = 0
	Player_2_Turn_Done = 0
	Player_3_Turn_Done = 0
	Player_4_Turn_Done = 0
	Player_5_Turn_Done = 0
	Enemy_1_Turn_Done = 0
	Enemy_2_Turn_Done = 0
	Enemy_3_Turn_Done = 0
	Enemy_4_Turn_Done = 0
	Enemy_5_Turn_Done = 0
	var i = 0
	for i in Turn_Order.size():
		if (Turn_Order[i] == Player_1_initiative) and (Player_1_Turn_Done == 0) and (Player.Player_1.Alive == 1 ):
			Player.Player_1.Calculate_Status_(Player.Player_1)
			star_player_turn(Player.Player_1)
			Player_1_Turn_Done = 1
			#This part of code visually indicates the current character turn
			Select_Character_1.show(); 
			Select_Character_2.hide(); 
			Select_Character_3.hide(); 
			Select_Character_4.hide(); 
			Select_Character_5.hide();
			yield(Player, "end_turn")
		elif (Turn_Order[i] == Player_2_initiative) and (Player_2_Turn_Done == 0) and (Player.Player_2 != null)and (Player.Player_2.Alive == 1 ):
			Player.Player_2.Calculate_Status_(Player.Player_2)
			star_player_turn(Player.Player_2)
			Player_2_Turn_Done = 1
			Select_Character_1.hide(); 
			Select_Character_2.show(); 
			Select_Character_3.hide(); 
			Select_Character_4.hide(); 
			Select_Character_5.hide();
			yield(Player, "end_turn")
		elif (Turn_Order[i] == Player_3_initiative) and (Player_3_Turn_Done == 0) and (Player.Player_3 != null)and (Player.Player_3.Alive == 1 ):
			Player.Player_3.Calculate_Status_(Player.Player_3)
			star_player_turn(Player.Player_3)
			Player_3_Turn_Done = 1
			Select_Character_1.hide(); 
			Select_Character_2.hide(); 
			Select_Character_3.show(); 
			Select_Character_4.hide(); 
			Select_Character_5.hide();
			yield(Player, "end_turn")
		elif (Turn_Order[i] == Player_4_initiative) and (Player_4_Turn_Done == 0) and (Player.Player_4 != null)and (Player.Player_4.Alive == 1 ):
			Player.Player_4.Calculate_Status_(Player.Player_4)
			star_player_turn(Player.Player_4)
			Player_4_Turn_Done = 1
			Select_Character_1.hide(); 
			Select_Character_2.hide(); 
			Select_Character_3.hide(); 
			Select_Character_4.show(); 
			Select_Character_5.hide();
			yield(Player, "end_turn")
		elif (Turn_Order[i] == Player_5_initiative) and (Player_5_Turn_Done == 0) and (Player.Player_5 != null)and (Player.Player_5.Alive == 1 ):
			Player.Player_5.Calculate_Status_(Player.Player_5)
			star_player_turn(Player.Player_5)
			Player_5_Turn_Done = 1
			Select_Character_1.hide(); 
			Select_Character_2.hide(); 
			Select_Character_3.hide(); 
			Select_Character_4.hide(); 
			Select_Character_5.show();
			yield(Player, "end_turn")
		elif (Turn_Order[i] == Enemy_1_initiative) and (Enemy_1_Turn_Done == 0)and (Enemy_1.Enemy.Alive == 1 ):
			Select_Enemy_1.show(); 
			Select_Enemy_2.hide();
			Select_Enemy_3.hide();
			Select_Enemy_4.hide();
			Select_Enemy_5.hide();
			star_enemy_turn(Enemy_1)
			Enemy_1_Turn_Done = 1
			yield(Current_Enemy, "end_turn")
		elif (Turn_Order[i] == Enemy_2_initiative) and (Enemy_2_Turn_Done == 0) and (Enemy_2.Enemy != null)and (Enemy_2.Enemy.Alive == 1 ):
			Select_Enemy_1.hide(); 
			Select_Enemy_2.show();
			Select_Enemy_3.hide();
			Select_Enemy_4.hide();
			Select_Enemy_5.hide();
			star_enemy_turn(Enemy_2)
			Enemy_2_Turn_Done = 1
			yield(Current_Enemy, "end_turn")
		elif (Turn_Order[i] == Enemy_3_initiative) and (Enemy_3_Turn_Done == 0) and (Enemy_3.Enemy != null)and (Enemy_3.Enemy.Alive == 1 ):
			Select_Enemy_1.hide(); 
			Select_Enemy_2.hide();
			Select_Enemy_3.show();
			Select_Enemy_4.hide();
			Select_Enemy_5.hide();
			star_enemy_turn(Enemy_3)
			Enemy_3_Turn_Done = 1
			yield(Current_Enemy, "end_turn")
		elif (Turn_Order[i] == Enemy_4_initiative) and (Enemy_4_Turn_Done == 0) and (Enemy_4.Enemy != null)and (Enemy_4.Enemy.Alive == 1 ):
			Select_Enemy_1.hide(); 
			Select_Enemy_2.hide();
			Select_Enemy_3.hide();
			Select_Enemy_4.show();
			Select_Enemy_5.hide();
			star_enemy_turn(Enemy_4)
			Enemy_4_Turn_Done = 1
			yield(Current_Enemy, "end_turn")
		elif (Turn_Order[i] == Enemy_5_initiative) and (Enemy_5_Turn_Done == 0) and (Enemy_5.Enemy != null)and (Enemy_5.Enemy.Alive == 1 ):
			Select_Enemy_1.hide(); 
			Select_Enemy_2.hide();
			Select_Enemy_3.hide();
			Select_Enemy_4.hide();
			Select_Enemy_5.show();
			star_enemy_turn(Enemy_5)
			Enemy_5_Turn_Done = 1
			yield(Current_Enemy, "end_turn")
		if Turn_Order == null:
			#This fix the null array error, I have no idea why but it seems to work
			print ("null error")
			return

Rifen | 2019-12-07 01:26