πŸͺ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

local hookId = exports.nolag_properties:registerHook(event, callback)

Parameters:

  • event (string): The name of the event to listen for

  • callback (function): Function to execute when the event is triggered

Returns:

  • hookId (integer): Unique identifier for the registered hook

Example:

-- Register a hook for property purchases
local hookId = exports.nolag_properties:registerHook('buyProperty', function(data)
    print(('Player %s bought property %s'):format(data.source, data.propertyId))
    
    -- Return false to cancel the purchase
    if someCondition then
        return false
    end
    
    -- Return true or nothing to allow the event to continue
    return true
end)

Removing Hooks

Parameters:

  • hookId (integer): The ID returned from registerHook

Example:

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

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:

3. Resource Cleanup

Hooks are automatically cleaned up when resources stop, but you can manually remove them:

Usage Examples

Example 1: Logging System

Example 2: Permission System

Example 3: Economy Integration

Troubleshooting

Common Issues

  1. 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

  2. Performance warnings

    • Review hook logic for expensive operations

    • Move heavy operations to async handlers

    • Consider caching frequently accessed data

  3. 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

Last updated