(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.

20 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)

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?

4 Likes

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

https://www.w3schools.com/java/java_oop.asp

Read more about it online