Thread Tools Display Modes
09-08-10, 09:08 PM   #1
Tiok
A Defias Bandit
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 2
How to Deal With Loss of "this", "event", "arg#"

As we've been informed, the "this" "event" and "arg#" globals have vanished. I haven't seen a thread talking about what the impacts of these losses are, nor any commentary on how to deal them. I know I don't have all the answers yet, but hopefully we can collect them in this thread so that authors have a place to start with porting their addons.

Please, chime in with any problems/solutions you have come up with in regards to these disappearing globals!


Dealing with the Loss of "this"

"this" used to see a lot of use in XML-defined event handlers. You'll need to go through your XML files and replace all "this" references with "self" instead. This still works because any XML-defined event handler is called internally with the ":OnEvent()" format, which invisibly passes a "self" parameter into the code.

In any other function that used to rely on "this", you'll need to explicitly pass the "self" parameter from wherever the function is called. That means either using MyObj.MyFunc(self,...) or MyObj:MyFunc(...). The second invisibly passes MyObj in as the first parameter before the ... .


Dealing with the Loss of "event"

"event" used to be used in XML-defined event handlers as well, especially the OnEvent handler, where you could pass it on to different handler functions based on the name of the event. Although "event" is no longer a global, it IS present in the XML-defined OnEvent handler. The internal workings just pass it directly to OnEvent as a parameter now, rather than making it global. You actually don't have to worry about the change here, though you will have to make sure you're passing "event" through to any subsequent calls in your XML-defined handler that need it.


Dealing with the Loss of "arg#"
"arg#", the event argument globals, were used with almost every single event handler function. As with "event", these are now being passed to the XML-defined OnEvent handler as parameters instead of globals. Officially they're coming in as a "...", which you'll need to make sure you're passing along to any function calls that were making use of the global "arg#" variables before.
One handy trick to parse the "..." into useful parameters is this:
Code:
   local arg1, arg2, arg3 = ...
Which you can use at the start of any function that used the old global variables for a quick fix. It would probably be better to switch over to more meaningful names, though.


If you know the answer to any of these questions, or know any other problems and/or solutions related to these issues, let us know about it here!

Last edited by Tiok : 09-14-10 at 06:44 AM.
 
09-08-10, 09:27 PM   #2
IQgryn
A Cyclonian
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 46
Originally Posted by Tiok View Post
Dealing with the Loss of "this"

"this" used to see a lot of use in XML-defined event handlers. You'll need to go through all of your XML files and replace the "this" references with the actual frame in question.
(e.g. this:StartMoving() becomes MyFrame:StartMoving() )

There's one major complication that I don't know how to deal with yet, though. That is virtual, or template, frames. You don't know the name of the frame in the XML, since you're instantiating from the template at runtime. You used to be able to use "this" in virtual frame event handlers, but now that it's gone, what can we do instead? How can we access the frame instances inside the XML-defined event handlers?
For any function that could be called as Frame:function() (this includes the XML-defined functions), you can use self inside that function to refer to its frame.

If you use a template, save the output of CreateFrame somewhere. For example:
Code:
local frame = CreateFrame("Frame", ...)
frame:SetPoint("TOPLEFT")
will work, and you can save that variable somewhere more permanent if you need to.

Last edited by IQgryn : 09-08-10 at 09:32 PM. Reason: Clarity
 
09-08-10, 09:38 PM   #3
IQgryn
A Cyclonian
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 46
Originally Posted by Tiok View Post
Dealing with the Loss of "arg#"
"arg#", the event argument globals, were used with almost every single event handler function. Now that they're no longer global, it seems that you have to pass them to your event handlers manually... but where do you get them from in the first place? Especially, how do you get access to event arguments inside the XML-defined OnEvent handler, so you can pass them through?
I believe the event handlers have all been converted to use ... instead of specific argument variables. You can emulate the old behavior by putting the following code at the beginning of your event handlers:
Code:
local arg1, arg2, arg3 = ...
(Add or subtract variables as necessary for each handler). You could of course use better names for each handler, which will require more work but be much easier to read later on.
 
09-08-10, 09:53 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Tiok View Post
With "event" gone, how do we tell which event occurred in the XML-defined OnEvent handler?

how do you get access to event arguments inside the XML-defined OnEvent handler, so you can pass them through?
As with self, the event and any arguments are passed to your OnEvent handler. These are identical:

Code:
local myFrame = CreateFrame("Frame", "MyFrame")
myFrame:SetScript("OnEvent", function(self, event, ...)
    -- self is a reference to the frame
    -- event is a string indicating which event is being handled
    -- ... is a vararg containing any and all arguments passed with the event
end)
Code:
<Frame name="MyFrame">
    <OnEvent>
        -- self is a reference to the frame
        -- event is a string indicating which event is being handled
        -- ... is a vararg containing any and all arguments passed with the event
    </OnEvent>
</Frame>
As someone else already pointed out, you get the individual arguments like this:
Code:
local arg1, arg2, arg3 = ...
 
09-08-10, 10:04 PM   #5
TSquared
Big Daddy!
Join Date: May 2008
Posts: 527
Event handlers - surprisingly, this is pretty valid

http://forums.worldofwarcraft.com/th...cId=36975623#5
 
09-09-10, 01:11 AM   #6
Gorebag
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 11
I guess I had followed a very old/bad sample when creating my XML for my handlers, because my original code had:

Code:
<OnLoad>QuestClicks_OnLoad();</OnLoad>
to define the OnLoad handler. I then relied on the "this" to access things.

In converting to "self", I found that I had to replace the above in the XML file with:

Code:
<OnLoad>QuestClicks_OnLoad(self);</OnLoad>
and update the definitions of the function in the LUA file to include the parameter. Without specifying the parameter in both locations, pretty much nothing happened/ran at all with my addon - the OnLoad method didn't seem to be called at all unless I included the parameter in the XML file.

When setting OnEvent, I currently have:

Code:
self:SetScript("OnEvent", QuestClicks_OnEvent);
in lua. The QuestClicks_OnEvent function is defined as:

Code:
function QuestClicks_OnEvent(self, event, ...)
"arg1", etc still get set (as locals) this way inside the OnEvent handler without needing to add a "local arg1, arg2 = ..." line.

Last edited by Gorebag : 09-09-10 at 01:13 AM.
 
09-09-10, 02:25 AM   #7
v6o
An Onyxian Warder
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 399
Even if it's not "new". Blizzard themselves have moved over to using

<OnEvent function="OnEventFunction"/>

which passes all arguments with it... basically the same as

<OnEvent>OnEventFunction(self, event, ...)</OnEvent>
__________________
I stopped playing back World of Warcraft in 2010 and I have no plans on returning.
This is a dead account and if you want to continue any of my addons or make a fork then feel free to do so.
This is your permission slip.

If you need to contact me, do so on Twitter @v6ooo

Best regards, v6.

Last edited by v6o : 09-09-10 at 03:38 AM. Reason: <OnEvemt>OnEventFunction(...)</OnEvent> -> <OnEvent>OnEventFunction(self, event, ...)</OnEvent>
 
09-09-10, 03:33 AM   #8
Cladhaire
Salad!
 
Cladhaire's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Jul 2005
Posts: 1,935
Originally Posted by v6o View Post
Even if it's not "new". Blizzard themselves have moved over to using

<OnEvent function="OnEventFunction"/>

which passes all arguments with it... basically the same as

<OnEvemt>OnEventFunction(...)</OnEvent>
This actually isn't the case, it's the same as:

Code:
<OnEvent>OnEventFunction(self, event, ...)</OnEvent>
If you ever have a question about precisely WHAT arguments are passed to which widget handler, including OnEvent, you can check the master list at http://wowprogramming.com/docs/scripts
__________________
"There's only one thing that I know how to do well and I've often been told that you only can do what you know how to do well, and that's be you-- be what you're like-- be like yourself. And so I'm having a wonderful time, but I'd rather be whistling in the dark..."
 
09-10-10, 09:50 AM   #9
Fodaro
A Cyclonian
 
Fodaro's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 42
Originally Posted by Tiok View Post
One handy trick to parse the "..." into useful parameters is this:
Code:
   local arg1, arg2, arg3 = ...
What I've always done, saving a line of code, is this:
lua Code:
  1. function MyEventHandler(self, event, arg1, arg2, arg3, ...)
And then in XML...
xml Code:
  1. <OnEvent>
  2. MyEventHandler(self, event, ...)
  3. </OnEvent>
As you've said, meaningful names are also a good idea when you can do it. And don't forget select().
__________________
Fodaro
(Main: Fodaro-Bronzebeard (EU))
Author of CharNote and Consolid8
 
09-12-10, 09:42 PM   #10
MysticalOS
A Wyrmkin Dreamwalker
 
MysticalOS's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 54
i'm having a substancia amount of trouble fixing an abandoned addon that isn't mine for personal use. i've found no equivilent so i want to make luckycharms2 work in cata but the addon has ridiculous overuse of this, arg1 and lack of proper event and self usage. I've fixed most of it but i'm stumped on a few remander issues

i'd post it all here but it'd just be a lot of spam, but if someone who isn't as clueless as me wants to help me with it. or point me to a similar addon that isn't abandoned.
 
09-13-10, 12:24 AM   #11
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Asking for help to be given privately defeats the purpose of having a community forum. If there are specific pieces of code you're having trouble adapting, start by posting (in this thread, or a new one) a particular section of code (and enough of the surrounding code to give context) and explain what you don't understand. If the code is more than 10-15 lines, use a service like pastebin.com, pastey.net, or paste.wowace.com and include the URL in your forum post.
 
09-13-10, 04:46 AM   #12
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Thank god I never use XML

It's supposed to be a markup language people!
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
 
09-13-10, 04:49 AM   #13
Cladhaire
Salad!
 
Cladhaire's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Jul 2005
Posts: 1,935
Perhaps I'm missing something, but none of this is specific to XML, other than the way you declare your script handlers in an XML file, which is essentially identical to the way you have to do it in Lua. Badly written code is badly written code, when deprecation details are made clear.
__________________
"There's only one thing that I know how to do well and I've often been told that you only can do what you know how to do well, and that's be you-- be what you're like-- be like yourself. And so I'm having a wonderful time, but I'd rather be whistling in the dark..."
 
09-13-10, 04:53 AM   #14
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
I have always used this declaration which is not affected by the deprecation:

Code:
someframe:SetScript("OnEvent", function(self, event, arg1, arg2, arg3)

end)
TBH I didn't even know these globals existed, and never had any trouble living without them.
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
 
09-13-10, 04:59 AM   #15
Cladhaire
Salad!
 
Cladhaire's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Jul 2005
Posts: 1,935
That's a function of the code you've written, not whether or not its been written in Lua or XML. The global arguments are leftover from 1.x, and have been deprecated since 2.x. Also, if you use that for every single OnEvent handler, it obviously won't work since many events can pass upwards of 12 arguments. There's a reason that the WoW client sends them as variable arguments, and uses this. The conventions are all actually set by the way the WoW client passes arguments to the XML handlers, since we MUST match those names or it doesn't work. Luckily, they're sensible and well documented. See http://wowprogramming.com/docs/scripts/OnEvent.

When you see:

Code:
<Scripts>
  <OnEvent>
    foo blah bar
  </OnEvent>
</Scripts>
The bit inside the
Code:
<OnEvent>
is wrapped with
PHP Code:
function(selfevent, ...) 
and
PHP Code:
end 
The moral of the story is that both XML and Lua are valid tools for writing addons, and there are things you can do in Lua that you can't do easily in XML, and there are things you can do in XML that you can't do easily in Lua.

None of that, however, is relevant for this thread which is about the deprecation and final removal of the global arguments. That's what my original response was meant to indicate.
__________________
"There's only one thing that I know how to do well and I've often been told that you only can do what you know how to do well, and that's be you-- be what you're like-- be like yourself. And so I'm having a wonderful time, but I'd rather be whistling in the dark..."
 
09-13-10, 01:09 PM   #16
MysticalOS
A Wyrmkin Dreamwalker
 
MysticalOS's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 54
Originally Posted by Phanx View Post
Asking for help to be given privately defeats the purpose of having a community forum. If there are specific pieces of code you're having trouble adapting, start by posting (in this thread, or a new one) a particular section of code (and enough of the surrounding code to give context) and explain what you don't understand. If the code is more than 10-15 lines, use a service like pastebin.com, pastey.net, or paste.wowace.com and include the URL in your forum post.
i'd basically have to paste the entire contents of addon then,c ause honestly it's got so many problems. i've only managed to fix maybe a handful of the many. i fixed onload and onevent only. since there were good examples of both on here. but every other fuction, changing this to self causes that entire function to just nil error. i've tried many variations of self, event, ..., or just self, or just eventname etc in the function and nothing makes it return anything but nil without changing all the self's back to this. as for addon itself i don'te ven know why it was pulled, maybe by author request, it got removed from ALL addon sites sometime this year, it's last update was dec 2009 and only remaining place it's hosted is http://www.tankadin.com/index.php?op...d&catid=8:mods

yet to date i haven't liked a single other raid marking addon but luckycharms2. it'd probably take less than 5 min for someone that knows what their doing to fix. unfortunately i've never really been good with the coding functions. I'm more of a template guy, give me a template to follow and i can do entire addons. I was able to add thousands of lines of code to DBM using examples that already exist in core and earlier raid mods. but ask me to actually add a new function from scratch and i'm clueless
 
09-13-10, 02:38 PM   #17
IQgryn
A Cyclonian
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 46
Originally Posted by MysticalOS View Post
i'd basically have to paste the entire contents of addon then,c ause honestly it's got so many problems. i've only managed to fix maybe a handful of the many. i fixed onload and onevent only. since there were good examples of both on here. but every other fuction, changing this to self causes that entire function to just nil error. i've tried many variations of self, event, ..., or just self, or just eventname etc in the function and nothing makes it return anything but nil without changing all the self's back to this. as for addon itself i don'te ven know why it was pulled, maybe by author request, it got removed from ALL addon sites sometime this year, it's last update was dec 2009 and only remaining place it's hosted is http://www.tankadin.com/index.php?op...d&catid=8:mods

yet to date i haven't liked a single other raid marking addon but luckycharms2. it'd probably take less than 5 min for someone that knows what their doing to fix. unfortunately i've never really been good with the coding functions. I'm more of a template guy, give me a template to follow and i can do entire addons. I was able to add thousands of lines of code to DBM using examples that already exist in core and earlier raid mods. but ask me to actually add a new function from scratch and i'm clueless
After a brief look at the code, I can only say that it's a mess. However, to try to help you out: these four code snippets all work exactly the same way.

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.

You can mix and match the period and the colon without any problems as long as you remember to pass/declare self when you use a period. However, this can get confusing and it's probably best to just stick with a colon for function that need self and a period otherwise.
 
09-13-10, 03:48 PM   #18
IQgryn
A Cyclonian
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 46
Originally Posted by IQgryn View Post
After a brief look at the code, I can only say that it's a mess.
I looked a bit harder, and while it's still not something I'd want to maintain I think I've gotten rid of all the nil errors on the beta. I don't know if it works or not because I don't know what it's supposed to do.

I've only changed the main two lua and xml files. I'll just pastebin those for now, but I'm open to other suggestions.

MysticalOS, would you mind testing this and seeing if I broke anything?

LuckyCharms2.lua
LuckyCharms2.xml (didn't want to work on pastebin for some reason)
 
09-13-10, 04:56 PM   #19
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,359
I took a look and it's not so hard to update it, but...
What differentiates it from the numerous other mark-bar addons including this beauty?

(to be worth the trouble reviving it I mean)

Last edited by Dridzt : 09-13-10 at 06:40 PM.
 
09-13-10, 05:33 PM   #20
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Yeah, there are several raid icon addons out there. My biased recommendation: Symbology
 
 

WoWInterface » AddOns, Compilations, Macros » Cataclysm Beta » How to Deal With Loss of "this", "event", "arg#"

Thread Tools
Display Modes

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