Page 1 of 2

Problems when using world callbacks[Solved]

Posted: Fri Jan 07, 2022 9:14 pm
by NoreoAlles
Hello, for some reason i get the error message "Error player.lua:93: attempt to index local 'coll' (a nil value)'" when this code gets used :

Code: Select all

function Player:beginContact(a, b, coll)
    if self.grounded == true then return end
    local mx,my = coll:getNormal()
    if b == self.physics.fixture then 
        if my > 0 then
            self:Land()
        end
    elseif b == self.fixture then 
        if my < 0 then
            self:Land()
        end
    end
end

Re: Problems when using world callbacks

Posted: Fri Jan 07, 2022 10:55 pm
by pgimeno
Can you post your World:setCallbacks call?

Re: Problems when using world callbacks

Posted: Sat Jan 08, 2022 12:28 am
by grump

Code: Select all

-- not correct
world:setCallbacks(player.beginContact)

-- correct
world:setCallbacks(function(a, b, c) player:beginContact(a, b, c) end)

Re: Problems when using world callbacks

Posted: Sat Jan 08, 2022 1:22 am
by pgimeno
Except for the colon after 'world' ;)

Re: Problems when using world callbacks

Posted: Sat Jan 08, 2022 1:53 am
by grump
Oops, fixed.

Re: Problems when using world callbacks

Posted: Sat Jan 08, 2022 10:13 am
by NoreoAlles
pgimeno wrote: Fri Jan 07, 2022 10:55 pm Can you post your World:setCallbacks call?
In main.lua

Code: Select all

function love.load()
    World = love.physics.newWorld(0, 0)
    World:setCallbacks(beginContact, endContact)
    ...
    end

function beginContact(a, b, coll)
    Player:beginContact()
end

function endContact(a, b, coll)
    Player:endContact()
end

Re: Problems when using world callbacks

Posted: Sat Jan 08, 2022 10:17 am
by NoreoAlles
grump wrote: Sat Jan 08, 2022 12:28 am

Code: Select all

-- not correct
world:setCallbacks(player.beginContact)

-- correct
world:setCallbacks(function(a, b, c) player:beginContact(a, b, c) end)
Tysm! It atleast doesnt crash anymore, now i can acctually implement functioning gravity.

Re: Problems when using world callbacks

Posted: Sat Jan 08, 2022 10:18 am
by pgimeno
NoreoAlles wrote: Sat Jan 08, 2022 10:13 am

Code: Select all

function beginContact(a, b, coll)
    Player:beginContact()
end
If you don't pass the parameters to the player, you can't expect them to be received by your method, and it will receive nil values instead.

Try:

Code: Select all

function beginContact(a, b, coll)
    Player:beginContact(a, b, coll)
end
Same for endContact obviously.

Something suspicious is a possible confusion between the player class and the player instance. But that's an entirely different thing.

Re: Problems when using world callbacks

Posted: Sat Jan 08, 2022 10:55 am
by NoreoAlles
Something suspicious is a possible confusion between the player class and the player instance. But that's an entirely different thing.
So, i did some testing and found out that when the player is on the ground from frame 1, that the script does work and y Vel is 0. That means it simply isnt updated (correct me if im wrong, but everything gets executed once when starting, right?) Buuuut, you`re probably right with your susspicion. Everytime i try to put the beginContact function into any function that ends up in love.update, i get an index error. I do not know how to fix this.

Re: Problems when using world callbacks[Solved]

Posted: Sat Jan 08, 2022 10:56 am
by pgimeno
You don't know which of a or b is going to be the player and which is going to be the platform. You need to check both, but you only check b.

Also, which one is the fixture? self.fixture or self.physics.fixture?