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.