Await breaking array size count in for loop

Godot Version

4.6.3

Question

i am currently trying to make a sequence of animations to play one after the other, as errors would occur when they all were called simultanoesly, but as soon as i put an await command in it tells me my incrementor in a for loop doesnt exist on the array that clearly has it as a valid index. is my usage of await breaking the array or is there some other workaround to get the intended sequence? also this is my first real long term project so i apolgize for some messy code.

	for i in range(type_dmgary.size()):
        #for context this block is essentially repeated for each differnt damage type to play an animation for
		if type_dmgary[i] == "BASIC": #this is where it errors and only when await is present saying "invalid index 1 in base array"
			for j in range(0,2,1):
				for k in cur_enemarry.size():
					if cur_enemarry[k].grid_row == select_row && cur_enemarry[k].grid_col == j && cur_enemarry[k].total_queued < cur_enemarry[k].health_cur:
						
						cur_enemarry[k].total_queued += val_dmgary[i]
						any_hit = true
						proj_wizard._anim_fly(cur_enemarry[k].grid_row,cur_enemarry[k].grid_col,"BASIC",k,val_dmgary[i])
						await get_tree().create_timer(0.8).timeout
						break
						
					if any_hit == true:
						any_hit = false
						break

Your code should work the same independent of any await unless something outside that for loop changes the type_dmgary before the for loop has finished.

  • in the case without await only code within the for loop (or in functions called within the for loop) can change that array.
  • with await the code modifying type_dmgary could be in any other method that has access to that field.

Don’t use await.
Schedule things using a tween instead.