Zum Inhalt

Callbacks

Short reference for using cv.callback, cv.callback.await, and cv.callback.register.


Concepts

  • cv.callback triggers a remote request and handles the response in a separate coroutine.
  • cv.callback.await yields the current coroutine until a response is received and returns the result.
  • cv.callback.register registers a handler that responds to remote callback requests.

Notes:

  • The delay parameter (client-only) can be a number in milliseconds or false. 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:

cv.callback.trigger('cv:getPlayerData', false, function(data)
    print(data)
end, 'balance')

Await a server response:

local balance = cv.callback.await('cv:getPlayerData', false, 'balance')
print(balance)

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)