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!
Tables with object identifiers as the index :(
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- 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 :(
You want units[x], not units.x. The latter is equivalent to units["x"].
Help us help you: attach a .love.
Re: Tables with object identifiers as the index :(
Sorry that was a last ditch "I don't know wtf I'm doing change" it doesn't work the correct way with [] either.
- 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 :(
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?
EDIT: Also, next time, could you please use code tags?
Re: Tables with object identifiers as the index :(
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.
- slime
- Solid Snayke
- Posts: 3170
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Tables with object identifiers as the index :(
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.)
Who is online
Users browsing this forum: Bing [Bot] and 10 guests