WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   'this' global variable (https://www.wowinterface.com/forums/showthread.php?t=20180)

Nuckin 01-20-09 01:49 PM

'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.

Davnel 01-20-09 02:43 PM

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.

Akryn 01-20-09 03:20 PM

Quote:

(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"

Nuckin 01-20-09 03:33 PM

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 :confused:

Tuhljin 01-20-09 06:38 PM

"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


All times are GMT -6. The time now is 12:21 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI