A Simple javascript-like luau class / Object Oriented Programming (OOP)

Basic Class

A simple luau class module. The basis of Object Oriented Programming

TL;DR: It is simple and almost identical to the javascript class. Just use it:

Why

There are a lot of coding structures in the world. But In many games, The most formal, and standard structure is CLASS. There was no built-in Basis implementation of class in Luau. But now, We got a light, simple, and extendable module. It is a valuable option for structuring scripts as well as possible.

Usage

local Class = require(path.to.module)
local CLASSNAME = Class:create("CLASSNAME") -- Create pure class
		
local CHILDCLASSNAME = CLASSNAME:inherit("CHILDCLASSNAME") -- Inherit class
		
function CLASSNAME:constructor(super, ...) super(var1, var2, ...) --[[call super constructor]] end -- Assign class constructor function
function CLASSNAME:destructor(super, ...) super(var1, var2, ...) --[[call super destructor]] end -- Assign class destructor function
		
function CLASSNAME:foo(...) end -- Add new function
		
locla obj = CLASSNAME(var1, var2, ...) -- Create object
obj:destroy() -- Destroy object
		
print(obj:__is(CLASSNAME)) -- Return object is given class or given the class' child's class
print(obj:__isStatic(CLASSNAME)) -- Return object is whether it's static or not
print(obj:__name(CLASSNAME)) -- Return object's name for programmer
print(obj:__tostring(CLASSNAME)) -- (A customizable implement function) Return object's status *USUALLY*

Source

GitHub Gist: BasicClass.lua · GitHub
Roblox: Basic Class - Roblox

I’m getting comments as well. Please comment on this topic if it has any problem.

Wait. I have a question.

6 Likes

I’m curious about what is the best method to connect Client-sided class and Server-sided class. Is there any way to do it? What if there is any, reply to me.

The module itself is nice but I’m just a bit worried that "Will it cause any performance issues?"

If not, I’ll probably think about it. And for your question, you may name the method something like :request or :response, something like this? And for how it’ll work, you may send the name of the class name or something like that and use a remote event yourself to get the request or something like that? (Please help, I’m really bad at js, I’m pretty sure I did not understand your question properly.)

But, this is a nice module.

First, I’ve confirmed that it won’t cause a performance issue. because it just indexes the function by metamethod and a class only costs memory addresses while in the table (except functions that it is your own risk).
And Second, yeah exactly. I’m considering the client-side class that performs :request, :response things.

(In addition, I’ve never used js. I just only referenced js. that’s all.)

Ah that’s great, I’ll look into the module a few mins later! (Creative idea btw)

Suggestion
Would you make things other than just class, for example arrays?

table.insert(myTable, 2)
-- but in this example
myTable.push(2)

How about this?

I have no concern to make basis array module. If you have another suggestion, I’ll look for it

I have two concerns with this module.

First, I’m concerning about how to make a remote connection between the server-side, and client-side classes, which the server side controls and the client side responds to the server’s request.

Second, I want to make type-checking like the following one

local Class = require(path.to.module)
local NewClass = Class:create("new")

function returnNewObj(): NewClass (or new)
    local newObj = NewClass()
    return newObj
end

As shown above, I want to make a type automatically assigned to the class variable or given class name.

If you have any answers to one of them, Please reply to my post.

Miner Bug Fixed. you can use it from here, BasicClass.lua · GitHub
or here,
Basic Class - Roblox

Miner Bug Fixed. changed

local newTbl = table.clone(tbl)
-> local newTbl = {}

for useless memory usage
you can use it from here,(BasicClass.lua · GitHub)
or here,
Basic Class - Roblox

1 Like