So, I want to start using modular scripting and I heard that you need to know what “self” is. So can someone explain “self”?
I want to know how to use self like:
self.thing()
For example in modules, where you invoke functions ( call with : ), then the first argument passed is automatically the module which has the function you’re invoking. That first argument can be used as “self” in the function arguments.
Sorry if I didn’t explain this clearly, tell me what you do not understand and I’ll try to say it better.
Ok, thanks!
Here’s a code sample:
module = {}
module.value = 5
module.increase = function(self,value)
self.value = self.value + value
end
print(module.value) -- 5
module:increase(3)
print(module.value) -- 8
You don’t actually have to name the argument as self and any other name would be fine, but “self” is a good way to identify it.
I don’t get it…
so “self” is the function name?
because you said
then the first argument passed is automatically the module
Needs to be
module:increase(3)
-- or
module.increase(module, 3)
Otherwise self won’t get passed
self in OOP is the table that contains methods or the table you are referencing
Apologies, I didn’t notice that
One more thing, if you define the function using syntax like this
function module:increase(value)
self.value = self.value + value
end
Then the name would remain self and would be unchangeable.
local Sword = {}
Sword.__index = Sword
function Sword.new()
return setmetatable({}, Sword)
end
function Sword:TakeDamage()
--//self is basically sword The table that you are referencing when creating functions
end
If you wanted to use self, you could do:
module.increase(module,3)
and use self as the first argument name inside the function (which is the module) afterwards
But there is syntactic sugar which is much easier to write and much prettier which is replacing . with : and removing the first argument which is the module since it would be included automatically:
module:increase(3)
local Sword = {}
Sword.__index = Sword
function Sword.new() --//Creating the basic framework of the sword
self.Damage = 50 --// == Sword.Health = 0
self.Attacking = false --// == Sword.Attacking = false
return setmetatable({}, Sword)
end
function Sword:TakeDamage()
self.Attacking = true --//Changing the value in sword to true
end
oh i get it now, thanks
Stupid example you can use for minigames
local Class = {}
function Class.new()
Class.Money = 0
end
function Class:PlayerWin()
self.Money = self.Money + 10
print("You have won the minigame")
end
Class.new()
wait(5)
print(Class.Money) --Before they won
Class:PlayerWin()
--//After they won
print(Class.Money)
If this was a minigame you would set there MoneyValue In class equal to their actual leaderstats Value but this is an example
Will this call the argument of the function? if I use self outside of a function can I call any argument or variable? I’m not entirely clear, does it also work with normal script?
Self use to returns the table the function is being called from.
local hi = {}
function hi.__tostring(self, ...)
return "hi table!"
end
function hi:something(...)
print(tostring(self))
end
Try that and u will know how it works
Extra Information:
https://www.lua.org/pil/16.html
self is used in OOP (Object Oriented Programming) in metatables.
local module = {}
module.__index = module
function module.new()
local self = setmetatable({},module)
return self
end
Now every time you make a function in the module it is added to the metatable that has been declared as self.