AcediaDocumentation
Guides

Changing a function temporarily

Use a function hook, retain the original, and restore it.

A function hook replaces what callers see when they call a target function. hookfunction returns a callable copy of the previous behavior, so your replacement can delegate to it.

Hook and restore

local function calculate(value)
    return value * 2
end

local original
original = hookfunction(calculate, function(value)
    return original(value) + 1
end)

print(calculate(3))
restorefunction(calculate)

Keep original in a scope that remains available to the replacement. restorefunction takes the hooked target and raises an error when it cannot find a matching hook.

Check the result: A protected executor namecall wrapper may cause hookfunction to return nil.

Other hook scopes

  • hookmetamethod replaces a function-valued metatable member. A shared metatable can affect multiple objects.
  • hookproto works at the prototype level and has its own restoration call.

Choose the narrowest operation that matches what you need, and restore it when the script is finished.

On this page