Skip to main content

Overview

@hook marks a method as a GMod hook handler. GLuaLS can then validate hook names, infer parameters from hook definitions, and autocomplete hook names in hook.Add and hook.Run.

Syntax

---@hook
---@hook HookName
  • Without an argument: GLuaLS uses the method name as the hook name
  • With an argument: GLuaLS uses the given hook name instead of the method name

Basic usage

local PLUGIN = {}

---@hook
function PLUGIN:PlayerSpawn(ply)
    -- ply is typed as Player from the hook definition ✅
end

---@hook PlayerInitialSpawn
function PLUGIN:OnPlayerFirstJoin(ply)
    -- maps to PlayerInitialSpawn hook
    -- ply typed as Player ✅
end

GM: overrides

GLuaLS treats methods on the gamemode table (GM) as hook overrides. You do not need @hook for them in most cases:
function GM:PlayerSpawn(ply)
    self.BaseClass.PlayerSpawn(self, ply)
end
GLuaLS infers the parameters from the GMod hook definition.

Custom prefixes

If your framework uses a custom table instead of GM or PLUGIN, configure GLuaLS to recognize it:
Use the settings menu instead of editing the JSON file directly. This helps you avoid config mistakes.
.gluarc.json
{
  "gmod": {
    "hookMappings": {
      "methodToHook": {
        "Run": "PlayerSpawn"
      },
      "emitterToHook": {
        "COMPONENT": "CustomHook"
      },
      "methodPrefixes": ["COMPONENT"]
    }
  }
}

Hook autocomplete

When you call hook.Add("|", ...), hook.Run("|", ...), or hook.Call("|", ...), GLuaLS autocompletes known hook names, including GMod built-ins and your custom hooks.

See also