ClassCreator
The tool for creating OOP classes easily and 3 times more efficiently.
Hello! I am receipes.
Over the last year in programming in lua I have learnt lots of things about the language. Today, I am very excited to announce that today I am releasing a utility that I have been developping the last few weeks. I find it very useful and that's why i'm showing it to you guys today. Let's start!I will assume that you understand the basics of OOP and the terminology. If not then here are the VERY basics:
Class: Starting point.
Extended class: The class that the new one is being based on.
SuperClass: Used as a reference to access the extended class methods.
Method: A function.
First, let me show you how it works.
Assume we have a base class titled: Mod with 3 methods (functions) and 1 variable. The functions being: OnEnable, OnDisable, SetEnabled and the variable being "Enabled". Without ClassCreator it would look like so:local static = {}
local class = {}
function static.new()
local self = setmetatable({}, {__index=class})
self.Enabled = false
return self
end
function class:OnEnable()
print("Mod has been enabled!")
end
function class:OnDisable()
print("Mod has been disabled")
end
function class:SetEnabled(value)
self.Enabled = value
if value then
self:OnEnable()
else
self:OnDisable()
end
end
Of course, this example is very simple but you get the idea.
Anyways, here’s how you would do it with ClassCreator.
return require(ClassCreator)({ -- Replace ClassCreator with the location of the module.
Init = function(self)
self.Enabled = false
end,
Methods = {
OnEnable = function(self)
print("Mod has been enabled!")
end,
OnDisable = function(self)
print("Mod has been disabled!")
end,
SetEnabled = function(self, value)
self.Enabled = value
if value then
self:OnEnable()
else
self:OnDisable()
end
end
}
})
As you can see, it is way easier to understand the logic behind it and it is shorter.
You can also make a class that extends another one pretty easily. Example we take the mod above and use it as our super class of our new mod titled: TestMod
local ModBase = require(ModBase)
return require(ClassCreator)({
-- We no longer need anything in our init function in this example simply because the super class will already take care of everything for us. Of course, if you wanted to declare another variable which is only in TestMod you would declare it in there.
ExtendFunction = function(self, ...) -- The same arguments are passed as in the init function. We have the self to access the class and the arguments of the .new when creating a new instance of the class.
return ModBase.new() -- Here you would return a super class instance by calling the new function. It also works with other OOP programs that aren't made with ClassCreator
end,
Methods = { -- You can override the functions over here or add new ones specific to this class.
setEnabled = function(self, v) -- In this example, we are overriding the setEnabled function and calling the function of the superclass. This is basically a logger that prints to the console the new value everytime.
print("setting enabled to "..tostring(v))
self.super:setEnabled(v)
end
}
})
There are different ways to create methods here are the ones that work:
- Only a function.
set = function(self, v)
self.X = v
end
- A table
set = {
Function = function(self, v)
self.X = v
end
Static = false -- This parameter is optional it will default to false.
}
ClassCreator.lua (959 Bytes)