(OOP) Class Module

So I’ve been using OOP on Roblox a bit more lately, and I wanted to get a better understanding of it. And so I thought: What better way to learn about OOP than to (almost) remake it yourself?

So I did…

It’s very similar to Python’s Class system(with minor alterations since Lua…)

Here’s an example of it in action:

local Class = require(game:GetService("ReplicatedStorage").Class)

local Vehicle = Class{
	__init__ = function(self, category, speed)
		self.category = category
		self.speed = speed
	end,
	
	PrintSpeed = function(self)
		print("Your speed is: "..self.speed.." mph")
	end
}

local Car = Class(Vehicle){
	__init__ = function(self, speed)
		self.__super__(self, "Car", speed)
	end,
	
	Honk = function(self)
		print("Beep Beep!")
	end
}

local Aircraft = Class(Vehicle) {
	__init__ = function(self, speed, altitude)
		self.__super__(self, "Airplane", speed)
		self.altitude = altitude
	end
}

local Lambo = Car.new(220)
local Lockheed = Aircraft.new(2200)

Lambo:Honk() --> Beep Beep!
Lockheed:PrintSpeed() --> Your speed is: 2200 mph

If you have any suggestions or encounter any problems, feel free to post here or PM me on the DevForum.

27 Likes

I’m thinking of using this module, is there a better module/system for classes?

1 Like

Yeah, there are other OOP implementations methods/simplifications/templates on roblox, @Kironte has also made an OOP template with documentation (Although incomplete) where you don’t have to touch meta tables at all.

(proof: I made a community resource project without touching the meta tables and metamethods although I’m not really sure I did it correctly)

https://devforum.roblox.com/t/advanced-oop-implementation/741988

Edit: Yeah, unless this resource has more documentation and a easy to use template format to OOP, I’ll recommend the other one made by Kironte. Otherwise,

Nice work on your side project! I personally can’t understand metatables and don’t want to spend the time to learn them until I need to work with them to obtain more functionality. Maybe we should open source on GitHub an easy to use OOP implementation template together so we can make it simpler for everyone?

5 Likes

OOP is Short form for Object Oriented Programming. You can do cool stuff with it:

Read more about it online

Hello! I really like your OOP implementation, but I’ve got an issue, when I try creating a Class inheriting from another Class, this error pops up :
'new' is a restricted keyword.
Here’s my scripts

-- Weapon module
local Class = require(script.Parent:WaitForChild("Class"))

return Class{
	__init__ = function(self,viewmodel: Model)
		self.viewmodel = viewmodel
		self.animations = {}
	end,
}
-- Firearm module
local Weapon = require(script.Parent:WaitForChild("Weapon"))
local Class = require(script.Parent:WaitForChild("Class"))

return Class(Weapon) {
	__init__ = function(self,viewmodel: Model)
		self.__super__(self,viewmodel)
		self.ammo = 0
		self.currentAmmo = self.ammo
	end,
}

As you can see, there’s no ‘new’ keyword.