Lua table question ?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Neolitik
Citizen
Posts: 55
Joined: Sun Jun 28, 2009 3:13 pm

Lua table question ?

Post 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 ! :P
User avatar
Sardtok
Party member
Posts: 108
Joined: Thu Feb 21, 2008 2:37 pm
Location: Norway/Norge/諾威/挪威 (Yes, I'm teh back!)
Contact:

Re: Lua table question ?

Post 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?
Take off every Zigg for great rapist.
Now, outgay that!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Lua table question ?

Post 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...
Help us help you: attach a .love.
Neolitik
Citizen
Posts: 55
Joined: Sun Jun 28, 2009 3:13 pm

Re: Lua table question ?

Post 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 :
Attachments
sp.love
(1.74 KiB) Downloaded 117 times
Post Reply

Who is online

Users browsing this forum: No registered users and 54 guests