Thread Tools Display Modes
10-10-16, 01:17 AM   #1
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
AceEvent library and RegisterUnitEvent

Hi all,

As far as I know (or as far as I did a proper research), AceEvent-3.0 does not support RegisterUnitEvent because it utilizes a single frame to manage registered events, and each frame object in WoW can only handle maximum two different units at once.

Thus, such event like PLAYER_SPECIALIZATION_CHANGED to be fired for a player only, the easiest way that I could think of is

Lua Code:
  1. function MyAddOn:PLAYER_SPECIALIZATION_CHANGED(event, ...)
  2.     if ... == "player" then
  3.        
  4.     end
  5. end

where it still has to calls this function and go through 'if' statement

or, maybe just create an extra frame that only manages unit event...


What would be the best solution here?
  Reply With Quote
10-10-16, 03:27 AM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Yes, your example is exactly how it worked before RegisterUnitEvent was introduced in MoP.

However, based on the event you chose as an example, I think you may be confused about what RegisterUnitEvent does. "Unit events" are events whose names start with "UNIT_" and whose first argument is a unit token. PLAYER_SPECIALIZATION_CHANGED is not a "unit event", so you can't use RegisterUnitEvent with it anyway. In your example code, your event handler would never actually do anything, because the first argument for PLAYER_SPECIALIZATION_CHANGED is not a unit token; actually, it's not anything, since PLAYER_SPECIALIZATION_CHANGED doesn't fire with any arguments. You would only use RegisterUnitEvent for events like UNIT_AURA or UNIT_HEALTH. For all other events, you just use RegisterEvent.

On a side note, if you're already using a dedicated function for each event, there's no reason to use a vararg (...). It just makes your code harder to read (and adds some so-small-as-to-be-mostly-just-theoretical overhead). You know what arguments the event will receive, so just name them directly in the function definition:

Code:
function MyAddOn:UNIT_SPELLCAST_START(event, unit, spellName, spellRank, lineID, spellID)
    if unit == "player" then
        
    end
end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 10-10-16 at 03:33 AM.
  Reply With Quote
10-10-16, 04:23 AM   #3
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Phanx View Post
Yes, your example is exactly how it worked before RegisterUnitEvent was introduced in MoP.

However, based on the event you chose as an example, I think you may be confused about what RegisterUnitEvent does. "Unit events" are events whose names start with "UNIT_" and whose first argument is a unit token. PLAYER_SPECIALIZATION_CHANGED is not a "unit event", so you can't use RegisterUnitEvent with it anyway. In your example code, your event handler would never actually do anything, because the first argument for PLAYER_SPECIALIZATION_CHANGED is not a unit token; actually, it's not anything, since PLAYER_SPECIALIZATION_CHANGED doesn't fire with any arguments. You would only use RegisterUnitEvent for events like UNIT_AURA or UNIT_HEALTH. For all other events, you just use RegisterEvent.

On a side note, if you're already using a dedicated function for each event, there's no reason to use a vararg (...). It just makes your code harder to read (and adds some so-small-as-to-be-mostly-just-theoretical overhead). You know what arguments the event will receive, so just name them directly in the function definition:

Code:
function MyAddOn:UNIT_SPELLCAST_START(event, unit, spellName, spellRank, lineID, spellID)
    if unit == "player" then
        
    end
end
Hi Phanx,

Thank you so much for your clarification!

I thought that every single events in WoW can be registered as a unit specific event D:...

Now I won't make the same mistake

Thank you again!!

Last edited by Layback_ : 10-10-16 at 06:54 AM.
  Reply With Quote
10-10-16, 07:04 AM   #4
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
You probably saw this thread, for reference
https://forums.wowace.com/showthread.php?t=20124
  Reply With Quote
10-10-16, 04:17 PM   #5
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Ketho View Post
You probably saw this thread, for reference
https://forums.wowace.com/showthread.php?t=20124
You are totally right, haha

I've only read Nevcairiel's comment saying that he considered adding RegisterUnitEvent function to AceEvent which he could've not due to conceptual problem.

If I paid more attention to Phanx's comments, I could've not made such a silly mistake

Last edited by Layback_ : 10-10-16 at 04:31 PM.
  Reply With Quote
10-10-16, 10:40 PM   #6
nebula
A Deviate Faerie Dragon
 
nebula's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2007
Posts: 16
Originally Posted by Phanx View Post
However, based on the event you chose as an example, I think you may be confused about what RegisterUnitEvent does. "Unit events" are events whose names start with "UNIT_" and whose first argument is a unit token. PLAYER_SPECIALIZATION_CHANGED is not a "unit event", so you can't use RegisterUnitEvent with it anyway.
PLAYER_SPECIALIZATION_CHANGED is actually a unit event now, so maybe that is the source of some confusion. I'm not sure when it changed, but it fires for more than just the player and passes the unit.

Anyway, probably overkill for most things, but we create a pseudo unit event handler in BigWigs that basically keeps a pool of frames for each unit and calls RegisterUnitEvent on the corresponding frame. I made a patch for AceEvent way back using this approach, but we decided it was a bit much to add.

Last edited by nebula : 10-11-16 at 01:09 AM.
  Reply With Quote
10-11-16, 06:15 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Even if PLAYER_SPECIALIZATION_CHANGED fires like a unit event now, if you're currently using AceEvent, it's not worth rolling your own system so you can use RegisterUnitEvent with it. Just filter manually, or continue to treat it like a normal event; it's not going to fire enough to matter either way.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » AceEvent library and RegisterUnitEvent

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