Page 1 of 1
Lua table question ?
Posted: Mon Jul 27, 2009 2:18 pm
by Neolitik
hello ,
i'have a question , i m a noob in lua , it' s a terrible language !
exemple :
Code: Select all
Add=function(self)
table.insert(self.Bul, {x=Me.x,
y=Me.y})
end,
Update=function(self)
table.foreach(self.Bul, i want make here Bul.y=bul.y+2)
end,
i dont know how i can increment y of my table with table.foreach()
if there are any solution ?
thank for all !
Re: Lua table question ?
Posted: Mon Jul 27, 2009 3:25 pm
by Sardtok
First of all, you're not supposed to use table.foreach (it's deprecated, and is going to be removed from Lua).
You might want to have a look at:
http://lua-users.org/wiki/ForTutorial
So something like this:
Code: Select all
for key,bullet in pairs(selv.Bul) do
bullet.y = bullet.y + 2
end
I've never coded Lua, so I'm not 100% sure about the above code.
And I'm also guessing that Bul is short for bullet, but you can call the key value pairs whatever you want.
PS:
WTF? No += operator in Lua?
Re: Lua table question ?
Posted: Mon Jul 27, 2009 4:46 pm
by Robin
Mind if I clean up a bit code of both of you? I think having good code here will only help Neolitik and other Lövers.
Code: Select all
function Me:Add()
table.insert(self.Bul, {x=self.x,
y=self.y})
end
function Me:Update()
for key,bullet in pairs(self.Bul) do
bullet.y = bullet.y + 2
end
end
Disclaimer: I may be horribly wrong about this...
Re: Lua table question ?
Posted: Mon Jul 27, 2009 6:30 pm
by Neolitik
thank a lot !
i m understand now "pairs" . It s ok.
i' old qbasic programmer lol
, lua is amazing ! Love more !
Code: Select all
Update=function(self)
for key,B in pairs(self.Bul) do
if B.y < 10 then
table.remove(self.Bul,key)
end
B.y = B.y - 2
end
Code used here :