From cd16955a4f771c15d145977612f1c8a2916d06a5 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sun, 18 Sep 2022 16:44:04 +1000 Subject: [PATCH] ox_lib: c->s and s->c callbacks --- docs/ox_lib/Callback/Client.md | 40 ++++++++++++++++++++------ docs/ox_lib/Callback/Server.md | 51 ++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 10 deletions(-) diff --git a/docs/ox_lib/Callback/Client.md b/docs/ox_lib/Callback/Client.md index c20c5772..4267115a 100644 --- a/docs/ox_lib/Callback/Client.md +++ b/docs/ox_lib/Callback/Client.md @@ -1,14 +1,17 @@ -Triggers a registered server callback and registers an event handler for the response. +## Client -> Server callback ### lib.callback + The response is handled in a separate coroutine. + ```lua lib.callback(name, delay, cb, ...) ``` -* name: `string` -* delay: `number` or `false` -* cb: `function` -* ...: `any` + +- name: `string` +- delay: `number` or `false` +- cb: `function` +- ...: `any` ```lua lib.callback('ox_inventory:getItemCount', false, function(count) @@ -17,15 +20,36 @@ end, 'water', {type = 'fresh'}) ``` ### lib.callback.await + The current coroutine is yielded until a response is received. + ```lua lib.callback.await(name, delay, ...) ``` -* name: `string` -* delay: `number` or `false` -* ...: `any` + +- name: `string` +- delay: `number` or `false` +- ...: `any` ```lua local count = lib.callback.await('ox_inventory:getItemCount', false, 'water', {type = 'fresh'}) print(count) ``` + +## Server -> Client callback + +### lib.callback.register + +```lua +lib.callback.register(name, cb) +``` + +- name: `string` +- cb: `function` + +```lua +lib.callback.register('ox:getNearbyVehicles', function(radius) + local nearbyVehicles = lib.getNearbyVehicles(GetEntityCoords(cache.ped), radius, true) + return nearbyVehicles +end +``` diff --git a/docs/ox_lib/Callback/Server.md b/docs/ox_lib/Callback/Server.md index ebd29dec..c0140c14 100644 --- a/docs/ox_lib/Callback/Server.md +++ b/docs/ox_lib/Callback/Server.md @@ -1,9 +1,56 @@ +## Server -> Client callback + +### lib.callback + +The response is handled in a separate coroutine. + +```lua +lib.callback(name, source, cb, ...) +``` + +- name: `string` +- source: `number` +- cb: `function` +- ...: `any` + +```lua +lib.callback('ox:getNearbyVehicles', source, function(vehicles) + for i = 1, #vehicles do + DeleteEntity(entity) + end +end, args.radius) +``` + +### lib.callback.await + +The current coroutine is yielded until a response is received. + +```lua +lib.callback.await(name, source, ...) +``` + +- name: `string` +- source: `number` +- ...: `any` + +```lua +local vehicles = lib.callback.await('ox:getNearbyVehicles', source, args.radius) + +for i = 1, #vehicles do + DeleteEntity(entity) +end +``` + +## Client -> Server callback + ### lib.callback.register + ```lua lib.callback.register(name, cb) ``` -* name: `string` -* cb: `function` + +- name: `string` +- cb: `function` ```lua lib.callback.register('ox_inventory:getItemCount', function(source, item, metadata, target)