1

I'm trying to run code that spawn 2 squares affected by gravity. When they hit each other, they just get destroyed using setuserdata and getuserdata. When I try to getuserdata, I get a nil value. What I'm doing here is getting values for each square to create it. First square gets userdata square1 and the other square get userdata square2. When the code detects a collision, they just get destroyed by adding them to destroyedbodies. I remove them one by one from the table and from the square table.

Here is my create square code:

Test = Class{}

function Test:init(type, x, y, userData, world)

self.world = world

self.x = x 
self.y = y 

self.type = type

self.boxBody1 = love.physics.newBody(self.world, self.x , self.y ,'dynamic')

if self.type == 'square' then
    self.boxShape = love.physics.newRectangleShape(10, 10)
end

self.boxFixture1 = love.physics.newFixture(self.boxBody1, self.boxShape)
self.boxFixture1:setRestitution(0.5)

self.boxFixture1:setUserData(userData)
end

function Test:update(dt)
end

function Test:render()

love.graphics.polygon('fill', self.boxBody1:getWorldPoints(self.boxShape:getPoints()))
end

and here is my destroy code

Test2 = Class{}

function Test2:init()
self.world = love.physics.newWorld(0, 300)

self.destroyedBodies = {}

self.square = {}

function beginContact(a, b, coll)
    local types = {}
    types[a:getUserData()] = true
    types[b:getUserData()] = true

    -- if we collided between both the player and an obstacle...
    if types['square1'] and types['square2'] then

        -- grab the boxBody1 that belongs to the player
        local square1 = a:getUserData() == 'square1' and a or b
        local square2 = a:getUserData() == 'square2' and a or b
        
        table.insert(self.destroyedBodies, square1:getBody())
        table.insert(self.destroyedBodies, square2:getBody())
    end
end

self.world:setCallbacks(beginContact)

table.insert(self.square, Test('square', VIRTUAL_WIDTH/2, VIRTUAL_HEIGHT/2, 'square1', 
self.world))
table.insert(self.square, Test('square', VIRTUAL_WIDTH/2, VIRTUAL_HEIGHT/3, 'square2', 
self.world))

self.groundBody = love.physics.newBody(self.world, 0, VIRTUAL_HEIGHT - 30, 'static')
self.edgeShape = love.physics.newEdgeShape(0, 0, VIRTUAL_WIDTH, 0)
self.groundFixture = love.physics.newFixture(self.groundBody, self.edgeShape)
end

function Test2:update(dt)
self.world:update(dt)

for k, boxBody1 in pairs(self.destroyedBodies) do
    if not boxBody1:isDestroyed() then 
        boxBody1:destroy()
    end
end

self.destroyedBodies = {}

for i = #self.square, 1, -1 do
    if self.square[i].boxBody1:isDestroyed() then
        table.remove(self.square, i)
    end
end
end

function Test2:render()
for k, square in pairs(self.square) do
    square:render()
end

love.graphics.setColor(255, 0, 0)
love.graphics.setLineWidth(3)
love.graphics.line(self.groundBody:getWorldPoints(self.edgeShape:getPoints())) 
end

the error I'm getting is

Error

src/Test2.lua:12: table index is nil


Traceback

[love "callbacks.lua"]:228: in function 'handler'
src/Test2.lua:12: in function <src/Test2.lua:10>
[C]: in function 'update'
src/Test2.lua:50: in function 'update'
main.lua:51: in function 'update'
[love "callbacks.lua"]:162: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'
1
  • 1
    Maybe this is because the groundFixture doesn't have any userdata. Commented Mar 23, 2024 at 6:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.