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.
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.
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!
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.
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?
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!
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!
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).