Skip to content

tw.hook

Side: Shared (Client + Server)

Action/filter hook system inspired by WordPress. Allows scripts to register handlers that execute on specific events (actions) or transform values through a chain (filters).

Functions

tw.hook.registerAction(name, handler, priority?)

Registers an action handler. Actions are fire-and-forget: they execute code but do not return a value.

ParameterTypeDefaultDescription
namestringHook name
handlerfunction(...)Handler to execute when the action fires
priorityinteger?10Execution order. Lower values run first

tw.hook.doActions(name, ...)

Executes all registered action handlers for the given hook name, in priority order.

ParameterTypeDescription
namestringHook name
...anyArguments passed to each handler

tw.hook.registerFilter(name, handler, priority?)

Registers a filter handler. Filters receive a value, optionally modify it, and return it for the next filter in the chain.

ParameterTypeDefaultDescription
namestringHook name
handlerfunction(value, ...)Handler that receives the current value and returns the modified value
priorityinteger?10Execution order. Lower values run first

tw.hook.applyFilters(name, value, ...)

Passes a value through all registered filter handlers in priority order. Each handler receives the value returned by the previous one.

ParameterTypeDescription
namestringHook name
valueanyInitial value to filter
...anyExtra arguments passed to each handler

Returns: The final filtered value.

Export

Other resources can register actions from outside without requiring tw:

lua
exports["twinded_libs"]:registerAction(name, handler, priority)

Examples

Action: custom hook for item pickup

lua
-- In your script: fire the action when a player picks up an item
tw.hook.doActions("onItemPickup", source, item_name, quantity)

-- In another script or in config/custom: react to the event
tw.hook.registerAction("onItemPickup", function(source, item_name, quantity)
    print(source .. " picked up " .. quantity .. "x " .. item_name)
end)

Filter: modify a price before purchase

lua
-- Register a filter that gives a 20% discount to sheriffs
tw.hook.registerFilter("getItemPrice", function(price, source, item_name)
    local job = tw.fw:getJob(source)
    if job == "sheriff" then
        return math.floor(price * 0.8)
    end
    return price
end)

-- Apply the filter when calculating price
local base_price = 100
local final_price = tw.hook.applyFilters("getItemPrice", base_price, source, "bandage")

Priority ordering

lua
-- This runs second (priority 10, default)
tw.hook.registerAction("onDoorUnlock", function(source, door_id)
    print("Default handler")
end)

-- This runs first (priority 5)
tw.hook.registerAction("onDoorUnlock", function(source, door_id)
    print("Early handler")
end, 5)

-- This runs last (priority 20)
tw.hook.registerAction("onDoorUnlock", function(source, door_id)
    print("Late handler")
end, 20)

External resource registering an action

lua
-- From any other resource (no tw global needed)
exports["twinded_libs"]:registerAction("onItemPickup", function(source, item_name, quantity)
    -- custom logging, webhook, etc.
end, 10)

Notes

  • Actions do not return values. Use filters when you need to transform data.
  • Filters must always return a value, even if unmodified, or the chain will break.
  • Lower priority numbers execute first. The default priority is 10.
  • The export registerAction allows non-Twinded resources to hook into your scripts.

Premium RedM Scripts — Multi-Framework