good OOP libraries?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
HappyYoyo09
Prole
Posts: 5
Joined: Tue Nov 26, 2024 8:38 pm

good OOP libraries?

Post by HappyYoyo09 »

I'm not really sure what OOP implementation to use, I'm coming from the playdateSDK, which has its own built in library, so something similar to that would be ideal. my main wants would be a way to avoid having to pass in self every time by using some sort of shorthand, as well as inheritance support.
MrFariator
Party member
Posts: 559
Joined: Wed Oct 05, 2016 11:53 am

Re: good OOP libraries?

Post by MrFariator »

Perhaps middleclass might suit your needs. It also has a companion library, stateful, for implementing state machines. For other implementations, check awesome-love2d repo and take your pick. This is the second time I've linked you the repo, I believe.

As for the self bit, the only thing you'll have is the colon syntax (":"), but it's just syntactic sugar:

Code: Select all

-- this...
function myClass:myFunction() 
  self.x = self.x+1 -- 'self' is inferred due to the colon syntax
end
-- ...is the same as..
function myClass.myFunction ( self ) 
  self.x = self.x+1 -- 'self' could be named anything here, really
end

-- and this...
myObject:containedFunction()
-- ...is the same as...
myObject.containedFunction(myObject)
You can mix-and-match using colon syntax and dot syntax, but you need to make sure that the 'self' points to the correct thing.
You could in theory make an OOP library that makes any object only work with the dot syntax without needing to pass the object itself as a parameter, but don't think it's worth the hassle. At that point, you'd be working against the more idiomatic lua conventions.
HappyYoyo09
Prole
Posts: 5
Joined: Tue Nov 26, 2024 8:38 pm

Re: good OOP libraries?

Post by HappyYoyo09 »

thanks, this looks like it functions almost exactly like the playdateSDK OOP library, which also uses : syntax. that said, this is the first library I've attempted to use in lua, and I'm not quite sure how it works, I'm using the example they gave under the "Quick Look" heading, saved to the file "main.lua", and it gives the following error, both in love2D and regular lua

Code: Select all

rror: main.lua:1: module 'middleclass' not found:
        no field package.preload['middleclass']
        no 'middleclass' in LOVE game directories.
        no file 'middleclass' in LOVE paths.
        no file '/tmp/.mount_love2DeRoYY6/share/luajit-2.1.0-beta3/middleclass.lua'
        no file '/tmp/.mount_love2DeRoYY6/share/lua/5.1/middleclass.lua'
        no file './middleclass.lua'
        no file '/usr/share/luajit-2.1/middleclass.lua'
        no file '/usr/local/share/lua/5.1/middleclass.lua'
        no file '/usr/local/share/lua/5.1/middleclass/init.lua'
        no file '/usr/share/lua/5.1/middleclass.lua'
        no file '/usr/share/lua/5.1/middleclass/init.lua'
        no file '/tmp/.mount_love2DeRoYY6/lib/lua/5.1/middleclass.so'
        no file './middleclass.so'
        no file '/usr/local/lib/lua/5.1/middleclass.so'
        no file '/usr/lib/lua/5.1/middleclass.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [love "boot.lua"]:352: in function <[love "boot.lua"]:348>
        [C]: in function 'require'
        main.lua:1: in main chunk
        [C]: in function 'require'
        [love "boot.lua"]:323: in function <[love "boot.lua"]:126>
        [C]: in function 'xpcall'
        [love "boot.lua"]:362: in function <[love "boot.lua"]:355>
        [C]: in function 'xpcall'
from what I can tell, this means that It can't find the middleclass.lua file, even though its right next to the main.lua file.
HappyYoyo09
Prole
Posts: 5
Joined: Tue Nov 26, 2024 8:38 pm

Re: good OOP libraries?

Post by HappyYoyo09 »

never mind, I managed to fix it, turns out I had misspelled middleclass at some point when I was moving it to the project directory.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests