Hello. In my project, I have an array of my custom class,
SnakeInfo, shown here.
var x : float
var y : float
var dir : int
var type : int
var curve : int
func toVector2() -> Vector2 : return Vector2(x, y)
and the array
var bodyPositions : Array
Every element of the array is assigned with the type
SnakeInfo, so, you can expect to access individual elements of my class snake info.
bodyPositions should return a value of type'
SnakeInfo, which I can then edit the values of by using the
bodyPositions.curve = 15 or something like that.
The problem I'm having is this. In one of my functions, when I assign a variable of
bodyPositions, I am also editing the value of
bodyPositions Tried and tested below:
bodyPositions.dir = direction
bodyPositions.type = TYPE.head
bodyPositions.curve = CURVE.noCurve
var beforeBug0 = bodyPositions.toVector2()
var beforeBug1 = bodyPositions.toVector2()
bodyPositions.y -= Global_Vars.sector
bodyPositions.y += Global_Vars.sector
bodyPositions.x += Global_Vars.sector
bodyPositions.x -= Global_Vars.sector
if bodyPositions.toVector2() == beforeBug0:
# This branch doesn't execute
print("bodyPositions didnt change?")
if bodyPositions.toVector2() != beforeBug1:
# This branch executes
Here I only change the values of bodyPositions, but the values of bodyPositions and bodyPositions change at the same time. This was a nightmare to figure out where in my code this went wrong, and the result doesn't make any sense to me.
I wish to know why this happens, and how I can prevent this kind of bug from happening. Thanks for your time and help in advance