View Single Post
03/17/23, 07:09 PM   #7
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,000
Code:
evm:RegisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED, LGRI.UpdateMyRole(newRole))
This des not work as it will only execute the function LGRI.UpdateMyRole(newRole) at the time the interpreter is running the code once, and at that time the parameter "newRole"of the event function is nil -> will lead to a missing role then and thus uses the unknown I guess.

You need to put such functions in an anonymous function so that this anony function is called at the time the event fires, and not at the time the code is interpreted.

Code:
evm:RegisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED, function() LGRI.UpdateMyRole(newRole) end)
And if you call that in your callback function of EVENT_GROUP_MEMBER_ROLE_CHANGED
-> evm:UnregisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED)
It will make the event EVENT_GROUP_MEMBER_ROLE_CHANGED only be called once and then it won't fire for your addon anymore.
Is this intended to only be fired once? I'd say it should fire on EACH role change, no matter how often you do it?

Also move the filter to your event_Add_on_loaded function where you register the event!
-> evm:AddFilterForEvent(EVENT_GROUP_MEMBER_ROLE_CHANGED, REGISTER_FILTER_UNIT_TAG_PREFIX, "player")

Lua Code:
  1. function LGRI.UpdateMyRole(eventId, roleId) --watch the 1st param always is eventIf for event callbacks!!!
  2.     local my = LGRI.my
  3.  
  4.     -- Role
  5.     if roleId == 1 then
  6.         my.roleIcon = "esoui/art/lfg/lfg_icon_dps.dds"
  7.         LGRIRoleIcon:SetTexture(my.roleIcon)
  8.  
  9.     elseif roleId == 2 then
  10.         my.roleIcon = "esoui/art/lfg/lfg_icon_tank.dds"
  11.         LGRIRoleIcon:SetTexture(my.roleIcon)
  12.     elseif roleId == 4 then
  13.         my.roleIcon = "esoui/art/lfg/lfg_icon_healer.dds"
  14.         LGRIRoleIcon:SetTexture(my.roleIcon)
  15.     else
  16.         my.roleIcon = "esoui/art/armory/builditem_icon.dds"
  17.         LGRIRoleIcon:SetTexture(my.roleIcon)
  18.     end
  19.  
  20.     local myNewRole = GetGroupMemberSelectedRole("player")
  21.     LGRI.my.roleId = myNewRole;
  22.  
  23.     --zo_callLater(function() LGRI.callbackForRoleChange(myNewRole) end, 100)
  24.       --Do not unregister here!
  25.     --evm:UnregisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED)
  26. end
  27.  
  28. function LGRI.OnAddOnLoaded(event, addonName)
  29.     if addonName ~= LGRI.name then return end
  30.     evm:UnregisterForEvent(LGRI.name, EVENT_ADD_ON_LOADED)
  31.  
  32.     LargeGroupRoleIcons.Initialize()
  33.  
  34.     evm:RegisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED, LGRI.UpdateMyRole)
  35.     evm:AddFilterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED, REGISTER_FILTER_UNIT_TAG, "player")
  36. end
  37.  
  38. SLASH_COMMANDS["/lgri"] = LGRI.HideANDShowIcons
  39.  
  40. evm:RegisterForEvent(LGRI.name, EVENT_ADD_ON_LOADED, LGRI.OnAddOnLoaded)
  41.  
  42. --[[ -- not needed!
  43. function LGRI.callbackForRoleChange(newRole)
  44.     evm:RegisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED, LGRI.UpdateMyRole(newRole))
  45. end
  46. ]]

Also watch out for the event callback functions, the 1st param always is the eventId!!! That's why you pass in a number like 32456 as the roleId which makes it fail!

Last edited by Baertram : 03/17/23 at 07:15 PM.
  Reply With Quote