hookfunction Synchronous

  • detour_function
  • detourfunction
  • hookfunc
  • replaceclosure

Replace calls to a function and return a callable for the preceding implementation. An optional filter limits which calls reach the replacement.

Syntax
Luau
hookfunction(target: Function, replacement: Function, filter: HookFilter?) -> Function

Parameters

Function parameters
ParameterTypeDescription
targetFunctionFunction to hook.
replacementFunctionFunction called when the filter matches, or for every call when no filter is supplied.
filterHookFilter?Optional HookFilter. A nonmatching call continues to the preceding implementation.

Returns

Function

A 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

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)
Kawaii documentation