So I have this code, the part that isn't working is in func move_turn_order(). When I have it directly using the array arr_ship there isn't a problem to run the code, I just wasn't able to properly reset array (probably a way to do this that I am unaware of). So I use a duplicate to keep the original from being changed, that way my thought was I could reset at the end of my code back to the original again as the template once all my ships have been removed. The problem seems to be for me that it doesn't apply the

if arr_ship_2[i].movement == 0:

but rather it keeps going into negative movement. Is there a reason that when it is using the duplicated array that it isn't measuring when the movement becomes 0, because when I use print(array_ship_2.movement) it shows the movement changing.

@onready var arr_ship = get_tree().get_nodes_in_group("spaceships")
var turn_move = 0
var turn_attack = 0

func _process(_delta):
	if turn_move == turn_attack:
		move_turn_order()
	elif turn_move > turn_attack:
		attack_turn_order()

func move_turn_order():
	var i = 0
	var arr_ship_2 = arr_ship.duplicate(false)
	var ship_active = arr_ship_2[i].active != false
	var ship_inactive = arr_ship_2[i].active == false
	var ship_empty = arr_ship_2.is_empty()
	var the_unit = arr_ship_2[i]
	var curr_pos = the_unit.position
	
	if  ship_inactive:
		arr_ship_2[i].active = true
	if ship_active:
		if Input.is_action_just_pressed("accelerate"):
			the_unit.global_position += Vector2(1,0).rotated(deg_to_rad(the_unit.rotation_degrees)) * 128
			arr_ship_2[i].movement -= 1
		if Input.is_action_just_pressed("decelerate"):
			the_unit.global_position = curr_pos + Vector2(-1,0).rotated(deg_to_rad(rotation_degrees)) * Vector2(128,128)
			arr_ship_2[i].movement -= 1
		if Input.is_action_just_pressed("rotate_left"):
			the_unit.rotation_degrees -= 60
			arr_ship_2[i].movement -= 1
		if Input.is_action_just_pressed("rotate_right"):
			the_unit.rotation_degrees += 60
			arr_ship_2[i].movement -= 1
		if arr_ship_2[i].movement == 0:
			arr_ship_2.pop_at(i)
			if ship_empty:
				arr_ship_2 = get_tree().get_nodes_in_group("spaceships
  • xyz replied to this.
  • xRegnarokx The move counter is already unique for each ship. It is a member of ship's script class, which makes it a unique property for each instantiated ship.

    The ship class should in fact have two properties regarding movement points: movement and movement_max. When you reset ship's movement points, simply assign movement_max to movement.

    var arr_ship = []
    
    func start_turn():
    	arr_ship = get_nodes_in_group("spaceships")
    	for ship in arr_ship:
    		ship.movement = ship.movement_max

    xRegnarokx Don't use a duplicate of the ships array. Work only with arr_ship. Currently you're making a fresh duplicate on each call to move_turn_order(). So even if you pop something off it in the current call, you'll enter the next call with a fresh copy of the full original array. This results in moving the first unit indefinitely regardless of its move counter.

      xyz Okay, that makes sense, however my issue when I used the original was that it wouldn't cycle through the ships the second time around, it would get stuck on the first ship and give me unlimited movement.

      xyz Also the other issue is after going back the the original with this code

      func move_turn_order():
      	var i = 0
      	var ship_active = arr_ship[i].active != false
      	var ship_inactive = arr_ship[i].active == false
      	var ship_empty = arr_ship.is_empty()
      	var the_unit = arr_ship[i]
      	
      	if  ship_inactive:
      		arr_ship[i].active = true
      	if ship_active:
      		if Input.is_action_just_pressed("accelerate"):
      			the_unit.global_position += the_unit.global_transform.x.normalized() * 128
      			arr_ship[i].movement -= 1
      		if Input.is_action_just_pressed("decelerate"):
      			the_unit.global_position -= the_unit.global_transform.x.normalized() * 128
      			arr_ship[i].movement -= 1
      		if Input.is_action_just_pressed("rotate_left"):
      			the_unit.rotation_degrees -= 60
      			arr_ship[i].movement -= 1
      		if Input.is_action_just_pressed("rotate_right"):
      			the_unit.rotation_degrees += 60
      			arr_ship[i].movement -= 1
      		if arr_ship[i].movement == 0:
      			arr_ship.pop_at(i)

      It says invalid get index 0 indicating every mention of the array and when I print the arr_ship at the end it says it is empty, how do I repopulate it?

      • xyz replied to this.

        xyz I figured out the problem I am having, I did what you said, and then did some extra tinkering and fixed my other problem. Now I have an issue where because I am permanently changing the variable movement it isn't resetting when I reset so the next ships movement goes to -1. So I need to figure out how to reset the movement variable.

        • xyz replied to this.

          xRegnarokx Add a function called start_turn() or reset_turn(). After each pop, check if arr_ship is empty and if yes, call that function. The function should reset your state, doing the following:

          • repopulate the arr_ship by calling get_nodes_in_group()
          • iterate through arr_ship and reset the available moves counter for each ship.

            xyz That is a good idea, my issue is what if the move counter is unique for each ship, maybe the way I have my ships set up creates issues for this. Is there a way to use store() to remember variable values and then call them back in the reset, or is that just more complicated?

            • xyz replied to this.

              xRegnarokx The move counter is already unique for each ship. It is a member of ship's script class, which makes it a unique property for each instantiated ship.

              The ship class should in fact have two properties regarding movement points: movement and movement_max. When you reset ship's movement points, simply assign movement_max to movement.

              var arr_ship = []
              
              func start_turn():
              	arr_ship = get_nodes_in_group("spaceships")
              	for ship in arr_ship:
              		ship.movement = ship.movement_max

                xyz Cool, that is a great idea. I am unsure how exported variables work with things like that, so originally my plan was to have pretty much all ships have the same script with all variables related to the ships "stats" be exported and to change them in the inspector and save each ship to be instatiated later as children, that way there is only one script that needs to be engaged with and that the inspector would change each ships unique "stats" is this a bad thing to do, I am not sure of the long term harm or difficulties I might face if I change them manually in the inspector.