Hi
I tested creating these 2 objects. TriangleArr object has an array of Triangle objects as one of its properties.
Inside the TestDraw method we are attempting to create a copy of each Triangle object in the array in order to make modifications, whilst leaving the original object intact. Why does it seem to modify the original object no matter what I try (Cloning - Which is a shallow copy, or even instantiating a new Traingle object with the parameters of the original)?
a1.triangles[1].points[1] should always display 0 in this example, and not increment each time TestDraw is called.
Thanks
a1 := new TriangleArr([new Triangle([0,100,50,0,100,100])], {x:0,y:0}) MsgBox, % a1.triangles[1].points[1] ;% a1.position.x += 25 a1.TestDraw() MsgBox, % a1.triangles[1].points[1] ;% return class TriangleArr { triangles := [] position := {} __New(triangles, position) { this.triangles := triangles this.position := position } TestDraw() { loop % this.triangles._MaxIndex() ;% { ; Both of the following modify the original object s := this.triangles[A_Index].Clone() ;s := new Triangle(this.triangles[A_Index].points) loop % s.points._MaxIndex() ;% { s.points[A_Index] += Mod(A_Index, 2) ? this.position.x : this.position.y } } } } class Triangle { points := [] __New(points) { this.points := points } }