A Confusing OOP Problem

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.
Post Reply
Emren
Prole
Posts: 3
Joined: Mon May 16, 2022 7:58 pm

A Confusing OOP Problem

Post 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
User avatar
marclurr
Party member
Posts: 101
Joined: Fri Apr 22, 2022 9:25 am

Re: A Confusing OOP Problem

Post 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
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: A Confusing OOP Problem

Post 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).
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Emren
Prole
Posts: 3
Joined: Mon May 16, 2022 7:58 pm

Re: A Confusing OOP Problem

Post 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'
User avatar
marclurr
Party member
Posts: 101
Joined: Fri Apr 22, 2022 9:25 am

Re: A Confusing OOP Problem

Post 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.
Emren
Prole
Posts: 3
Joined: Mon May 16, 2022 7:58 pm

Re: A Confusing OOP Problem

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

Who is online

Users browsing this forum: No registered users and 41 guests