Problems when using world callbacks[Solved]

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.
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Problems when using world callbacks[Solved]

Post 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
Last edited by NoreoAlles on Sat Jan 08, 2022 10:19 am, edited 1 time in total.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problems when using world callbacks

Post by pgimeno »

Can you post your World:setCallbacks call?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Problems when using world callbacks

Post by grump »

Code: Select all

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

-- correct
world:setCallbacks(function(a, b, c) player:beginContact(a, b, c) end)
Last edited by grump on Sat Jan 08, 2022 1:52 am, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problems when using world callbacks

Post by pgimeno »

Except for the colon after 'world' ;)
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Problems when using world callbacks

Post by grump »

Oops, fixed.
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Problems when using world callbacks

Post 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
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Problems when using world callbacks

Post 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.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problems when using world callbacks

Post 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.
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Problems when using world callbacks

Post 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.
Last edited by NoreoAlles on Sat Jan 08, 2022 10:56 am, edited 1 time in total.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problems when using world callbacks[Solved]

Post 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?
Post Reply

Who is online

Users browsing this forum: No registered users and 76 guests