Thread Tools Display Modes
03-19-09, 11:39 PM   #1
eliljey
A Deviate Faerie Dragon
Join Date: Feb 2009
Posts: 14
Dynamically registering events

I've got a piece of code that changes some settings and needs to wait until an event indicates that the change was successful. (Refer to my SetLootThreshold thread to get an idea of what I'm doing.)

My current implementation registers the event in question ("PARTY_LOOT_METHOD_CHANGED" in this case) in my OnLoad function. The problem is that if I'm in a group and someone else triggers the event my addon tries to do it's thing because it's always watching.

I know you can unregister events but I was curious if it's considered "bad coding" to have a function that registers an event, runs code that should trigger the event, and then unregister it or is that exactly what the register/unregister functionality is for?
  Reply With Quote
03-20-09, 02:15 AM   #2
Tristanian
Andúril
Premium Member
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 279
It's not considered bad coding to unregister events. Quite the contrary actually. If you are only going to use event registration for specific, one time or generally rare actions/tasks (eg variable initialization), then it is usually wise to unregister the event after the relevant code has been executed. In your particular case however, I suppose you want to execute SetLootThreshold() or related code after the PARTY_LOOT_METHOD_CHANGED has fired, but make sure that this doesn't happen in response to the event being fired without you "causing" it. Why not just set a boolean to true when you change the loot method yourself, then check against that value in the code executing when the event fires. If true (meaning, you did change the loot method yourself), then execute the code normally (don't forget to reset the boolean), if false, don't do anything.
__________________
  Reply With Quote
03-20-09, 08:36 AM   #3
eliljey
A Deviate Faerie Dragon
Join Date: Feb 2009
Posts: 14
Yeah, there's actually a couple of ways to attack the problem. This was just one of the solutions I came up with and it made me wonder if the technique was considered bad coding.

I'm not a software guy but I play one on WoWInterface.com.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Dynamically registering events


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