Need Little help ^^

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

Need Little help ^^

Post by Neolitik »

hello any !

i'm trying to understand Lua , love and all the rest ...( female , world war ... :o )

I have write this :

Code: Select all

--AutoGenerated by DEVotion
PhyRect={}
PhyRect.Rect={}
function Add(Tx,Ty)
	local R={}
	R.Body= love.physics.newBody(PhyWorld, Tx*32, Ty*32,0)
	R.Shape=love.physics.newRectangleShape(R.Body,32,32)
	R.Shape:setRestitution(0.5)
	print( table.getn(PhyRect.Rect))
	table.insert(PhyRect.Rect,R)
	--return Rect
end

function RemoveAll()
	for k,v in ipairs(PhyRect.Rect) do
	     v.Shape:destroy()
	     v.Body:destroy()
	     table.remove(PhyRect.Rect,k)
	     print("remove ="..k.."  tgn"..table.getn(PhyRect.Rect).."")
	end
end
But My RemoveAll() function dont remove all !

Why my function don't remove all ?

If i 'm add :
while table.getn(PhyRect.Rect) > 0 do .......end

in the function , it's ok

thank for help !
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Need Little help ^^

Post by Robin »

Let me help you visualize what happens if you do it the first way:
Frame one:

Code: Select all

{ --this is all pseudocode
> Rect1,
  Rect2,
  Rect3,
  Rect4,
}
(The little arrow is the pointer of the for loop)
The first rect gets deleted, and the loop pointer advances one position:

Code: Select all

{
  Rect2,
> Rect3,
  Rect4,
}
The third rect gets deleted, and the loop pointer advances one position:

Code: Select all

{
  Rect2,
  Rect4,
>
}
Lua thinks now: "Oh, apparently, the loop has ended. That means I'm all done."
Thus the loop terminates, leaving half the entries as they are.

I may be mistaken about the precise process and results, but you get the gist of it?
Help us help you: attach a .love.
Neolitik
Citizen
Posts: 55
Joined: Sun Jun 28, 2009 3:13 pm

Re: Need Little help ^^

Post by Neolitik »

Hum i m think i have dont understand Pairs and Ipairs in lua.

Pairs : for 2 ..4 ..6 ... ?
Ipairs : for 1..3..5..7..?

hum, i dont understand the help of lua ! ^^

how make a "For Block " for pass all entry of my array ?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Need Little help ^^

Post by Robin »

It does visit every entry of your table! The problem is that you remove entries while visiting. A solution would be:

Code: Select all

for i=#myTable,1,-1 do
  table.remove(myTable, i)
end
The difference between pairs and ipairs is visible if you use them on a table like:

Code: Select all

t = {35, 6, 20, [42] = 4, name = "James"}
Try

Code: Select all

for k, v in pairs(t) do
  print(k, v)
end
and

Code: Select all

for k, v in ipairs(t) do
  print(k, v)
end
, and you can see the difference yourself.

(hint: pairs visits every key, ipairs visits only numeric keys, beginning at 1 and stopping when the value is nil.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Need Little help ^^

Post by bartbes »

ill try to explain pairs and ipairs:

Code: Select all

t = {2,9}
t[4] = 5
print("ipairs")
for i,v in ipairs(t) do
--loops through numerical indices till empty
print(i,v)
end
print("pairs")
for i,v in pairs(t) do
--loops through all indices no order
print(i,v)
end
--[[prints
ipairs
1 2
2 9
pairs
1 2
2 9
4 5
Neolitik
Citizen
Posts: 55
Joined: Sun Jun 28, 2009 3:13 pm

Re: Need Little help ^^

Post by Neolitik »

Thank , i have try your code in the Lua demo live.
I 'm understand more now ^^

Big thank for all !
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests