Page 1 of 1

A Confusing OOP Problem

Posted: Tue May 17, 2022 5:16 am
by Emren
Hello guys, could you help me with a minor OOP related problem? My project returns "objects/Char.lua:89: attempt to index field 'physics' (a nil value)" error. Looks like "self.physics.fixture" in the constructor cannot be perceived by the "if a == self.physics.fixture then" line in "function Char:beginContact(a, b, collision)". Probably there is a minor, stupid mistake I can't detect.

https://github.com/EmrenBitirim/platformer_game

Re: A Confusing OOP Problem

Posted: Tue May 17, 2022 2:49 pm
by marclurr
I believe the issue is in PlayState.lua, you have the following:

Code: Select all

function beginContact(a, b, collision) -- 3rd argument: contact object created upon collision
    Char:beginContact(a, b, collision)
end

function endContact(a, b, collision)
    Char:endContact(a, b, collision)
end
when you probably want

Code: Select all

function beginContact(a, b, collision) -- 3rd argument: contact object created upon collision
    self.player:beginContact(a, b, collision)
end

function endContact(a, b, collision)
    self.player:endContact(a, b, collision)
end

Re: A Confusing OOP Problem

Posted: Tue May 17, 2022 3:18 pm
by ReFreezed
Like marclurr is kind of pointing out, Char (which is a class) should really be an instance of the class. But since beginContact() is called for collisions between any two fixtures you have to determine what objects in your game are actually colliding (which you're kind of doing in the Char:beginContact() function, but it needs to happen directly in beginContact() so you can know what 'Char' should be replaced with).

Re: A Confusing OOP Problem

Posted: Tue May 17, 2022 4:10 pm
by Emren
marclurr wrote: Tue May 17, 2022 2:49 pm I believe the issue is in PlayState.lua, you have the following:

Code: Select all

function beginContact(a, b, collision) -- 3rd argument: contact object created upon collision
    Char:beginContact(a, b, collision)
end

function endContact(a, b, collision)
    Char:endContact(a, b, collision)
end
when you probably want

Code: Select all

function beginContact(a, b, collision) -- 3rd argument: contact object created upon collision
    self.player:beginContact(a, b, collision)
end

function endContact(a, b, collision)
    self.player:endContact(a, b, collision)
end
I tried your solution, but it returns this error message:

Code: Select all

states/PlayState.lua:41: attempt to index global 'self' (a nil value)


Traceback

[love "callbacks.lua"]:228: in function 'handler'
states/PlayState.lua:41: in function <states/PlayState.lua:40>
[C]: in function 'update'
states/PlayState.lua:25: in function 'update'
StateMachine.lua:23: in function 'update'
main.lua:38: in function 'update'
[love "callbacks.lua"]:162: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'

Re: A Confusing OOP Problem

Posted: Wed May 18, 2022 7:25 am
by marclurr
Apologies I misread beginContact and endContact as being scoped to PlayState. As your player object is a member of PlayState you'll have to find a way to make that available outside of that object (the simplest, dirtiest way would be a global variable instead). That said as ReFreezed points out, those functions will be called for every collision body in the world, not just the player, so those functions will need to be a bit more sophisticated.

Re: A Confusing OOP Problem

Posted: Wed May 18, 2022 9:24 am
by Emren
I solved the problem by using a more Luaesque approach to creating the player object. Instead of using the class library, I created a table for the player object and imported it to playState.lua. Since I want the game to have different states, I need to stick to the OOP approach in some cases, but for game objects I'll try using tables. Thanks for the answer.