Object Oriented Programming Example
Before I try to explain how to use self, I would like to bring up object oriented programming, as this is what you are trying to do in this case.
The easiest and simplest example of this would be the account example in chapter 16 of the lua pil, specifically the :new()function
Account = {balance = 0}
function Account:new (o, name)
o = o or {name=name}
setmetatable(o, self)
self.__index = self
print(self)
return o
end
function Account:deposit (v)
self.balance = self.balance + v
print(self)
end
local a = Account:new(nil,"Demo")
a:deposit(20)
print(a.name)
print(a.balance)
as you can see, this uses the __index metamethod and sets self as the metatable of o, therefore, if I try index something that is nil for that specific account, such as balance, it will fire the __index metamethod, same thing happens when I use the deposit function, as deposit is a method of the metatable of account a.
Something interesting that happens is that if you were to print self when running a:deposit then it prints table a, and not the Account table.
What self seems to be
Generally, the implicit self parameter of a method is the table from which the method is called, in the previous example, self for method deposit is the table a whenever it was a:deposit that was called. however, when defining a as Account:new(nil,"Demo"), printing self in the :new() method prints the Account table.
How this can be used for a color changing function would essentially have the same principle, that being using the index metamethod to access important functions:
Example With Pseudo Color functions
local function lerp(a,b,c)
return a + (b-a) * c
end
local Color3 = {}
function Color3:new(r,g,b)
r= r or 0
g = g or 0
b = b or 0
local Colors = {r=r,g=g,b=b}
setmetatable(Colors,self)
self.__index = self
return Colors
end
function Color3:Lerp (newColor,Alpha)
local LerpedColors = {}
for k,v in pairs (self) do
LerpedColors[k] = lerp(v,newColor[k],Alpha)
end
return Color3:new(LerpedColors.r,LerpedColors.g,LerpedColors.b)
end
function Color3:Change(newColor)
for alpha = 0, 1, 0.05 do
self = self:Lerp(Color3:new(0,0,0),alpha)
end
end
local c = Color3:new(200,200,200)
local c2 = c:Lerp(Color3:new(100,100,100),0.5)
for k,v in pairs(c2) do
print(v)
end
Hopefully my explanation wasn't too inaccurate or confusing, and hopefully it helped you out with your problem :3
Reference
OOP lua pil chapter
7 comments
self is just the first parameter, consider you have a part named
p, you could destroy p by doing p:Destroy() OR you could also do p.Destroy(p). Both do the same exact thing, the one with the colon just implicitly sends itself as the first argumentself.I know that, I want to know what It looks like, can you read my question again
inb4 scriptguider answer (pls answer this scriptguider)
You're saying you want some sort of function that just gradually changes objects' number values? Reading the question again doesn't help, you need to word it better
Apologies then, yeah let’s go with that and I’ll tell you if it was what I was looking for
It's fine, I just have a hard time seeing how self is related to the general idea of the post itself. Are you specifically looking for this to work on Color3s or rather on other data types like Vector3, UDim2, etc?
Yeah, sorry for the late response, I’ve been in a few exams