Thread Tools Display Modes
01-20-09, 01:49 PM   #1
Nuckin
is laughing at you
 
Nuckin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 58
'this' global variable

I have programmed in many language including C/C++, Java, BASIC, PHP, Python, Bash... etc, but Lua is the only one (maybe its just the Blizzard interpretation of Lua) with a global variable 'this'.

In all original code I have written, I haven't used it once. But right now I'm working on updating an addon to be 100% compliant with 3.0.x's deprecation of this variable. Some of the 'this' calls I have hacked out using 'self' (which I have figured out).

So what I'm getting at is what is 'this'? Any light people could shed on this would be awesome, I have yet to find a formal definition anywhere on google or in any books I own.
  Reply With Quote
01-20-09, 02:43 PM   #2
Davnel
A Fallenroot Satyr
 
Davnel's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 24
it was replaced by self.

Mainly 'self' needs to be passed as an arguement, 'this' on the other hand didn't. So in most cases its a case of seeing where 'this' is used and replacing it with 'self' AND adding 'self' as an input parameter, and working backwards to find out where self needs to come from, which is normally from any xml script calls.

Wont take too long, just be ready to miss out adding self to a few function calls.
  Reply With Quote
01-20-09, 03:20 PM   #3
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
(maybe its just the Blizzard interpretation of Lua)
It is.

"this" is a reference to the frame that most recently had one of its handler functions called. Thus it's value is identical to "self"'s as long as "self" has been properly passed down from the handler that received it. Similarly, "event" "arg1" "arg2" etc. for event firings are globals that have the same value as the parameters passed to the OnEvent handler after "self"

Last edited by Akryn : 01-20-09 at 03:23 PM.
  Reply With Quote
01-20-09, 03:33 PM   #4
Nuckin
is laughing at you
 
Nuckin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 58
I replaced a whole slew of variable references of the style "this.casting = nil" with "self.casting = nil" without self as a parameter in the function. Seems to be working somehow
  Reply With Quote
01-20-09, 06:38 PM   #5
Tuhljin
A Flamescale Wyrmkin
 
Tuhljin's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2008
Posts: 106
"self" is implied as the first argument if the function is defined like so:
Code:
function MyTable:PrintNameAndNumber(num)
  print(self.name, num)
end
Notice the colon (":").

It's also implied when used in the XML like this:
Code:
<OnEnter>
  GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
  GameTooltip:AddLine(self.date, 1, 1, 1);
  GameTooltip:Show();
</OnEnter>
However, in a case like the below, self isn't implied so you'd have to put it in yourself:
Code:
MyFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
MyFrame:SetScript("OnEvent", function(self)
  print(self.name)
end)
See: http://lua-users.org/wiki/ObjectOrientationTutorial
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » 'this' global variable


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off