WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   AceEvent library and RegisterUnitEvent (https://www.wowinterface.com/forums/showthread.php?t=54627)

Layback_ 10-10-16 01:17 AM

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?

Phanx 10-10-16 03:27 AM

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


Layback_ 10-10-16 04:23 AM

Quote:

Originally Posted by Phanx (Post 319803)
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 :banana:

Thank you again!!

Ketho 10-10-16 07:04 AM

You probably saw this thread, for reference
https://forums.wowace.com/showthread.php?t=20124

Layback_ 10-10-16 04:17 PM

Quote:

Originally Posted by Ketho (Post 319810)
You probably saw this thread, for reference
https://forums.wowace.com/showthread.php?t=20124

You are totally right, haha :D

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

nebula 10-10-16 10:40 PM

Quote:

Originally Posted by Phanx (Post 319803)
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.

Phanx 10-11-16 06:15 PM

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.


All times are GMT -6. The time now is 07:44 AM.

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