How do you handle classes?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

How do you handle classes?

Post by Vimm »

I've noticed that people seem to do classes differently, which I find weird cuz I never knew it could be done in different ways XD

I learned using classic.lua but apparently it isnt a great way to do it, so I wanna see how other people go about it!

lately ive been adding

Code: Select all

module(..., package.seeall)
to the top of my classes, which is something I saw in a youtube video, how do you guys do it?
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: How do you handle classes?

Post by Beelz »

Lua doesn't have a set in stone version of how classes should be... That's the beauty of it. If you find a way to do it that suits your needs and you are comfortable with, then by all means I would say to roll with it. I like to do things myself and keep it simple, so I prefer not to use libraries like classic, 30log, or middleclass. This is the entirety of my class.lua that I drop into my projects:

Code: Select all

local class = {}

function class:extend(subClass)
	return setmetatable(subClass or {}, {__index = self})
end

function class:new(...)
	local copy = setmetatable({}, {__index = self})
	return copy, copy:init(...)
end

function class:init(...) end

return class

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: How do you handle classes?

Post by Inny »

module(..., package.seeall) is discouraged. There's a huge and lengthy reason for it, but you don't need to know why right now unless you want (in which case you can start here: http://lua-users.org/wiki/LuaModuleFunctionCritiqued ). Also, the module function doesn't make classes, it makes modules.

As for classes, I highly recommend you use either kikito's middleclass or Yonaba's 30log. If you want to go your own way that's fine, but if you use these, then you can get a lot of help from people on this board (even from the library writers themselves). You own homebrewed class library would leave you to fend for yourself if you ran into problems.
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: How do you handle classes?

Post by Vimm »

Inny wrote:module(..., package.seeall) is discouraged. There's a huge and lengthy reason for it, but you don't need to know why right now unless you want (in which case you can start here: http://lua-users.org/wiki/LuaModuleFunctionCritiqued ). Also, the module function doesn't make classes, it makes modules.

As for classes, I highly recommend you use either kikito's middleclass or Yonaba's 30log. If you want to go your own way that's fine, but if you use these, then you can get a lot of help from people on this board (even from the library writers themselves). You own homebrewed class library would leave you to fend for yourself if you ran into problems.
Ohh I like Kikito's stuff, ill probably look at middleclass! I dont think I understand enough about lua to make my own class.lua lol
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: How do you handle classes?

Post by airstruck »

You might also consider using factory functions instead of classes if you don't need static class members. In other words, just write a function that creates, initializes, and returns a table, similar to what your class constructor would do (except create the table and return it instead of just initializing it). Inheritance can be achieved by writing factories that serve as decorators for tables created by other factories. This should produce very straightforward and understandable code, and won't require any special knowledge of Lua metatable stuff or any dependencies.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 91 guests