Go to Page... |
Thread Tools | Display Modes |
03/17/23, 12:02 PM | #1 | |
Join Date: Feb 2022
Posts: 17
|
Questions about events, RegisterForEvent, RegisterForUpdate, ..
Atm I have a texture on screen when my role = LFG_ROLE_INVALID and this texture changes when I enter group according to my selected role. You can see in the ss how I'm doing it atm, however I want to do it more efficiently. I want to find a way to only UpdateMyRole when event EVENT_GROUP_MEMBER_ROLES_CHANGED is detected, I've tried a couple different ways using RegisterForEvent and RegisterForUpdate but no sucess and I can't find good info on how to use this and events in general.
Also I would like to know how impactful it is to game performance to use this "zo_callLater(function() LGRI.UpdateMyRole(myNewRole) end, 100)" as you can see inside my function LGRI.UpdateMyRole(roleId), and if it's good practice or not |
|
03/17/23, 12:54 PM | #2 |
RegisterForEvent is used with the EVENT_* constants and that events fire automatically by the game at some defined trigger.
You register a callback fnction hat is executed as the event triggers by the game. RegisterForUpdate is a kind of a repeated check every n milliseconds where a callback function that you define is called each time. So this is done to repetivle do checks every n milliseconds, like an OnUpdate task during moving controls on the screen, or check every n milliseconds if you are grouped etc. (-> makes not sense as if you get grouped or leave group there are events that fire and you can react on them instead of constantly check every n milliseconds something that cannot change without those events having fired). zo_callLater is using RegisterForUpdate and calls your callback function the delay later that you have defined. In some circumstances it's okay to use it as some data will not be updated "instantly", but as events fire the data of the events should be fine and a zo_callLater should not be needed there (unless you check anything that is not related to the particular event). Perfomance wise it's always bet to use events, and register event filters (if possible) as they will be called at C code, before the lua event even triggers. If the filter say "event is not needed for you" it's the best as no lua code of your callback function will be called at all! (e.g. in combat/buff events filter for unit tag "player" to only see ylur combat related stuff, and strip other group player's or enemy cobmat/buff stuff). Last edited by Baertram : 03/17/23 at 12:58 PM. |
|
03/17/23, 01:01 PM | #3 |
Events: https://wiki.esoui.com/Events
Event filters: https://wiki.esoui.com/AddFilterForEvent Registering your event callback function: Lua Code:
Unregistering your event callback function if not needed anymore Lua Code:
|
|
03/17/23, 05:09 PM | #4 | ||
Join Date: Feb 2022
Posts: 17
|
I have some more questions tho, in the attached ss I submited there are 3 independent scenarios: A is just what I had before, C is the changes I made I would like to know if this is what you meant I should do in your previous answer, and B is just a curious attempt however whenever I /reloadui or log in using it I can't get past the loading screen and game freezes, I'm curious of what might be the reason for this happening (And just to clarify I'm not running this 3 versions simultaneously, I just put them there for visual representation ) |
||
03/17/23, 05:15 PM | #5 |
Could you please show your total code, and no screenshots? It's ahrd to see what is wrong if we do not have ALL addon files that you are calling (especially thx txt "manifes" file can create issues too!)
You can attach zip files to the posts too afaik and if this does not work try to just paste the code between the lua highlight tags, or post a link to github/gist/whatever code sharing platform you prefer. Basically you need to do EVENT_MANAGER:RegisterForEvent("myAddonName", EVENT_GROUP_MEMBER_ROLE_CHANGED, yourCallbackFunctionForThatEvent) within your EVENT_ADD_ON_LOADED callback function, without any zo_callLater! This will register yourCallbackFunctionForThatEvent for that event, whenever it fires (should fire each time a group member changes his role). And if you want to update your role texture once at EVENT_ADD_ON_LOADED you need to do that manually, by either calling your function yourCallbackFunctionForThatEvent once (if that was designed to work without the event data which the event callback would pass in as parameters!) or by other means. And EVENT_MANAGER:UnregisterForEvent is not needed unless you want to explicitly unregister events because they are not needed any longer (eg. if you disable a setting about that group role update -> unregister the events for that too so they do not fire any longer the callback functions. And if you enable the setting -> Register the events again). btw: You got a typo in your event name! EVENT_GROUP_MEMBER_ROLE_CHANGED without S after ROLE Last edited by Baertram : 03/17/23 at 05:22 PM. |
|
03/17/23, 06:48 PM | #6 | ||
Join Date: Feb 2022
Posts: 17
|
and thank you so much for this Last edited by nilo : 03/20/23 at 10:47 AM. |
||
03/17/23, 07:09 PM | #7 |
Code:
evm:RegisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_ROLE_CHANGED, LGRI.UpdateMyRole(newRole)) 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) -> 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:
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. |
|
03/17/23, 07:19 PM | #8 | |||||
Join Date: Feb 2022
Posts: 17
|
Last edited by nilo : 03/17/23 at 07:44 PM. |
|||||
03/18/23, 06:10 PM | #9 | |
Join Date: Feb 2022
Posts: 17
|
Ok so now I have a new problem, I'm successfully calling my UpdateMyRole function whenever EVENT_GROUP_MEMBER_ROLE_CHANGED triggers, however I need smth to call this function whenever I stop being in a group and my roleId changes to LFG_ROLE_INVALID.
In order to fix this I tried doing this Code:
EM:RegisterForEvent(LGRI.name, EVENT_GROUP_MEMBER_LEFT, LGRI.UpdateMyRole) EM:AddFilterForEvent(EVENT_GROUP_MEMBER_LEFT, ????????? , string memberDisplayName) https://wiki.esoui.com/AddFilterForEvent https://wiki.esoui.com/EVENT_GROUP_MEMBER_LEFT also I just found this here "isLocalPlayer - simple boolean True if it is the player, false for other group members." I guess I could use this too but again, i can't find which filter type to use Last edited by nilo : 03/20/23 at 10:48 AM. |
|
03/19/23, 07:01 AM | #10 |
You do not always need to filter events, and beside that you cannot even filter each event! The filters are helpers to prevent event callbacks firing each time for unneccessary circumstances, like if you only want to check yourself but the event fires for all units around you.
Some events do not even support filters as the Wiki describes, e.g. if there is no unitTag in the event's callback function parameters you cannot use any unitTag related event filters for it. Same counts for displayname and I think there does not even exist and event filter stuff for displaynames -> check the event filter site at the wiki. If the callback function of the event already provides a parameter like isLocalPlayer and it's true if YOU left the group, then just add the EVENT_MANAGER:RegisterForEvent... of the group_left and check in your callback function at the start if it's you who left, or others. e.g. if only you are the relevant unit to check things with you can skip all others simply by: Lua Code:
Lua Code:
Last edited by Baertram : 03/19/23 at 07:06 AM. |
|
03/19/23, 01:09 PM | #11 | ||
Join Date: Feb 2022
Posts: 17
|
This is not working for me tho Lua Code:
Lua Code:
And also one extra question, I want to register another event(EVENT_GROUP_MEMBER_JOINED) to trigger my UpdateMyRole() function, what's the correct way? Lua Code:
Last edited by nilo : 03/19/23 at 01:11 PM. |
||
03/19/23, 01:12 PM | #12 |
Event register and unregister always is 1 event only, not multiple within one call.
And the event filters should work like I had added my code above, with the first para = the same unique name you have used to register your event with. You can register multiple filters with the same unique name, as long as the filtertype is different (e.g. REGISTER_FILTER_POWER_TYPE and REGISTER_FILTER_UNIT_TAG works, but 2x REGISTER_FILTER_UNIT_TAG won't work with the same unique name). Example: Code:
EVENT_MANAGER:RegisterForEvent("ZO_HealthWarning", EVENT_POWER_UPDATE, OnPowerUpdate) EVENT_MANAGER:AddFilterForEvent("ZO_HealthWarning", EVENT_POWER_UPDATE, REGISTER_FILTER_POWER_TYPE, COMBAT_MECHANIC_FLAGS_HEALTH) EVENT_MANAGER:AddFilterForEvent("ZO_HealthWarning", EVENT_POWER_UPDATE, REGISTER_FILTER_UNIT_TAG, "player") e.g. d(">unitTag: " ..tostring(unitTag)) Perhas the group related event filters only accept unitTags that start with group. e.g. the unitTag start at group1 and count up to groupn. Maybe "player" is never send to the event EVENT_GROUP_MEMBER_ROLE_CHANGED then? You can remove the filter and test what the unitTag is like as you yourself change the role (via the d debug message). Maybe it will be group2 or similar then and that's why it won't work with "player". In this case leave the event filter away and check manuallyif the group unitTag passed to the callback function parameter is the one of yourself. You should be able to detect that via GetUnitDisplayName(unitTag) and comparing the displayName with GetDisplayName() result (cache that once at the start of your addon as it would not change in between during gameplay). Last edited by Baertram : 03/19/23 at 01:18 PM. |
|
03/19/23, 01:17 PM | #13 | |||
Join Date: Feb 2022
Posts: 17
|
I'll check it later when I have more time |
|||
03/19/23, 01:19 PM | #14 |
I've edited my answer above.
|
|
03/19/23, 03:00 PM | #15 | ||
Join Date: Feb 2022
Posts: 17
|
this returns my index Lua Code:
So I'm thinking of doing this Lua Code:
Last edited by nilo : 03/19/23 at 03:28 PM. |
||
03/19/23, 03:34 PM | #16 |
If the unitTag is always nil, there either is a bug in that event or your parameter order of that event is wrong so that you do not actually check the unitTag but some other param which returns nil. Did you add the 1st parameter eventId in your callback?
>this returns my index And what is your index then? if it's 4294967296 it's wrong Should be something like 1, 2, 3 etc. You cannot use a function in the event filters! Code:
EM:AddFilterForEvent(LGRI.name .. "MyRoleChanged", EVENT_GROUP_MEMBER_ROLE_CHANGED, REGISTER_FILTER_UNIT_TAG, function (unitTag) That event filter would only work with something like this: Code:
EM:AddFilterForEvent(LGRI.name .. "MyRoleChanged", EVENT_GROUP_MEMBER_ROLE_CHANGED, REGISTER_FILTER_UNIT_TAG, "group1") And this would neither work or at least is not needed. GetGroupUnitTagByIndex should always return "group" .. <number> so it cannot be anything else than group..index as you have passed in the index. Lua Code:
Your functin function LGRI.UpdateMyRole(eventId) needs the unitTag as 2nd parameter and then you can use that function properly. Lua Code:
Last edited by Baertram : 03/19/23 at 03:53 PM. |
|
03/19/23, 03:40 PM | #17 | |
Join Date: Feb 2022
Posts: 17
|
yes, I have eventId as 1st parameter
and my index was 2 |
|
03/19/23, 03:44 PM | #18 |
Updated my answer above, the code at the bottom should be the one you are searching for (no event filter, just unitTag check for your own tag).
Edit: Updated it again and changed the unitTag == nil within LGRI.UpdateMyRole to a ~= nil check, so hat your call from EM:RegisterForEvent(LGRI.name .. "ILeftGroup", EVENT_GROUP_MEMBER_LEFT, still works. Last edited by Baertram : 03/19/23 at 03:54 PM. |
|
03/19/23, 04:27 PM | #19 | |
Join Date: Feb 2022
Posts: 17
|
Yep makes sense, thanks
|
|
ESOUI » Developer Discussions » Lua/XML Help » Questions about events, RegisterForEvent, RegisterForUpdate, .. |
«
Previous Thread
|
Next Thread
»
|
Thread Tools | |
Display Modes | |
|
|