AcediaDocumentation

hookmetamethod

Replaces a function-valued metamethod and returns its original implementation.

The existing metamethod must be a function. Shared userdata metatables can affect more than one object.

hookmetamethod(object: table | userdata, method: string, replacement: function): function

Parameters

ParameterTypeDescription
objecttable | userdataObject with a metatable.
methodstringMetamethod name, such as "__index".
replacementfunctionNew implementation.

Example

local object = setmetatable({}, { __index = function() return "old" end })
local original = hookmetamethod(object, "__index", function() return "new" end)
print(object.missing)
restorefunction(original)

Example output

new

Errors: Raises if the object has no metatable or the named member is not a function.

On this page