Yuari
Documentation
Docs/Metatables/hookmetamethod

hookmetamethod

Hooks a metamethod and returns the original.

Syntax

hookmetamethod(object: any, method: string, hook: function) -> function

Parameters

ParameterTypeDescription
objectanyThe object value used by the call.
methodstringThe method value used by the call.
hookfunctionThe replacement callback that receives the original arguments.

Returns

TypeDescription
functionThe 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.