Callbacks¶
Short reference for using cv.callback, cv.callback.await, and cv.callback.register.
Concepts¶
cv.callbacktriggers a remote request and handles the response in a separate coroutine.cv.callback.awaityields the current coroutine until a response is received and returns the result.cv.callback.registerregisters a handler that responds to remote callback requests.
Notes:
- The
delayparameter (client-only) can be a number in milliseconds orfalse. When a number is provided, it acts as a cooldown before the same callback can be triggered again. - On the client, the first argument to registered handlers is the payload; on the server, the first argument is the
source(player ID). - Always validate player context on the server before performing character operations.
Client¶
Trigger a server callback:
Await a server response:
Register a client callback to respond to server requests:
cv.callback.register('cv:getNearbyPlayers', function(radius)
local nearbyPlayers = lib.getNearbyEntities(GetEntityCoords(cache.ped), radius, false)
return nearbyPlayers
end)
Server¶
Trigger a client callback:
cv.callback('cv:getNearbyPlayers', playerId, function(players)
for i = 1, #players do
-- Process nearby players
end
end, args.radius)
Await a client response:
local players = cv.callback.await('cv:getNearbyPlayers', playerId, args.radius)
for i = 1, #players do
-- Process nearby players
end
Register a server callback to respond to client requests:
cv.callback.register('cv:getPlayerData', function(source, dataType)
local player = CVPlayer.GetPlayer(source)
if not player or not player.charId then
return nil -- Return nil or appropriate default
end
if dataType == 'balance' then
return player:getMoney('cash')
elseif dataType == 'job' then
return player.job
end
return nil
end)