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): functionParameters
| Parameter | Type | Description |
|---|---|---|
object | table | userdata | Object with a metatable. |
method | string | Metamethod name, such as "__index". |
replacement | function | New 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
newErrors: Raises if the object has no metatable or the named member is not a function.
