Tables with object identifiers as the index :(

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
osoII
Prole
Posts: 9
Joined: Tue Jul 16, 2013 4:46 am

Tables with object identifiers as the index :(

Post by osoII » Sat Jul 20, 2013 1:03 am

I'm using love2d and want to add an object to a table with the body as the index then call on that entry in the table from a collision event. From what I have now, I can do that just fine in love.load but if I try to call it again from the collision callback it fails saying the body is a nil value :/ I've put !!!!s next to the two places that should be behaving the same but aren't. Here's my code:


function love.load()
world = love.physics.newWorld (0,10,true)
world:setCallbacks(beginContact,endContact, preSolve)

units = {}

o = {} --make body one
o.body = love.physics.newBody(world, 300, 100, "dynamic")
o.shape = love.physics.newRectangleShape(100, 100)
o.fixture = love.physics.newFixture(o.body, o.shape)
o.info1 = 1
o.info2 = 2

units[o.body] = o --insert it into table with body as index
o = nil

o = {} --make body two
o.body = love.physics.newBody(world, 300, 300, "dynamic")
o.shape = love.physics.newRectangleShape(100, 100)
o.fixture = love.physics.newFixture(o.body, o.shape)
o.info1 = 1
o.info2 = 2

units[o.body] = o --insert it into table with body as index

--calling the table by the body works here but not when called out of beginContact...
units[o.fixture:getBody()].info1 = 2 --!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
o = nil

end

function love.update(dt)
world:update(dt)
for i, v in pairs(units) do
if v.body:getY() > 350 then v.body:setY(350) end --prevent bodies from falling too far so they collide with each other
end
end

function love.draw()
for i, v in pairs(units) do
love.graphics.rectangle('fill', v.body:getX(), v.body:getY(), 100, 100) --draw everything
end
end

---[[
function beginContact(a, b, coll)
local x = a:getBody()
--throws an error?!
units.x.info1 = 2 --!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
end
--]]



HALP!

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

Re: Tables with object identifiers as the index :(

Post by Robin » Sat Jul 20, 2013 2:30 am

You want units[x], not units.x. The latter is equivalent to units["x"].
Help us help you: attach a .love.

osoII
Prole
Posts: 9
Joined: Tue Jul 16, 2013 4:46 am

Re: Tables with object identifiers as the index :(

Post by osoII » Sat Jul 20, 2013 3:50 am

Sorry that was a last ditch "I don't know wtf I'm doing change" it doesn't work the correct way with [] either.

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Tables with object identifiers as the index :(

Post by bartbes » Sat Jul 20, 2013 8:53 am

Yes, when getBody is called love re-introduces the body into lua, however it can't re-use the old lua-side wrapper for that (called Proxy, internally), so it creates a new one. This means that it and the original body are not equal, from lua's perspective.

EDIT: Also, next time, could you please use code tags?

osoII
Prole
Posts: 9
Joined: Tue Jul 16, 2013 4:46 am

Re: Tables with object identifiers as the index :(

Post by osoII » Wed Aug 21, 2013 5:29 am

bartbes wrote:Yes, when getBody is called love re-introduces the body into lua, however it can't re-use the old lua-side wrapper for that (called Proxy, internally), so it creates a new one. This means that it and the original body are not equal, from lua's perspective.

EDIT: Also, next time, could you please use code tags?

AHA! Thank you, that was a very frustrating problem. I'm glad to get some closure on it. I'll use code tags next time, didn't know that was a thing. Thanks.

User avatar
slime
Solid Snayke
Posts: 2888
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Tables with object identifiers as the index :(

Post by slime » Wed Aug 21, 2013 6:07 am

While the values are technically not equal so using them as table keys like that won't work, you can still compare them with '==' because LÖVE's objects take advantage of Lua metatables to provide their own equality comparison operator (so o.fixture:getBody() == o.body will evaluate to true, for example.)

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests