🪝 Hooks System
Overview
The Hooks system is an event-driven architecture that allows other resources to register callback functions for specific events within the nolag_properties resource. This system enables seamless integration with other resources and provides extensibility for custom functionality.
API Reference
Registering Hooks
lua
Parameters:
event(string): The name of the event to listen forcallback(function): Function to execute when the event is triggered
Returns:
hookId(integer): Unique identifier for the registered hook
Example:
lua
Removing Hooks
lua
Parameters:
hookId(integer): The ID returned fromregisterHook
Example:
lua
Event Cancellation
Hooks can prevent events from continuing by returning false. This is useful for:
- Adding custom validation logic
- Implementing permission systems
- Preventing actions based on custom conditions
lua
Best Practices
1. Keep Hooks Fast
Hooks should execute quickly to avoid performance issues. Avoid:
- Database queries in hooks (use async patterns)
- Heavy computations
- Long-running operations
2. Error Handling
Always implement proper error handling in your hooks:
lua
3. Resource Cleanup
Hooks are automatically cleaned up when resources stop, but you can manually remove them:
lua
Usage Examples
Example 1: Logging System
lua
Example 2: Permission System
lua
Example 3: Economy Integration
lua
Troubleshooting
Common Issues
- Hook not triggering
- Verify the event name is spelled correctly
- Check that the resource is started after nolag_properties
- Ensure ox_lib dependency is met
- Performance warnings
- Review hook logic for expensive operations
- Move heavy operations to async handlers
- Consider caching frequently accessed data
- Events getting cancelled unexpectedly
- Check all registered hooks for the event
- Verify return values (undefined/nil is treated as true)
- Add logging to identify which hook is cancelling
Debug Tips
lua