Hello everyone,
I was diving deep into the subject of events, not to use them as the default, but to make an alternate implementation.
During my research, I discovered that a method called SetEventRoutine exists in the Citizen object and is used by default in server files that build the environment for resources. (fivem/scheduler.lua at 29fc26752ab72805f53a2c489fc46094ca1f8714 · citizenfx/fivem · GitHub link will highlight the specified function and his content)
Since all the methods are not necessarily present in the environment of the resource. I checked if the founded function exist into resources environnement, and if it could be called, and I tried to use it
--- @param eventName string name of the received event
--- @param payload string stringified and message packed payload
--- @param netId string stringified network client identifier
local function EventDispatchRoutine(eventName, payload, netId)
print(("^1Event '^3%s^1' received from netId '^3%s^1'^7"):format(eventName, netId))
-- Custom event dispatch here
end
Citizen.SetEventRoutine(EventDispatchRoutine)
It’s seem’s the function exist, can be called, but … it seem that the call to this method is not taken into account.
And then, i was wondering… is it a method wich could be called only once ? or not ? So i remove the call to the function made by default in Cfx code when creating resource env, and test again the same code without any modification in my resource. And surprisingly… it works and my custom event dispatch function was called !
So… I’m wondering… is this a desired mechanic by Cfx? or a bug? And, If it is a desired mechanic , Is it possible to make it evolve ? and to allow the call of SetEventRoutine for overloading with our own events managing process ? or the integration of a way to register middleware function to allow a pre-processing of events ?
Thanks in advance for anyone who will respond
Have a great day