ClassCreator - The tool for creating classes as a beginner

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:

  1. Only a function.
set = function(self, v)
    self.X = v
end
  1. 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)

7 Likes

can you explain why it’s easier than doing this?

local static = {}
static.__index = static

function static.new()
    local self = setmetatable({}, static)
    
    self.Enabled = false
    
    return self
end

function static:SetEnabled(value)
    self.Enabled = value
    if value then
        print("Mod has been enabled!")
    else
        print("Mod has been disabled")
    end
end

return static

When I first started lua, I had a lot of trouble understanding metatables and the magic behind them.

I feel like to would be easier to just use meta tables on your own…

1 Like

This module is really useful for people who come from a Java background specifically as the syntax from that language is extremely similar to this. Though I believe it is more confusing if you have only ever used Lua

can’t you just read this???

imo roblox-ts already fixes that problem.

1 Like

Beginners who come from other programming languages might not understand right away this as they have never seen it. I do recommend reading them to understand better how they work and as soon as someone is ready they can stop using this module.

Some people (like me) simply don’t want the trouble of learning typescript just after learning Lua. I already had enough while learning javascript. So, I didn’t want to learn a variant of it. As someone else already said this module is useful for people who come from a Java background (like me), who don’t like the idea of lua’s Functional Programming and have a lot of trouble to learn metatables. I do not think we should not let people learn how to use them.

I think one of the most useful features of this module is the ability to create classes that extend other classes in a WAY easier manner than having to write the code for extending another class everytime.

I also forgot to mention that I was also going to start working on a plugin to convert some custom lua into a lua class but to do this, I had to do this module. Only with the module I think it can be done pretty well but I based myself around some community answers for the syntax of the custom lua here is an example:

class TestMod <- ModBase
    static function HelloWorld()
        return "Hello World"
    end

    function someFunction()
        print("cool")
    end

    function someOverridenFunc()
        print("called")
        self.super:someOverridenFunc()
    end
end

JavaScript/TypeScript and Java are two entirely different programming languages.

Java and javascript/typescript do classes almost the same way.

1 Like

ok, time to create roblox-js for javascript users

it’s a joke lol

2 Likes