Need some Lua help

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
LordKaT
Prole
Posts: 5
Joined: Wed May 20, 2009 6:33 pm

Need some Lua help

Post by LordKaT »

I've been scratching my head over this one for a bit now, and I'm stumped. I've got a background in C/C++ so fi this is something related to Lua syntax or if I'm just not seeing something, please point it out, even if it's obvious

Code: Select all

function CollisionManager:collision()
	local isCollide = false
	for i = 0, self.colCount - 1, 1 do -- start for1
		isCollide = false
		for j = 0, self.colCount - 1, 1 do --start for2
			if j ~= i and self.colTable[i].colNum ~= self.colTable[j].colNum then -- start if1
				if self.colTable[i].box:isContact(self.colTable[j].box) then -- start if2
					isCollide = true
					if self.colTable[i].collide ~= nil then start --if3
						if type(self.colTable[i].collide) == "function" then --start if4
							self.colTable[i]:collide(self.colTable[j])
						end --end if4
					end --end if3
				end -- end if2
				if isCollide == false and j ~= i then -- start if5
					if self.colTable[i].collide ~= nil then -- start if6
						if type(self.colTable[i].collide) == "function" then --start if7
							self.colTable[i]:collide(nil)
						end --end if7
					end -- end if6
				end -- end if5
			end --end if if1
		end --end for2
	end --end for1
end --end function

(pastebin version here)

Here's what's happening:

The nested loop (for1 and for2) is designed to scroll through an array (or a metatable in this case) and compare each index with every other index in the table EXCEPT itself. As the code is pretty obvious, the idea here is to check for collisions.

My problem is this: if1 is correct and successfully skips executing if2 (therefore if3 and if4 are also skipped); however, when if2 is skipped, if5 *always* returns true UNLESS isCollide == true -- this is true even when i and j are equal!

When if2 is not skipped, if5 behaves as I'm expecting it to.

I know I *could* just check for nil and the objects own ID int he member:collide function, but dammit that wouldn't explain why I'm getting this behavior.

Any sane help for an insane function?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Need some Lua help

Post by Robin »

I'm not sure I've got it right, and I don't know what it's supposed to do, but it seems like you already checked for i ~= j in if1, therefore making the i ~= j check in if5 unnecessary. (Also, you don't need to supply the step in for loops if it's 1 ;))
Help us help you: attach a .love.
LordKaT
Prole
Posts: 5
Joined: Wed May 20, 2009 6:33 pm

Re: Need some Lua help

Post by LordKaT »

Thanks Robin.

I trashed the function, and rewrote it, since it's so small. I really do wish I knew why I was seeing that behavior.

Ah well.
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: Need some Lua help

Post by appleide »

Assuming colNums can identify a specific colTable.... (i.e is similar to index where there is a bijection between indices and objects in the table)

I'd have done your function like this... you seem to have missed a really cool feature.

Code: Select all

function CollisionManager:collision()
	for key1, collisionTable in pairs(self.colTable) do
		local isCollide=false;
		for key2, secondCollisionTable in pairs(self.colTable) do
			if (collisionTable~=secondCollisionTable and collisionTable:isContact(secondCollisionTable)) then
				if type(collisionTable.collide) == "function" then --start if4
					collisionTable:collide(secondCollisionTable)
					isCollide=true;
				end 
			end
		end
		if not isCollide and type(collisionTable.collide)=='function' then
			collisionTable:collide(nil)
		end
	end
end
from above:
-> type(nil) == 'nil' -- evaluates to true
for loop:
-> for k, v in pairs(aTable) do
aTable[k] == v --evaluates to true
end

"if self.colTable.collide ~= nil then start --if3
if type(self.colTable.collide) == "function" then --start if4"
is identical to:
"if self.colTable.collide ~= nil and type(self.colTable.collide) == "function" then --start if4"

Since if the first condition is false it'd just stop reading that line anyway.

and if you know .collide is definitely either nil or a function you can also do...

(self.colTable.collide or function() end)(self.colTable, self.colTable[j]);
LordKaT
Prole
Posts: 5
Joined: Wed May 20, 2009 6:33 pm

Re: Need some Lua help

Post by LordKaT »

Thanks! I'm still rather new to Lua, so I'm still getting over some of my C/C++ predispositions ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 223 guests