Usage
The Callback module provides a custom promise-based RPC (Remote Procedure Call) system for communication between client and server.
Overview
The callback system allows you to:
- Call server functions from client and get return values
- Call client functions from server and get return values
- Use both synchronous (blocking) and asynchronous (non-blocking) patterns
- Build clean, maintainable client-server communication
Server Functions
register
Registers a callback that can be called from the client.
FM.callback.register(eventName, callback)
Unique name for the callback
Function(src, ...) to execute when called. Must return value(s).
Example:
-- Server-side registration
FM.callback.register('getPlayerMoney', function(src, moneyType)
local player = FM.player.get(src)
if not player then return 0 end
return player.getMoney(moneyType or 'money')
end)
FM.callback.register('canAfford', function(src, amount)
local player = FM.player.get(src)
if not player then return false end
return player.getMoney('bank') >= amount
end)
FM.callback.register('getOnlineCount', function(src, jobName)
if jobName then
return FM.framework.getPlayers({ job = jobName, count = true })
else
return FM.framework.getPlayers({ count = true })
end
end)
async
Makes an asynchronous callback request to a client.
FM.callback.async(eventName, src, callback, ...)
Name of the registered client callback
Player source ID
Function to execute with the response
Arguments to pass to the client callback
Example:
-- Server-side async call
FM.callback.async('getVehicleInFront', src, function(vehicleData)
if vehicleData then
print("Vehicle model:", vehicleData.model)
print("Vehicle plate:", vehicleData.plate)
else
print("No vehicle found")
end
end)
sync
Makes a synchronous callback request to a client (blocks until response).
FM.callback.sync(eventName, src, ...)
Name of the registered client callback
Player source ID
Arguments to pass to the client callback
Returns: Response value(s) from the client
Example:
-- Server-side sync call
local coords = FM.callback.sync('getPlayerCoords', src)
if coords then
print("Player is at:", coords.x, coords.y, coords.z)
end
local hasWeapon = FM.callback.sync('hasWeaponEquipped', src)
if hasWeapon then
print("Player has weapon equipped")
end
Client Functions
register
Registers a callback that can be called from the server.
FM.callback.register(eventName, callback)
Unique name for the callback
Function(...) to execute when called. Must return value(s).
Example:
-- Client-side registration
FM.callback.register('getPlayerCoords', function()
return GetEntityCoords(PlayerPedId())
end)
FM.callback.register('hasWeaponEquipped', function()
local ped = PlayerPedId()
return GetSelectedPedWeapon(ped) ~= GetHashKey("WEAPON_UNARMED")
end)
FM.callback.register('getVehicleInFront', function()
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
local forward = GetEntityForwardVector(ped)
local rayHandle = StartShapeTestRay(
coords.x, coords.y, coords.z,
coords.x + forward.x * 5.0,
coords.y + forward.y * 5.0,
coords.z + forward.z * 5.0,
10, ped, 0
)
local _, hit, _, _, vehicle = GetShapeTestResult(rayHandle)
if hit and DoesEntityExist(vehicle) then
return {
model = GetEntityModel(vehicle),
plate = GetVehicleNumberPlateText(vehicle),
entity = vehicle
}
end
return nil
end)
async
Makes an asynchronous callback request to the server.
FM.callback.async(eventName, callback, ...)
Name of the registered server callback
Function to execute with the response
Arguments to pass to the server callback
Example:
-- Client-side async call
FM.callback.async('getPlayerMoney', function(money)
print("You have $" .. money)
end, 'bank')
FM.callback.async('canAfford', function(canAfford)
if canAfford then
print("You can afford this!")
else
print("Not enough money")
end
end, 5000)
sync
Makes a synchronous callback request to the server (blocks until response).
FM.callback.sync(eventName, ...)
Name of the registered server callback
Arguments to pass to the server callback
Returns: Response value(s) from the server
Example:
-- Client-side sync call
local money = FM.callback.sync('getPlayerMoney', 'bank')
print("Bank balance: $" .. money)
local onlineCount = FM.callback.sync('getOnlineCount')
print("Players online:", onlineCount)
local policeCount = FM.callback.sync('getOnlineCount', 'police')
print("Police online:", policeCount)
Exports
Callback functions are available as exports:
-- Server
exports['fmLib']:callback_register(eventName, callback)
exports['fmLib']:callback_async(eventName, src, callback, ...)
exports['fmLib']:callback_sync(eventName, src, ...)
-- Client
exports['fmLib']:callback_register(eventName, callback)
exports['fmLib']:callback_async(eventName, callback, ...)
exports['fmLib']:callback_sync(eventName, ...)
