Creating classes/modules
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 3
- Joined: Fri Dec 02, 2016 11:28 pm
Creating classes/modules
How would I create classes and modules? I've been using tables in the main file to use for classes but there has to be a better way (requiring classes in the folder?)
Re: Creating classes/modules
Lua only has tables so no, you will always wind up using tables. Having enough dedication, you can do it via closures but you really shouldn't.
Lua doesn't have classes so regardless of the way you do it, it's fine - as long as you don't do anything absurd, of course. Conventional way is to create a table that has a number of functions attached to it, which represents class, and to create an instance of the class you create a new table and assign class table as its metatable, i.e.
It's a common practice to put that in a "new" method of a class and then return the instance from it.
As for modules, since certain Lua version the way you do it is by defining a (local) master table in a file, and at the end of the file returning that table. All functions, variables and constants go into that table. You can, of course, use locals there.
Then you can require that file as a module.
Lua doesn't have classes so regardless of the way you do it, it's fine - as long as you don't do anything absurd, of course. Conventional way is to create a table that has a number of functions attached to it, which represents class, and to create an instance of the class you create a new table and assign class table as its metatable, i.e.
Code: Select all
local instance = setmetatable ( { }, Class )
As for modules, since certain Lua version the way you do it is by defining a (local) master table in a file, and at the end of the file returning that table. All functions, variables and constants go into that table. You can, of course, use locals there.
Code: Select all
local module = { }
function module.foo ( )
return bar
end
return module
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Creating classes/modules
For libs/modules, this is a neat read in my opinion: http://kiki.to/blog/2014/03/30/a-guide- ... a-modules/
But basically, what raidho said above.
But basically, what raidho said above.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: Creating classes/modules
If you dont want to use libraries, here's 2 ways of doing it.
Or
Code: Select all
local Class = {}
Class.x = 0
Class.y = 0
function Class.new(x, y)
local object = {
x = x,
y = y,
}
return setmetatable(object, {
__index = Class
})
end
function Class:getPosition()
return self.x, self.y
end
return setmetatable(Class, {
__call = function(_, ...) return new(...) end
})
Code: Select all
local function getPosition(self)
return self.x, self.y
end
local function new(x, y)
return {
x = x or 0,
y = y or 0,
getPosition = getPosition
}
end
return setmetatable({
new = new,
getPosition = getPosition
}, {
__call = function(_, ...) return new(...) end
})
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests