Concepts: Object Oriented Programming in Roblox

Static class methods don’t have a this pointer that refers to the object because it’s a class level method. Though, you are right about methods existing only for the class to reduce memory, your terminology and use case of static is incorrect.

In Lua, the __index metamethod allows us to define methods on the class itself instead of each object, but it would not be considered static because the methods are invoked through the object itself. Apart from that, I do agree, it’s a very important practice to reduce the overall memory of each object.

Yeah I meant this is passed in for member methods, not static methods.

Oh yeah I did get confused sorry about that, I must of gotten mixed beweenbetween semi colons and colons or something like that.

2 Likes

I know this was from a while ago, but I’m sure people scrolling by will have the same question. Well, to answer this, let’s say you want to make Semi-Auto guns and Auto guns. Here’s how you could apply OOP:

You would have a base class called Object.

Object can have a property called Name.

You could have Gun as a subclass of Object, and then you could have SemiGun and AutoGun as subclasses of Gun.

Gun would have a method called Reload(), and a property called Ammo.

AutoGun would have a method called StartFiring(), and another one called StopFiring().

SemiGun would have a method called Fire().

In this case, you would be taking advantage of Inheritance, mostly. In situations like this where two classes (SemiGun and AutoGun) are going to need the same function (Reload()), you can use inheritance and avoid copy-pasting code, following D.R.Y to a greater extent.

3 Likes

Really good tutorial. This helped me learn that metatables aren’t hard at all compared to normal table. Only thing I’m unsure about is creating new properties within instances since their metatables are locked. I’m going to read up about that though. Really good tutorial, I really love it and the information it provides. Thank you OP!

3 Likes

The way you put it in simple terms helps to understand, thank you a lot for this document!

1 Like

Good tutorial, but I noticed a little issue. Roblox isn’t exactly the perfect language for OOP, so clean looking code is most frequently not very efficient. In this particular case, I’m talking about method declaration. The main issue being that every time you declare a new car object, you also declare duplicate methods.

A much better solution would be to declare the methods before assigning them to the objects. It might look ugly, but that’s just what Lua development is like.

local Car do
	local carDrive = function(self)
		self.isDriving = true
	end
	local carStop = function(self)
		self.Speed = 0
		self.isDriving = false
	end
	local carSetSpeed = function(self,speed)
		if self.isDriving then
			self.Speed = speed
		end
	end
	
	Car = {
		new = function()
			return {
				Model = "EpicModel",
				Color = "Blue",
				Speed = 0,
				
				Drive = carDrive,
				Stop = carStop,
				SetSpeed = carSetSpeed,
			}
		end
	}
end

return Car

Same goes for metatables. Although you avoided the issue by using Car as your metatable, you can also declare it beforehand with the methods.

If I understood your message correctly, the methods aren’t actually being duplicated, when you create a new car object, since the Car table is also the metatable of the car object and the index metamethod points to the Car table, when you attempt to call a function on the car object it will retrieve the function from the Car table. Also each time you require the Car module it will return the same table in memory.

2 Likes

Great tutorial, might be a bit late on the concept, but this is just like math, it never changes, or never will(as long as no one debunks it)

So I am making an system that uses OOP, but every time I run the code I get an error. It keeps saying attempting to index with nil. My code is below.

local UpdateLog = require(game.ReplicatedStorage.ModuleScripts.GameVersion.UpdateLog)
local updatelog = UpdateLog.new()
updatelog:setSize(0, 20, 0, 20)

It’s the third line that gives me the error.

Hey there, could I see your module so I can help you. I don’t know if it’s best to do it in here so feel free to send me a Devforum message.

1 Like

Great tutorial, thanks for writing it! While I’m fairly confident in my ability to write OOP now (using this thread as a reference), I feel like it would help to see some code examples. Can we still expect a use cases thread in the future?

2 Likes

I haven’t been able to access my computer and won’t be able to for a few weeks. If people really want a usecases thread I can definitely get to working on it more. Also thank you for your response and I’m glad this helped you!

3 Likes

Cheers for the guide, it’s been a great help in understanding OOP

From my current understanding, objects are simply just tables that exist on either the server or client.

However how would I go about using OOP efficiently across both the client and the server for large game projects that rely almost entirely on OOP?

3 Likes

Amazing guide, should be pinned by roblox somehow

2 Likes

UPDATE V3

This tutorial has undergone a complete rewrite. I aimed to explain things in greater depth and cut out some of the unnecessary stuff. If you see any issues with the new version please feel free to let me know!

11 Likes

You made a typo. In the function it should return self.Color instead

1 Like

Thanks for this concept guide I learned alot for it :grin:

1 Like

Sorry if I have misread, but can metatables be removed?

As in removing no longer needed tables and any reference of them to save memory.

To remove a metatable, simply just use nil for the second argument of setmetatable. For example, setmetatable(your_table, nil). This only works if the __metatable key is not set in the current metatable (it is protected).