##Hello there, I am SomeoneYouKnow_Last. This tutorial will show you on how to implement OOP into your code.
First ever tutorial ever posted here, so feel free to correct me if I am wrong. I know a little bit of OOP and I want to be helpful, so I decided to create a tutorial on it.
OOP stand for Object-Oriented Programming, if you didn't know. OOP help to clean code and also keeping it organised.
Note : Don't overuse the OOP, if the code is simple you should know that it doesn't need a OOP to do that task specifically. Like this is a bad example :
function Module:Tween(Position)
-- Tweening the self's position to the 'Position'
-- Scroll down to see what is 'self' exactly
end
This is absolutely unnecesary, you should instead use a local function rather than use a ModuleScript. Hopefully you understand what I meant.
We first need to create a ModuleScript anywhere you want, and also create a ServerScript that will using the ModuleScript.
Lets get to the next chapter, shall we?
Going to the ModuleScript you will see this :
local module = {}
return module
Lets say that we're making a NPC creation stuff, we should change the boring 'module' to 'NPC' like so :
local NPC = {}
return NPC
Now, let make NPC module to have a function to create a NPC :
local NPC = {}
function NPC.new(Personality, Traits)
local NewNPC = {}
NewNPC.Personality = Personality
NewNPC.Traits = Traits
return setmetatable(NewNPC, NPC)
end
return NPC
Done! Some of you might point out that something look wrong, and you're right. What's wrong is that we don't have a metamethod ".__index", which I will implementing right now :
local NPC = {}
NPC.__index = NPC
function NPC.new(Personality, Traits, Name)
local NewNPC = {}
NewNPC.Personality = Personality
NewNPC.Traits = Traits
NewNPC.Name = Name
return setmetatable(NewNPC, NPC)
end
return NPC
What is metamethod "index" exactly? Well the metamethod "index" is like when you try to 'index' the returned metatable the 'index' but it doesn't exist, so the code will try to find a table that is connected to the returned metatable, and find if that table have an index similar to the 'index', if then fire that. Which work like that.
Correct me if I am wrong and also sorry for bad English. English is not my primary languenge.
Alright, this one is making the NPC module to have functionality instead of none. So here we go :
local NPC = {}
NPC.__index = NPC
function NPC.new(Personality, Traits)
local NewNPC = {}
NewNPC.Personality = Personality
NewNPC.Traits = Traits
return setmetatable(NewNPC, NPC)
end
function NPC:GoTo(Position)
-- Just pretend we have a pathfinder ModuleScript somewhere...
local Path = Pathfinder:GetPath(self, Position)
for _, Waypoint in Path do
-- Go to that part idk to lazy too write it
end
end
return NPC
Noticed something in that code? Well if you haven't figured by now, I used 'colon' instead of 'dot'.
-- this is colon -> :
-- this is dot -> .
Using 'colon' is different than using dot. 'colon' automatically put self there while 'dot' you need to do it manually.
function Module:Stuff()
-- Automatically have the variable 'self'
end
function Module.Stuff(self)
-- Has to manually put 'self' there
end
You ask what's 'self'? Well 'self' can be the ModuleScript itself while also being the returned metatable that we got from using the NPC.new(). 'self' is very useful if you want to a function that is in the ModuleScript but separated from it.
In simple term, 'self' is referring to the ModuleScript and returned metatable itself.
Okay so, lets say you already have like input detecting stuff in the background while I teaching you, you only need to add this to your code :
local Module = require(where_ModuleScript)
-- Where to add it
local NewNPC = Module.new("FUNNY", "can dance")
while true do
-- Don't just do a pure while true loop, add a task.wait() to it
NewNPC:GoTo(Vector3.new(42, 50, 14))
end
Why use OOP?
Well as I said above in earlier, OOP help to clean code and keeping it organised.
As this is the end, I hopefully teached you something useful about this. Also sorry if I made any English and grammar mistakes. Goodbye, and see you in probably 3 weeks