I keep seeing people use self
, and Im very confused on why and how self works.
And what instances should I use self in?
I keep seeing people use self
, and Im very confused on why and how self works.
And what instances should I use self in?
There are A LOT of this same exact question and A LOT of videos for it, although here is a simple explanationâŚ
Lets say you have a function which prints something
local module = {}
function module.abc(...)
print(...)
end
Now there is 2 ways to use this function
module.abc("a") --prints a
module:abc("a") --prints tableX000 and a
What is this supposed to do with self, and why does it print TableX0000,
Well, if you see the ways we called the function,
The first one was .
which printed ONLY what you gave, the second one is :
which printed what you gave + TableX0000
When using the :
it passes self to the function, which in this case is the module (table) thats why it printed the table name.
This is just a simple explanation if you have any questions feel free to ask
What is self though? what values does it hold / inherit
Lets say you have a module to make card or something, for this you use metatables:
local car = {}
car.__index = car
function car.new(brand, color)
local newCar = {}
setmetatable(newCar, car)
newCar.Brand = brand
newCar.Color = color
return newCar
end
return car
Now, in your reg script you would require the module script, then make a new car like this: carModule.new("Lamborghini", "Green")
Now you wanna make a function that you can call on your myCar
variable to change the color, like this:
myCar:ChangeColor("Blue")
In this case in your module youâll do something like this:
function car:ChangeColor(newColor)
self.Color = newColor
end
So when you call the function on myCar
variable, self will be myCar
Full module:
local car = {}
car.__index = car
function car.new(brand, color)
local newCar = {}
setmetatable(newCar, car)
newCar.Brand = brand
newCar.Color = color
return newCar
end
function car:ChangeColor(newColor)
self.Color = newColor
end
return car
Important note: self only exists within methods using the :functionName
Hopefully this cleared things up!
Does car.__index = car
make it so that anything that tries to check if a nil value âcarâ is avaliable, its parent would be in the table?
No, this is used when you use metatables, it is a metamethod, it basically means that when ever you make a metatable like we did here:
That it will inherit all the indexes and functions from car.
For more information look up .__index and metamethods
Im a bit confused, when should you use setmetatable, do you need to use it at the beginning of a script?
Does it connect two tables? and which one is the actual metatable?
So all of the values in the table?
Which one is the metatable? newCar or Car?
newCar is the metatable, it has all the functions and items that car
has + its own.
Here is a really great article on metatables and metamethods:
No, but you can. You use setmetatable whenever you want to, well, create a new metatable. In our case we used it when making a newCar so that every new car has everything a car has aswell as itâs own things, and also we can do stuff like what we did with the :ChangeColor function.
Yes, as mentioned in the article I linked in my previous reply: you can think of a metatable, as a normal table that holds configurations/settings for another table that letâs you change the behaviour of that other table. In short, metatables help add more functionalities to a table.
in the example i gave, self is the module (table) so ANYTHING inside it will be as a âpartâ of self, if you have a number called abc then self.abc will return the same value and so on