I know of the popular frameworks’ callbacks and I have found resources such as this but just wondering if a native callback system exists for returning values from server ~> client.
If not, does everyone just trigger a server event, do their work and end by triggering a client event back at the source?
I don’t want to add a dependency to what I’m doing, rather have the script handle it without.
Not at this time. There was a brief time when this was implemented by allowing functions to be passed in events, but this was fundamentally flawed, and when a memory leak in function-passing was fixed, this RPC bit broke as well.
The client/server callback code seen in ESX/QBCore and similar is very simple to remake, though, it’d be around 20-30 lines of actual code to track callbacks by a ‘reply ID’ in a similar way to how e.g. JSON-RPC would work for asynchronous requests, with back-and-forth event sending.
I was looking at ESX’s code and all I could find in it was:
CLIENT
function ESX.TriggerServerCallback(name, cb, ...)
Core.ServerCallbacks[Core.CurrentRequestId] = cb
TriggerServerEvent('esx:triggerServerCallback', name, Core.CurrentRequestId, ...)
if Core.CurrentRequestId < 65535 then
Core.CurrentRequestId = Core.CurrentRequestId + 1
else
Core.CurrentRequestId = 0
end
end
SERVER
function ESX.RegisterServerCallback(name, cb)
Core.ServerCallbacks[name] = cb
end
function ESX.TriggerServerCallback(name, requestId, source, cb, ...)
if Core.ServerCallbacks[name] then
Core.ServerCallbacks[name](source, cb, ...)
else
print(('[^3WARNING^7] Server callback ^5"%s"^0 does not exist. ^1Please Check The Server File for Errors!'):format(name))
end
end
Is it just creating a request id, then passing that id back and forth to keep track of the callback in question?