Hi,
Having trouble using a list.
Hope someone can tell me what i am doing incorrectly.
Background:
I needed to pass some parameters by reference.
So, I created a class Vec2 to use for this purpose.
I pass an instance of the Vec2 class (myVec2) to a method in an instance (mytest) of a class (test).
The passed myVec2 has some basic math done to it a few times via the object ref.
Each time math is performed on it, ref is saved into a list of Vec2's (neighbors).
The issue:
Although it seems like different Vec2 values are being told to be added to the
neighbor list via .AddFirst, all that is ever seen in neighbors is the last set of data that was added.
The question:
Why are not all 3 unique Vec2's pairs of data being saved in the neighbor's list?
Having trouble using a list.
Hope someone can tell me what i am doing incorrectly.
Background:
I needed to pass some parameters by reference.
So, I created a class Vec2 to use for this purpose.
I pass an instance of the Vec2 class (myVec2) to a method in an instance (mytest) of a class (test).
The passed myVec2 has some basic math done to it a few times via the object ref.
Each time math is performed on it, ref is saved into a list of Vec2's (neighbors).
The issue:
Although it seems like different Vec2 values are being told to be added to the
neighbor list via .AddFirst, all that is ever seen in neighbors is the last set of data that was added.
The question:
Why are not all 3 unique Vec2's pairs of data being saved in the neighbor's list?
Code:
Import mojo
Function Main()
New MyApp
End
Class Vec2
Field x:INT
Field y:INT
End
Class test
Method FindAtRange:List<Vec2>(ref:Vec2)
Local neighbors:List<Vec2> = New List<Vec2>
ref.x += 1
ref.y += 2
neighbors.AddFirst(ref)
Print(neighbors.First.x + " " + neighbors.First.y) ' expect 10 and 10 : get 10 and 10
Print(neighbors.Last.x + " " + neighbors.Last.y) ' expect 10 and 10 : get 10 and 10
ref.x += 7
ref.y -= 8
neighbors.AddFirst(ref) ' 17 and 2
Print(neighbors.First.x + " " + neighbors.First.y) ' expect 17 and 2 : get 17 and 2
Print(neighbors.Last.x + " " + neighbors.Last.y) ' expect 10 and 10 : get 17 and 2
ref.x -= 1
ref.y -= 2
neighbors.AddFirst(ref) ' 16 and 0
Print(neighbors.First.x + " " + neighbors.First.y) ' expect 16 and 0 : get 16 and 0
Print(neighbors.Last.x + " " + neighbors.Last.y) ' expect 10 and 10 : get 16 and 0
Return neighbors
End
End
Class MyApp Extends App
Method OnCreate()
Local myVec2:Vec2 = New Vec2
myVec2.x = 9
myVec2.y = 8
Local mytest:test = New test
Local temp:= mytest.FindAtRange(myVec2)
Print(temp.First.x + " " + temp.First.y) ' expect 16 and 0 : get 16 and 0
Print(temp.Last.x + " " + temp.Last.y) ' expect 10 and 10 : get 16 and 0
End
End