Clarification on callback function use

I’m puzzled about event handlers after some code I recently found. So there’s no way to ‘return’ data from an AddEventHandler block and I’ve seen so many patterns where people triggerserverevent and return the data with a triggerclientevent. It ends up being disconnected code and the flow is lost but that’s how I thought things were done. So ESX and QBCore have functions to “register callbacks” and I’ve even seen another resource which provides server callbacks for when you don’t have either of those RP frameworks.

Going through code working out how to add a new esx_license type I see a callback function being specified as an argument to a server trigger.

Not expecting this to be a thing I checked the eventhandler in esx_license and see this


I get that a straight function would cater for callbacks but why would the parent event handler cater for it (assuming my understanding they don’t work is correct).

So if invoking a callback to a client trigger DOES in fact work then what is the point of having ESX.RegisterServerCallback and QBCore.Functions.CreateCallback ??

Huh? This isn’t ‘a server trigger’, it’s a local ‘trigger’.

Well, If I understand this correctly(And I think I do) then it depends. ESX Server Callback here in esx_license is not a great idea(from your perspective of course) but! Let me explain how it works.

First thing. AddEventHandler[or just events in general] have callbacks implemented in them but they are only one sided. So if you want to receive callback from server to client you must use one of those callbacks from ESX or QBCore. But if you want to use callback from the same side then FiveM has already implemened it! Here is example:

AddEventHandler(‘testevent:callback’, function(p1, p2, cb)
if p1 == nil then
p1 = “”
end
if p2 == nil then
p2 = “”
end
cb(p1)
end)

RegisterCommand(‘testcommand’, function(source, args)
TriggerEvent(‘testevent:callback’, args[1], args[2], function(param)
print(param)
end)
end)

This example here should work(but is obviously shitty tbh). And in license like you showed(esx_weaponshop I know) it works on callback since it sends data back to client if Player has purchased license or not. It’s not really effective(optimization wise) But its doing its job and its important!

And also here is the answer for your last question:

I haven’t looked in esx logic behind callbacks very much. But it works and it does not break your script in any way. But you cannot use callbacks in FiveM by default for client => server and vice versa. And ESX does this manually so this is the final Answer. In case of any questions Just Answer to this reply!