Tables with object identifiers as the index :(
Posted: 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!
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!