I am trying to use the speed variable found in the children within an array, however depending on how I format it I keep getting errors, where it is invalid set index for base Array[node].


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

func _ready():
	arr_ship.sort_custom(func(a, b): return a.speed<b.speed)
	arr_ship.speed = 1
  • A foreach loop might be the piece you're missing:

    for ship in ships:
        if ship.active:
             return
    reset_turn() --> for ship in ships: ship.active = true

    altho for your use case I would have a copy of the ships array named ships_active and pop them until ships_active.empty()

    at the start of the iteration:
    var ships_active = ships_array
    and then next_ship_to_play = ships_active.pop_back() or something like that depending on your context

    anyway everything depends 🙂

arr_ship is array of nodes, so you need to specify which ship from array you want like for example arr_ship[0].speed

    GlyphTheWolf okay thanks, is there anyway to look at the variable for everything in the array without listing out each iteration? Or if the exact quantity of nodes within the array is unknown?

      arr_ship.size()

      xRegnarokx look at the variable for everything in the array without listing out each iteration

      Please explain what you want to do.

        DaveTheCoder I have a variable called active = false, I want to check all the ships to see if all are false, if all are false then I can restart from the beginning of the queue. The point of the active variable is to determine if the ship can move, and because this is turn based only one ship will move at a time. So I have set a variable active = false to search for, in order to control moving to the next node in the array or to restart at 0. That is the specific reason for the question, the general reason is in order to just understand the possibilities of arrays a little better, this is my first time really diving into arrays, and I have found a lot of information on them, but not so much for this specific area.

        The elements of the Array are separate variables. If they're objects with properties that you want to check, then you have to look at each element individually.

        However, there are several Array methods (all, any, filter, map, reduce) that call a method on each element. That may (or may not) be useful in your situation. For example, you could use the Array method all to see if all the spaceship speeds are greater than 5.
        https://docs.godotengine.org/en/4.0/classes/class_array.html#class-array-method-all

        From what you've posted, a loop iteration may be the simplest solution.

          A foreach loop might be the piece you're missing:

          for ship in ships:
              if ship.active:
                   return
          reset_turn() --> for ship in ships: ship.active = true

          altho for your use case I would have a copy of the ships array named ships_active and pop them until ships_active.empty()

          at the start of the iteration:
          var ships_active = ships_array
          and then next_ship_to_play = ships_active.pop_back() or something like that depending on your context

          anyway everything depends 🙂

            DaveTheCoder Awesome mate! Thanks for that information, that is helpful to think through my array usage.

            If you want ships to move one after another on player's command like in a turn-based game, then looping is not really a viable approach. A loop is always executed in one frame which means that you cannot wait for player to trigger movement or for movement animation to finish.

            What you really need is event based approach. Put your ships in an array and sort it in order of movement priority. You already figured out this part. Now make an event handler that's triggered by user input. This handler should start movement animation for the top ship in the array and pop it. If the array is empty at this point, it means that all ships were moved for the current turn.

              xyz ahhh awesome man, that'll keep me from going down a use case that wouldn't work for this. Thinking through loops with what I want to eventually implement was seeming like a nightmare. Time to make an event based turn order!