hookfunction Synchronous
detour_functiondetourfunctionhookfuncreplaceclosure
Replace calls to a function and return a callable for the preceding implementation. An optional filter limits which calls reach the replacement.
Luau
hookfunction(target: Function, replacement: Function, filter: HookFilter?) -> FunctionParameters
| Parameter | Type | Description |
|---|---|---|
target | Function | Function to hook. |
replacement | Function | Function called when the filter matches, or for every call when no filter is supplied. |
filter | HookFilter? | Optional HookFilter. A nonmatching call continues to the preceding implementation. |
Returns
FunctionA callable for the implementation displaced by this hook. Save it if the replacement needs to forward the call.
Usage notes
Filters work with Luau and C-closure targets. Supplying a non-filter value raises an error before installation.
Restore the target with restorefunction when the hook is no longer needed.
A match calls the replacement. A miss continues through the preceding hook or original function. Omit the filter or pass nil to handle every call.
Example
Luau
local target = newcclosure(function(value) return value * 2 end)
local previous
previous = hookfunction(target, function(value)
return previous(value) + 1
end, ArgumentFilter.new(1, 3))
print(target(3)) -- 7
print(target(4)) -- 8
restorefunction(target)