Quantcast WoWInterface - View Single Post - the _G changes.
Thread: the _G changes.
View Single Post
09-17-10, 10:53 AM   #9
IQgryn
A Cyclonian
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 46
Originally Posted by Seerah View Post
You still have to define self with Frame:function() like so
Code:
Frame:function(self)
With the : notation, the frame reference is passed as the first argument (which you need to define with a variable if you wish to use it).
Actually, the self parameter is implied in that case. The only time you'd need to declare it specifically is if you called and/or defined it with a period instead of a colon. To quote myself from another thread:

Originally Posted by IQgryn View Post
These four code snippets are exactly the same thing with different syntax:

Code:
function MyAddon:MyFunc(var1) print(self, var1) end
MyAddon:MyFunc("test value")
Code:
function MyAddon.MyFunc(self, var1) print(self, var1) end
MyAddon.MyFunc(MyAddon, "test value")
Code:
function MyAddon:MyFunc(var1) print(self, var1) end
MyAddon.MyFunc(MyAddon, "test value")
Code:
function MyAddon.MyFunc(self, var1) print(self, var1) end
MyAddon:MyFunc("test value")
In other words:
When defining a function, if a colon is used, self will be the first parameter (before any others you list).
When calling a function, self is automatically passed (as the first argument) with the value of whatever is before the colon when you use a colon instead of a period.
This is nothing specific to WoW; all of Lua works this way to make object-oriented code easier to write.