hookmetamethod
Hooks a metamethod and returns the original.
Syntax
hookmetamethod(object: any, method: string, hook: function) -> function
Parameters
| Parameter | Type | Description |
|---|---|---|
object | any | The object value used by the call. |
method | string | The method value used by the call. |
hook | function | The replacement callback that receives the original arguments. |
Returns
| Type | Description |
|---|---|
function | The original metamethod. |
Description
hookmetamethod replaces a metamethod slot on the object's raw metatable and returns the previous metamethod.
Every later metamethod dispatch for that object goes through the replacement, so the hook can observe or rewrite namecalls, indexing and other metamethod events.
Example
local old
old = hookmetamethod(game, "__namecall", function(self, ...)
return old(self, ...)
end)
Notes
- Metatable changes are applied directly to the live object.
- Keep the returned original to forward calls.
- The change is applied to the live metatable shared by the object.