Methods: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
No edit summary
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">"
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Methods are unique functions which belong to a particular object or can be custom made.  They are a major focus of Roblox Lua. Some common examples of methods include 'Destroy()', 'Clone()', and 'FindFirstChild()'. You can find a full list of methods [http://wiki.roblox.com/index.php/Category:Methods here].
Methods are unique functions which belong to a particular object or can be custom made.  They are a major focus of Roblox Lua. Some common examples of methods include 'Destroy()', 'Clone()', and 'FindFirstChild()'. You can find a full list of methods [http://wiki.roblox.com/index.php/Category:Methods here].


A method acts like a function stored within an object.  The method itself is accessed in the same way a [[Functions|function]] in a table is accessed.  However, a special property of methods changes how they are called.
A method acts like a {{type|function}} stored within an object.  The method itself is accessed in the same way a {{type|function}} in a {{type|table}} is accessed.  However, a special property of methods changes how they are called.


These two lines of code are equivalent, calling the function <var>a</var> with the parameter <var>b</var>.
These two lines of code are equivalent, calling the {{type|function}} <var>a</var> with the parameter <var>b</var>.


<code lua>
<syntaxhighlight lang="lua">
b.a(b)  
b.a(b)  
b:a()
b:a()
</code>
</syntaxhighlight>


The second is briefer and more concise. Methods can increase typing speed, because there is less need to retype variable names.
The second is briefer and more concise. Methods can increase typing speed, because there is less need to retype variable names.
Line 18: Line 18:
*Make your tables more dynamic
*Make your tables more dynamic


{{EmphasisBox|This tutorial-like section assumes you have a good knowledge of table terminology and functions.}}
{{EmphasisBox|This tutorial-like section assumes you have a good knowledge of table terminology and understand functions.}}


First, we need a table to ''apply'' the method to. I'm going to make a checkbox. It doesn't actually do anything, but it's an application example. Feel free to make this work.
First, we need a table to ''apply'' the method to. I'm going to make a checkbox. It doesn't actually do anything, but it's an application example. Feel free to make this work.


<code lua>
{{code|=
Checkbox = {
Checkbox = {
Checked = false;
Checked = false;
Line 35: Line 35:
Checkbox:ChangeState()
Checkbox:ChangeState()
end)
end)
</code>
}}


You might think you could type
You might think you could type


<code lua>Checkbox.ImageButton.MouseButton1Down:connect(Checkbox.ChangeState)</code>
{{code|=Checkbox.ImageButton.MouseButton1Down:connect(Checkbox.ChangeState)}}


However, that would call <code lua>Checkbox.ChangeState(x, y)</code> when the event fired. We need to call <code lua>Checkbox.ChangeState(Checkbox)</code>.
However, that would call <syntaxhighlight lang="lua">Checkbox.ChangeState(x, y)</syntaxhighlight> when the event fired. We need to call <syntaxhighlight lang="lua">Checkbox.ChangeState(Checkbox)</syntaxhighlight>.


When you call a function as a method, you automatically pass an argument as the TABLE ITSELF. For example,
When you call a {{type|function}} as a method, you automatically pass an argument as the TABLE ITSELF. For example,


<code lua>
{{code|=
function ReturnTable(Num)
function ReturnTable(Num)
return {
return {
Num;
Num = Num;
PrintNum = function(self)  
PrintNum = function(self)  
print(self.Num)  
print(self.Num)  
Line 56: Line 56:
Tabl = ReturnTable(5)
Tabl = ReturnTable(5)
Tabl:PrintNum()
Tabl:PrintNum()
</code>
}}


That will output 5 because we passed in the table, so the function will receive the table it's located in. Because we called it as a method, it passed "Tabl" as an argument. You can do it with a dot, but it looks kind of stupid:
That will output 5 because we passed in the {{type|table}}, so the {{type|function}} will receive the {{type|table}} it's located in. Because we called it as a method, it passed "Tabl" as an argument. You can do it with a dot, but it looks kind of stupid:


<code lua>Tabl.PrintNum(Tabl)</code>
{{code|=Tabl.PrintNum(Tabl)}}

Latest revision as of 06:02, 27 April 2023

Methods are unique functions which belong to a particular object or can be custom made. They are a major focus of Roblox Lua. Some common examples of methods include 'Destroy()', 'Clone()', and 'FindFirstChild()'. You can find a full list of methods here.

A method acts like a function stored within an object. The method itself is accessed in the same way a function in a table is accessed. However, a special property of methods changes how they are called.

These two lines of code are equivalent, calling the function a with the parameter b.

b.a(b) 
b:a()

The second is briefer and more concise. Methods can increase typing speed, because there is less need to retype variable names.

Making your own

Making your own methods can

  • Make your code look cool
  • Make your tables more dynamic
This tutorial-like section assumes you have a good knowledge of table terminology and understand functions.

First, we need a table to apply the method to. I'm going to make a checkbox. It doesn't actually do anything, but it's an application example. Feel free to make this work.

Checkbox = {
	Checked = false;
	CheckedImage = "Image";
	UncheckedImage = "Image";
	ImageButton = ...;
	ChangeState = function(self)
		self.Checked = not self.Checked
	end
}
Checkbox.ImageButton.MouseButton1Down:connect(function()
	Checkbox:ChangeState()
end)

You might think you could type

Checkbox.ImageButton.MouseButton1Down:connect(Checkbox.ChangeState)

However, that would call

Checkbox.ChangeState(x, y)

when the event fired. We need to call

Checkbox.ChangeState(Checkbox)

.

When you call a function as a method, you automatically pass an argument as the TABLE ITSELF. For example,

function ReturnTable(Num)
	return {
		Num = Num;
		PrintNum = function(self) 
			print(self.Num) 
		end
	}
end
Tabl = ReturnTable(5)
Tabl:PrintNum()

That will output 5 because we passed in the table, so the function will receive the table it's located in. Because we called it as a method, it passed "Tabl" as an argument. You can do it with a dot, but it looks kind of stupid:

Tabl.PrintNum(Tabl)