Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to use Self in metamethods?

I'm trying to get accustomed to using metamethods for increasing the efficiency of my Code. I've got a somewhat solid idea of how to use them, though I don't understand self. I presume it refers to the Object I'm applying the metamethod to. Though I may be wrong.

My Objective is to create a function, lets say for this question, a Color changing function. The result I'm seeking would look like so:

local Color = Color3.new(255,255,255)

Color:Change(0,0,0)

I’d like it to gradually apply to any passed Object, including variables.

I have gotten to use self from time to time, though I mostly used them after calling a function usually written called new. I'd like to not have to do this.

I have barely any knowledge on this and I believe it's easy, though I cannot find any proper resources to truly show me how this works.

How could I do this, where would I go to look?

7 comments

0

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 argument self.

0

I know that, I want to know what It looks like, can you read my question again

0

inb4 scriptguider answer (pls answer this scriptguider)

0

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

0

Apologies then, yeah let’s go with that and I’ll tell you if it was what I was looking for

0

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?

0

Yeah, sorry for the late response, I’ve been in a few exams

1 answer

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)--"Demo"
print(a.balance)--20

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)--Color3 constructor
	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)--lerping colors
	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)--150,150,150
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

2 comments

0

This is what I’m looking for, though I only would like to know how to write Code for Objects that already exist

This discussion is preserved as it was. New answers, comments, and votes are closed.