Creating classes/modules
Posted: Fri Dec 02, 2016 11:30 pm
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?)
Code: Select all
local instance = setmetatable ( { }, Class )
Code: Select all
local module = { }
function module.foo ( )
return bar
end
return module
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
})