### Install ox_lib with Yarn Source: https://github.com/overextended/ox_lib/blob/main/package/README.md Install the ox_lib package using Yarn. ```yaml # With Yarn yarn add @overextended/ox_lib ``` -------------------------------- ### Install ox_lib with npm Source: https://github.com/overextended/ox_lib/blob/main/package/README.md Install the ox_lib package using npm. ```yaml # With npm npm install @overextended/ox_lib ``` -------------------------------- ### Create Thread with Wait Source: https://github.com/overextended/ox_lib/wiki/Intervals This snippet demonstrates creating a thread that repeatedly prints 'hello world' with a 0-millisecond wait. This is a basic Lua threading example. ```lua CreateThread(function() while true do print('hello', 'world') Wait(0) end end) ``` -------------------------------- ### Install ox_lib with pnpm Source: https://github.com/overextended/ox_lib/blob/main/package/README.md Install the ox_lib package using pnpm. ```yaml # With pnpm pnpm add @overextended/ox_lib ``` -------------------------------- ### Import callbacks using import function Source: https://github.com/overextended/ox_lib/wiki/Home Use the import function to load modules similarly to require, supporting lazy-loading. This example imports the 'callbacks' module. ```lua local ServerCallback = import 'callbacks' ``` -------------------------------- ### Include pe-lualib in fxmanifest.lua Source: https://github.com/overextended/ox_lib/wiki/Home Add this line before any other files in your fxmanifest.lua to include the pe-lualib. ```lua shared_script '@pe-lualib/init.lua' ``` -------------------------------- ### Import ox_lib Server Source: https://github.com/overextended/ox_lib/blob/main/package/README.md Import the ox_lib server library in your TypeScript files. ```typescript import lib from '@overextended/ox_lib/server'; ``` -------------------------------- ### Import ox_lib Client Source: https://github.com/overextended/ox_lib/blob/main/package/README.md Import the ox_lib client library in your TypeScript files. ```typescript import lib from '@overextended/ox_lib/client'; ``` -------------------------------- ### Awaiting a Server Callback with a Promise Source: https://github.com/overextended/ox_lib/wiki/Callbacks Triggers a server event and allows the client to await the response using a promise-like mechanism. Includes a timeout. ```APIDOC ## Await ### Description Triggers a server event and returns a promise that resolves with the server's response. Includes a timeout. ### Method `ServerCallback.Await(resourceName, eventName, timeout, ...)` ### Parameters - **resourceName** (string) - Required - The name of the resource where the server callback is registered. - **eventName** (string) - Required - The name of the event to trigger. - **timeout** (number) - Required - The time in milliseconds to wait for a response before timing out. - **...** (any) - Optional - Additional arguments to pass to the server event. ### Returns - **(any, any, ...)** - The values returned by the server callback. ### Example ```lua CreateThread(function() local a, b = ServerCallback.Await('resourceName', 'eventName', 100, 'hello there') print(a, b) -- 'hello there', 'general kenobi' end) ``` ``` -------------------------------- ### Utilize import.callbacks directly Source: https://github.com/overextended/ox_lib/wiki/Home An alternative way to import modules using the import function, specifically accessing 'callbacks' directly. ```lua -- or you can utilise import.callbacks directly ``` -------------------------------- ### Registering a Server Callback Source: https://github.com/overextended/ox_lib/wiki/Callbacks Registers a server event that can be triggered by clients. The callback function receives the source of the event and any data passed with it. It can return values that will be sent back to the client. ```APIDOC ## Register ### Description Registers a server-side callback function for a given event name. ### Method `ServerCallback.Register(eventName, callbackFunction)` ### Parameters - **eventName** (string) - Required - The name of the event to register. - **callbackFunction** (function) - Required - The function to be executed when the event is triggered. It receives `source` and `data` as arguments and can return values. ### Example ```lua local ServerCallback = import 'callbacks' ServerCallback.Register('eventName', function(source, data) return data, 'general kenobi' end) ``` ``` -------------------------------- ### Register Server Callback Source: https://github.com/overextended/ox_lib/wiki/Callbacks Registers a server event that can be triggered by clients. The callback function receives the source of the event and any provided data. It can return values to the client. ```lua local ServerCallback = import 'callbacks' -- Registers a server event with the name __cb_resourceName:eventName ServerCallback.Register('eventName', function(source, data) return data, 'general kenobi' end) ``` -------------------------------- ### Trigger Server Callback with Await Source: https://github.com/overextended/ox_lib/wiki/Callbacks Triggers a server event and awaits a promise for the response. This is useful for synchronous-like handling of server callbacks. A timer is included to prevent rapid triggering. ```lua local ServerCallback = import 'callbacks' -- Trigger the server event and await a promise CreateThread(function() local a, b = ServerCallback.Await('resourceName', 'eventName', 100, 'hello there') print(a, b) -- 'hello there', 'general kenobi' end) ``` -------------------------------- ### SetInterval and ClearInterval Usage Source: https://github.com/overextended/ox_lib/wiki/Intervals This snippet demonstrates how to set an interval, store its reference, and then clear it after a specified time. It also shows how to set a new interval using the reference of a previously set one. ```lua local ontick = SetInterval(function(arg1, arg2) print(arg1, arg2) end, 0, 'hello', 'world) SetInterval(ontick, 1000) ClearInterval(ontick) ``` -------------------------------- ### SetInterval Basic Usage Source: https://github.com/overextended/ox_lib/wiki/Intervals This snippet shows the basic usage of SetInterval to execute a function with arguments after a delay. The function prints the arguments it receives. ```lua SetInterval(function(...) print(...) end, 0, 'hello', 'world') ``` -------------------------------- ### Trigger Server Callback Asynchronously Source: https://github.com/overextended/ox_lib/wiki/Callbacks Triggers a server event asynchronously and provides a callback function to handle the response. A timer is included to prevent rapid triggering. ```lua local ServerCallback = import 'callbacks' -- Trigger the server event and receive a callback function ServerCallback.Async('resourceName', 'eventName', 100, function(a, b) print(a, b) -- 'hello there', 'general kenobi' end, 'hello there') ``` -------------------------------- ### Triggering a Server Callback Asynchronously Source: https://github.com/overextended/ox_lib/wiki/Callbacks Triggers a server event from the client and provides a callback function to handle the response. Includes a timeout to prevent indefinite waiting. ```APIDOC ## Async ### Description Triggers a server event and executes a callback function with the results. Includes a timeout. ### Method `ServerCallback.Async(resourceName, eventName, timeout, callbackFunction, ...)` ### Parameters - **resourceName** (string) - Required - The name of the resource where the server callback is registered. - **eventName** (string) - Required - The name of the event to trigger. - **timeout** (number) - Required - The time in milliseconds to wait for a response before timing out. - **callbackFunction** (function) - Required - The function to execute with the received data. It receives the returned values from the server. - **...** (any) - Optional - Additional arguments to pass to the server event. ### Example ```lua local ServerCallback = import 'callbacks' ServerCallback.Async('resourceName', 'eventName', 100, function(a, b) print(a, b) -- 'hello there', 'general kenobi' end, 'hello there') ``` ``` -------------------------------- ### Import Specific ox_lib Function Source: https://github.com/overextended/ox_lib/blob/main/package/README.md Deconstruct and import only the required function from ox_lib. ```typescript import { checkDependency } from '@overextended/ox_lib'; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.