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:
function LGRI.UpdateMyRole(eventId, roleId) --watch the 1st param always is eventIf for event callbacks!!!
local my = LGRI.my
-- Role
if roleId == 1 then
my.roleIcon = "esoui/art/lfg/lfg_icon_dps.dds"
LGRIRoleIcon:SetTexture(my.roleIcon)
elseif roleId == 2 then
my.roleIcon = "esoui/art/lfg/lfg_icon_tank.dds"
LGRIRoleIcon:SetTexture(my.roleIcon)
elseif roleId == 4 then
my.roleIcon = "esoui/art/lfg/lfg_icon_healer.dds"
LGRIRoleIcon:SetTexture(my.roleIcon)
else
my.roleIcon = "esoui/art/armory/builditem_icon.dds"
LGRIRoleIcon:SetTexture(my.roleIcon)
end
local myNewRole = GetGroupMemberSelectedRole("player")
LGRI.my.roleId = myNewRole;
--zo_callLater(function() LGRI.callbackForRoleChange(myNewRole) end, 100)
--Do not unregister here!
--evm:UnregisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED)
end
function LGRI.OnAddOnLoaded(event, addonName)
if addonName ~= LGRI.name then return end
evm:UnregisterForEvent(LGRI.name, EVENT_ADD_ON_LOADED)
LargeGroupRoleIcons.Initialize()
evm:RegisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED, LGRI.UpdateMyRole)
evm:AddFilterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED, REGISTER_FILTER_UNIT_TAG, "player")
end
SLASH_COMMANDS["/lgri"] = LGRI.HideANDShowIcons
evm:RegisterForEvent(LGRI.name, EVENT_ADD_ON_LOADED, LGRI.OnAddOnLoaded)
--[[ -- not needed!
function LGRI.callbackForRoleChange(newRole)
evm:RegisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED, LGRI.UpdateMyRole(newRole))
end
]]
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!