Class

An optional utility to create OOP-style classes in Luau

-- ! Please note that this is just an example. This Signal class SHOULD NOT be used

local Quebec = require(QuebecMainModule)

-- We are creating a class called "Signal" now, to emit and receive events

type Signal = {
    name: string,
    callback: ((...any) -> unknown)?,
}

local Signal = Quebec.util.class(
    -- This is the constructor. It sets the class fields
    function(self: Signal, name: string)
        self.name = name
    end,
    -- This is the prototype. The methods that a Signal class has
    {
        fire = function(self: Signal, ...)
            if self.callback then
                local args = { ... }
            
                task.spawn(function()
                    self.callback(table.unpack(args))
                end)
            end
        end,
        connect = function(self: Signal, cb: (...any) -> unknown)
            self.callback = cb
        end
    }
)

-- Now you can use it like this:

local finishedSignal = Signal.new("finished")

finishedSignal:connect(function()
    print("called")
end)

finishedSignal:fire()

Function Signature

Quebec.util.class(constructor, prototype)

constructor is a function that is tied to the class.new function later on. It is used to set fields in the class.

prototype is a table of methods that the class has.

Last updated