Last active
October 16, 2020 15:38
-
-
Save DemmyDemon/3ae668db9484bd8e590744b71ede577a to your computer and use it in GitHub Desktop.
How server callbacks are done in Night City
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local on_server = IsDuplicityVersion() | |
local resource = GetCurrentResourceName() | |
local callbacks = {} | |
local eventName = resource..':night-callback' | |
if on_server then | |
local callbacks = {} | |
function DefineNightCallback(name, funcref) | |
if callbacks[name] then | |
return false | |
end | |
callbacks[name] = funcref | |
return true | |
end | |
RegisterNetEvent(eventName) | |
AddEventHandler (eventName, function(name, id, ...) | |
local src = source | |
if callbacks[name] then | |
TriggerClientEvent(eventName, src, name, id, callbacks[name](src, ...)) | |
else | |
print( | |
('[%s] [%s] [%s %s] requested invalid NightCallback: %s\n'):format( | |
os.date("%H:%M:%S"), | |
resource, | |
tostring(src), | |
tostring(GetPlayerName(src)), | |
tostring(name) | |
) | |
) | |
TriggerClientEvent(eventName, src, name, id) | |
end | |
end) | |
else | |
local callbackID = 0 | |
local pending = {} | |
function NightCallback(name, funcref, ...) | |
callbackID = callbackID + 1 | |
callbacks[callbackID] = funcref | |
while pending[name] do | |
Citizen.Wait(0) | |
end | |
pending[name] = true | |
TriggerServerEvent(eventName, name, callbackID, ...) | |
end | |
exports('NightCallback', NightCallback) | |
RegisterNetEvent(eventName) | |
AddEventHandler (eventName, function(name, id, ...) | |
if callbacks[id] then | |
callbacks[id](...) | |
end | |
pending[name] = nil | |
callbacks[id] = nil | |
end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment