I have a string of code used to erase the [0] index of an array, however it will delete the item, and there will be 1-2 physics process calls where the second object in the array persists before also being deleted. Here is the code...

	if  the_unit.active == false:
		the_unit.active = true
	elif the_unit.movement == 0 and the_unit.active == true:
		the_unit.active = false
		arr_ship.remove_at(i)

the_unit = arr_ship
var i = 0

for context

  • Assuming that arr_ship and the_unit are dealing with things like nodes which are reference based...

    If you do the_unit = arr_ship[0] then the_unit doesn't reference the first element of the array, it references the same object that the first element of the array references. Both the_unit and arr_ship[0] are pointing to the same object somewhere else in memory.
    When you then erase the first element, the_unit is still pointing to the original.
    So you'd need to do the_unit = arr_ship[0] again after the erase.

Is i always 0? Index 0 is the first element of the array. When that element is deleted, the next element of the array becomes the first element, with index 0. Is that the problem?

If you're only using [i] to delete the first element, you can do it without an index using pop_front().

    The if statement is toggling the_unit.active between true and false, if the_unit.movement stays at 0. If the_unit.active gets toggled to false, then it will get toggled back to true the next time.

    Think like a computer. 😉

      DaveTheCoder but the_unit gets removed, so next time around it should be searching the 2nd unit which has now moved to array[0].

      DaveTheCoder the question would be, when I change the movement for array[0] is that permenantly attached to array[0]? So when another unit occupies [0] will its movement = the changed movement?

      EDIT: so this is the problem, I tested it with print. It isn't changing the unique ship instances variable but rather the variables attached to [0].
      Is there a way to change that with out creating code line to reset the variables on [0]?

      • xyz replied to this.

        xRegnarokx Here's a question. Can you predict the output of this piece of code without running it:

        var arr = [1, 2, 3]
        var first_one = arr[0]
        print(first_one)
        arr.remove_at(0)
        print(first_one)

        Will it be

        1
        1

        Or

        1
        2

        ?

          xyz

          At first thought I'd assume
          1
          2
          However, if it runs like I think, it would create the array, the variable would remember the 1 from array[0] and so when you change the array, array[0] becomes 1.
          So I would think
          1
          1

          I think you don't have full grasp on what arrays actually are. Arrays are just containers for existing objects. You can put objects in and out of arrays, or rearrange their order inside the array. None of it will affect actual contained objects.

          Arrays are like boxes into which you put numerated objects. When you say var the_unit = array[0] the program will create a new reference to the object that's stored in the array and name that reference the_unit. Now both array[0] and the_unit are names that refer to the same object - your actual spaceship node.

            xyz Yes I understand that.

            My confusion is I have the_unit, I change the variable movement, I then delete the_unit from the array. My assumption which I've proven false by testing was that the_unit would then have the unchanged variable stats of the new unit that occupies array[0]. However, the new unique unit moving its index in the array to [0] causes it to take on the variable values of the deleted unit.

            So I guess my thought was that the array stored objects, and if I changed an objects values it changed it for that object rather than for that position in the array.

            • xyz replied to this.

              xRegnarokx Variables and array elements (which too are variables) don't really store objects. They just reference them. They point to pieces of computer memory that hold object's properties and code. You can have any number of variables referencing the same object. For example:

              var node1 = Node.new()
              var node2 = node
              var node3 = node2
              var array = [node1, node2]

              Only one actual node object is created here by calling Node.new(). All three variable as well as both array elements, all refer to the same object. Now take a look at the following code

              node1.moves = 3
              array[1].moves -= 1
              array[0].moves -= 1
              node3.moves = node1.moves - 1
              print(node2.moves) 

              It prints 0 . Each subtraction decrements the same property of the same object. It's just accessed via differently named reference variable each time.

              In languages like GDScript or Python, references are one of the fundamental concepts. Understanding them is crucial for proficient coding.

                xyz That makes sense, and I understand that, that is how I'd expect it to work when you give multiple names for the same object reference.

                However, for me I have two ship nodes that are referenced in an array, when I check movement they both begin at 2, as I decrease the movement of my first ship node the other doesn't change. However, when I erase the reference to the first ship, the second ship reference takes on the variable values of the deleted references variables.

                This is where my confusion is, I would expect when you reference an object in an array and change its variables that it would change it only for that object. However, the variable changes seem to be being fixed to the index position [0] since when a new object reference moves to that position all their variables change to == that of the previous occupant of [0].

                • xyz replied to this.

                  Assuming that arr_ship and the_unit are dealing with things like nodes which are reference based...

                  If you do the_unit = arr_ship[0] then the_unit doesn't reference the first element of the array, it references the same object that the first element of the array references. Both the_unit and arr_ship[0] are pointing to the same object somewhere else in memory.
                  When you then erase the first element, the_unit is still pointing to the original.
                  So you'd need to do the_unit = arr_ship[0] again after the erase.

                    xRegnarokx You can't reference "object in an array". You can only reference an object.

                    When you do the_unit = arr_ship[0] you don't make a reference to a position in the array. You make a duplicate of the reference found at arr_ship[0] at that time, and store it into the_unit.

                    It's important to note here that arr_ship is populated with references. Remember that get_nodes_in_group() returned an array of references (to node objects). It seems you think the array contains objects themselves. This is not the case. It's all just references.

                    So the_unit cannot know what happens in arr_ship array. It simply copied a reference from the array when you made the assignment the_unit = arr_ship[0]. Afrer that, the_unit and arr_ship part ways.

                    Post the script code that's confusing you. The complete script, not just the part you think is confusing 🙂

                      xyz Thanks, between your explanation and Kojack it is no longer confusing me.

                      It wasn't that I was thinking that the array stored actual objects, or I'd be deleting my ships. It was confusion with how array[0] was working in conjunction with the variable. I was thinking that it would call the reference at index[0] and by creating the variable it was just referencing whatever was at array[0].

                      But rather it was creating a reference to the object at array[0] which then was being used for every the_unit reference, and since later I would delete that reference, yet the information was already referenced in the variable I was in essence creating a template using the reference I deleted.