View Single Post
03/17/23, 07:19 PM   #8
nilo
Join Date: Feb 2022
Posts: 15
Originally Posted by Baertram View Post
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.
I see

Originally Posted by Baertram View Post
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.
Ok I'll try to do that

Originally Posted by Baertram View Post
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?
I see, it's not intended to only fire once, it's on each role change yes

Originally Posted by Baertram View Post
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!
Interesting, I didn't know about the eventId part

Last edited by nilo : 03/17/23 at 07:44 PM.
  Reply With Quote