the self
keyword has a realtionship with OOP Object Oriented Programming which you don’t really need to know to understand self
but it is something you should really learn about!
The first part of this article made by Egomoose aswell explains a bit about OOP! link
I also have an article that I made but it’s really just a mess and has a LOT of mistakes link .
So, let’s start off, lua’s syntax allows you to turn tables into a some sort of object. We can put functions into tables.
local tab = {something = function(par) print(par) end} --"something" is the name of the function btw
--And if you wanna call that function that's stored inside of the table you would need to do
tab.something(2)
But lua’s syntax allows you to make functions inside of tables in another way!
local tab = {}
function tab.something(par)
print(par)
end
--this is littearly the same thing as above! but written out differently
tab.something(2) --calling it the same way
Now we’ll get back to this way of defining functions inside of tables later. If you think about it, there are two notations for functions, there are functions written out with a dot .
and ones with the double dots :
like methods. Something like part:Destroy()
uses :
, and there is something like math.floor()
which uses .
.
If we get back to the way we assigned the functions above. We can see we can actually do this as well, we can chose either a :
function or a .
function.
local tab = {}
function tab.something(par)
print(par)
end
tab.something(2)
--or this way!
function tab:something(par)
print(par)
end
tab:something(2) --very interesting isn't it?
Now, this is the exciting part, the part that has a relation with self
.
Let’s say we have a table, that has a color property inside of it, (beacuse remember! tables kind of act like objects, like parts and other things, this has a relationship with oop). And this table would have a method, or in other words just a function that will change the color (a method is really just a function, but there are some minor differences). Let’s write that method in the 2 different ways above.
local object = {color = "blue"}
print(object.color) --prints blue, and you can see it kind of looks like an actual property, like part.BrickColor the one you're used to
function object.ChangeColor(object, color)
object.Color = color
end
--You can see if we type it out this way! We have to specify the object
--That we wanna change its color in the paramaters.
function object:ChangeColor(color)
self.Color = color
end
--You can see the usage of self pops up, and as well you can notice that we
--don't specify the table that we wanna change its color. Interesting!
Very interesting! Then what does self
refer to? Well it refers to the object (table) that we are using the method on! The methods with the :
notation kind of apply DIRECTLY on the object, while the ones with the .
need the object affected to specified in the paramaters.
local object = {color = "blue"}
print(object.color) --prints blue, and you can see it kind of looks like an actual property, like part.BrickColor the one you're used to
function object.ChangeColor(obj, color)
obj.Color = color
end
object.ChangeColor(object, "pink") --we have to specify the object!
function object:ChangeColor(color)
self.Color = color
end
object:ChangeColor("pink") --In here self refers to "object"! It refers to the object that we are using the method on