Method: Difference between revisions
>Trappingnoobs Added how to make a method. May not be completely accurate and/or not beginner friendly enough. Need somone to check it over. |
>NXTBoy Syntax was invalid |
||
Line 1: | Line 1: | ||
Methods are functions which belong to a particular object. Although standard Lua doesn't have them, they're a major focus of Roblox Lua. | Methods are functions which belong to a particular object. Although standard Lua doesn't have them, they're a major focus of Roblox Lua. | ||
Methods act 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. | Methods act 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 | These two lines of code are equivalent, calling the function <code>a</code> with the parameter <a>b</b>. | ||
b.a(b) | |||
b:a() | b:a() | ||
The second is more | The second is briefer and more concise. Methods can increase typing speed, because there is less need to retype variable names. | ||
==Examples== | ==Examples== | ||
All instances | All instances, possibly excluding some core services, have the remove method, along with the clone method. Sounds have the Play method and the Stop method. | ||
Sounds have the Play method and the Stop method. | |||
==Making your own== | ==Making your own== | ||
Line 26: | Line 21: | ||
{{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 functions.}} | ||
First, we need a table to ''apply'' the method to. I'm going to make a checkbox | 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) | |||
Checkbox.ImageButton.MouseButton1Down:connect( | |||
However, that would call <code>Checkbox.ChangeState(x, y)</code> when the event fired. We need to call <code>Checkbox.ChangeState(Checkbox)</code>. | |||
< | |||
Checkbox. | |||
</ | |||
When you call a function as a method, you automatically pass an argument as the TABLE ITSELF. For example, | When you call a function as a method, you automatically pass an argument as the TABLE ITSELF. For example, | ||
function ReturnTable(Num) | |||
function ReturnTable(Num) | return { | ||
Num, | |||
PrintNum = function(self) | |||
print(self.Num) | |||
end | |||
} | |||
end | |||
end | |||
Tabl = ReturnTable(5) | Tabl = ReturnTable(5) | ||
Tabl:PrintNum() | 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: | 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) | |||
Tabl.PrintNum(Tabl) | |||
Revision as of 08:50, 22 July 2011
Methods are functions which belong to a particular object. Although standard Lua doesn't have them, they're a major focus of Roblox Lua. Methods act 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 <a>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.
Examples
All instances, possibly excluding some core services, have the remove method, along with the clone method. Sounds have the Play method and the Stop method.
Making your own
Making your own methods can
- Make your code look cool
- Make your tables more dynamic
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, 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)