Rythian2277's way of implementing OOP into games

How I optimise my Class based implementation

Why I've made this

To keep it short, lots of the developers I work with have asked that I make an explanation of how the class system I use works and how I use it so this exists for them and so I don’t have to repeat myself over and over.

Prerequisite Knowledge:

  • How ModuleScripts work,
  • How the self argument works,
  • Object Oriented Programming.

Introduction

Object Oriented programming is an essential element to any job in the Programming side of Computer Science. As such, I collated how others were implementing their OOP until I found the optimal module for what I needed at the time, which has now become essential in my workflow.

How it works

This class implementation uses an Open Source module to simplify the construction of classes in your own frameworks.

The Basic Module Class Code
local Class = require(3696101309)

local Object = Class() do
    function Object:init(...) -- The function ':init()' is called automatically when your object is instantiated.
        -- Define any values here which you want in your custom object!
    end

    function Object:foo(bar: String) -- Like all other functions, type checking is supported.
        print(bar)
    end
end

return Object
How it's called on the Server
local Object = require(OBJECT_MODULE_SCRIPT)
local Object_Instance = Object(...) -- You instantiate the class in a similar way to JavaScript, by calling it as a function. 
Object_Instance:foo("Hello World") -- Prints "Hello World"

Simple right?

This system is implementable into most frameworks as it does not require installation of additional class modules for you to create a new class, simply require a fully open source module and you have your class!

1 Like

For those of you who wish to directly see the source code without taking a copy of the asset, I have added the class implementation module to a GitHub Repository.