I'm trying to do some OOP in Lua (newcomer), and I've come to this problem. I think it may have something to do with pointers although I can't seem to wrap my mind around it.
This is the code:
Atom = {
px = 0,
py = 0,
vx = 0,
vy = 0,
r = 255,
g = 255,
b = 255
}
function Atom:new ( pos_x, pos_y, r_t, g_t, b_t )
local atom = {}
setmetatable(atom, self)
self.__index = self
self.px = pos_x or 0
self.py = pos_y or 0
self.vx = 0
self.vy = 0
self.r = r_t or 255
self.g = g_t or 255
self.b = b_t or 255
return atom
end
Running the following script I get some wacky results:
math.randomseed(os.time())
a = {} -- Can't be local
for i=1,5 do
a[i] = Atom:new(math.random())
end
for i=1,5 do
print(a[i])
print(a[i].px)
end
table: 0000000000799590
0.56619703258596
table: 00000000007994d0
0.56619703258596
table: 00000000007991d0
0.56619703258596
table: 00000000007996d0
0.56619703258596
table: 00000000007995d0
0.56619703258596
As you can see, the referenced tables are different but the numeric value of the variables are the same.
What am I doing wrong?
I apologise if there's something obviously wrong, as previously said, I'm a newcomer :-)