> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# hookmetamethod

> Ищет metamethod function у value и hook-ит эту function через hookfunction. First argument может быть любым value, который принимает raw metafield lookup. Second argument должен быть string metamethod name, а third argument должен быть function replacement. Если raw metafield lookup не находит named metafield, function вызывает "Metamethod doesn't exist" для argument 2. Если metafield существует, но не является function, вызывает "Metamethod isn't a function" для argument 2. Когда metafield является function, hookmetamethod вызывает hookfunction с found metamethod function как target и возвращает cloned original closure из hookfunction.

<div className="real-function-page">
  ```lua theme={null}
  hookmetamethod(object: any, metamethod: string, replacement: function): function
  ```

  ## Аргументы

  <ParamField path={"object"} type={"any"} required>
    Value, чей methods проверяется через raw metafield lookup.
  </ParamField>

  <ParamField path={"metamethod"} type={"string"} required>
    Metafield name, передаваемый в raw metafield lookup.
  </ParamField>

  <ParamField path={"replacement"} type={"function"} required>
    Function, передаваемая в hookfunction как replacement для found metamethod function.
  </ParamField>

  ## Возвраты

  <ResponseField name={"original"} type={"function"}>
    Cloned original metamethod closure, возвращенная hookfunction.
  </ResponseField>

  ## Пример

  Hook an existing metamethod function.

  ```lua theme={null}
  local object = setmetatable({}, {
      __index = function(_, key)
          return key
      end,
  })

  local original
  original = hookmetamethod(object, "__index", function(self, key)
      if key == "Name" then
          return "Real"
      end

      return original(self, key)
  end)
  ```
</div>
