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
A Confusing OOP Problem
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: A Confusing OOP Problem
I believe the issue is in PlayState.lua, you have the following:
when you probably want
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
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
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."
"If each mistake being made is a new one, then progress is being made."
Re: A Confusing OOP Problem
I tried your solution, but it returns this error message:marclurr wrote: ↑Tue May 17, 2022 2:49 pm I believe the issue is in PlayState.lua, you have the following:when you probably wantCode: 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
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
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
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
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.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests