Overview
Hooks allow resources to register callbacks that are triggered by specific events. Each hook can returnfalse to cancel the event, making them ideal for validation and permission checks.
The hooks system is based on ox_inventory’s hooks implementation.
Exports
registerHook
Registers a callback function to be triggered by a resource event.Parameters
event(string) - The event name to hook intocb(function) - The callback function to execute when the event is triggered
Returns
hookId(integer) - A unique identifier for the registered hook
Callback Behavior
- The callback receives the event payload as its argument
- Returning
falsefrom the callback cancels the event - Returning any other value (or no return) allows the event to continue
Example
removeHooks
Removes a previously registered hook by its ID.Parameters
id(number) - The hookId returned fromregisterHook
Example
How It Works
Event Triggering
When an event is triggered, all registered hooks for that event are executed in order:- Each hook callback is called with the event payload
- Execution time is measured for performance monitoring
- If a hook takes longer than 100ms, a warning is logged
- If any hook returns
false, the event is cancelled immediately - All hooks must complete successfully for the event to proceed
Performance Monitoring
The hooks system automatically tracks execution time and warns about slow hooks:Automatic Cleanup
Hooks are automatically cleaned up when a resource stops:Advanced Usage
Multiple Hooks on Same Event
You can register multiple hooks for the same event. They will execute in the order they were registered:Hook Lifecycle
Automatic cleanup happens when resources stop, but you can manually remove hooks if needed for dynamic behavior.
Best Practices
- Keep hooks fast - Avoid heavy computations or blocking operations
- Return explicit false - Only return
falsewhen you want to cancel the event - Store hook IDs - Save the returned hookId if you need to remove the hook later
- Handle errors - The system uses
pcallinternally, but your hooks should still handle errors gracefully - Use descriptive event names - Choose clear, specific event names to avoid conflicts