So you’ve come to your senses. You want to be like the pros and join the Object-Oriented Programming bros(oop).
Well, what is oop? Oop uses Objects and makes them unique. Such as your red part in the workspace. It has its unique properties such as BrickColor, Name, Anchored, etc. That is an example of an object but you’ll be making your own. Don’t get your hopes up were not creating instances were creating other things such as tables.
When should you use oop? I use oop mainly in module scripts. Why, because I’m gonna be using that module script over and over again. Let’s call it a system. If you’re going to be using your system once and won’t need it again, you’d be better off putting it in a server script with the methods on top and code at the bottom. What are methods? Methods are a fancy name for a function.
I expect you to decent at functions and have a well understanding of tables before continuing.
Now let’s talk about metatables. Metatables are tables but with functions. An improved table. Here are some good links to explain meta tables since they’re very detailed and I can’t explain them all in one post.
Come back to me once you got some knowledge about metatables.
Ah, so you’re back. I hope you got some coffee and some food because this is going to be a long one.
I hope what you learned from those articles is that: Metatables are tables with extra functions such as __index and __newindex. I hope you also learned about inheritance. Of course, they’re other functions but those are your main ones. In our case, we’re gonna be using __index.
Inheritance is when you set a metatable and that table gets all the functions of the meta table/table.
If you didn’t get a good understanding of __index here’s what it does.
__index fires when you index something in your main table and it doesn’t find it. It then checks if you have a metatable and if that item is inside the meta table.
Well, how do you give tables a metatable? Simple it is.
local Table = {1,2,"This is your table"}
local MetaTable = {3}
setmetatable(Table,MetaTable)
Now, what if index something that isn’t in our table?
print(Table[3])
Well, 3 isn’t in our main table so theoretically, it would error, and since we don’t have a __index function setup for our meta table it doesn’t check the meta table.
Let’s add a __index function to our meta table.
MetaTable.__index = MetaTable -- We set the table to our meta table
--So when something in our main table gets indexed it checks the metatable
So now it should check our MetaTable and print 3
Good job on getting this far. If you don’t understand something feels free to ask in the comments and someone or I will help you.
Alright, time to get to work on that car system.
Firstly let’s create a module script inside on ReplicatedStorage called ‘Car system’
Let’s change the code to something like this
local CarSystem = {}
return CarSystem
Alright, so let’s set our __index. Do this every time you’re making a system unless you need something specific like __newindex or anything else.
CarSystem.__index = CarSystem
I’m a fan of pascal so feel free to change it.
Alright, let’s now make our first Method. If you don’t remember Methods are a fancy name for functions.
function CarSystem.New(Color,Speed)
local NewCar = {}
setmetatable(NewCar,CarSystem) -- The new car will get all the functions
--Of car system
NewCar.Color = Color
NewCar.IsDriving = false
NewCar.Speed = Speed
return NewCar
end
Now we have a car, good job.
We can now print things about the car such as its speed or Color
You may not know how to get the car so let’s go ahead and do it
Create a Server script inside of workspace/ServerScriptService
Inside of it require your car system
local CarSystem = require(path.to.CarSystem)
local MyCar = CarSystem.New('Red',30)
print(MyCar.Speed)
Example of code with what you can do with your car
Now let’s add more functions to the CarSystem so we can make it drive.
Before that, though let’s talk about the difference between : functions and . functions. Also, what does self
means.
A dot function is calling from the core. In our case, we’re calling from the car system. So we don’t have self.
If we’re calling from an object. In our case, the car we have self. Self would be the car because that’s the object that we called the function on. So we could do something like
print(self.Speed)
Of course only inside of a : function.
Back to the module script.
Let’s add a function called Drive inside of our CarSystem
function CarSystem:Drive()
self.IsDriving = true
end
This will make our variable ‘IsDriving’ true.
Well, there you go. We went over the basics of oop inside of luau. I hoped you all enjoyed it and that this was clear to read. If you have any suggestions please tell me. Now go make yourself some systems.