Skip to main content

Overview

GMod addons use hooks to respond to game events. GLuaLS understands hook.Add, hook.Remove, hook.Run, hook.Call, and GM: definitions. It provides completions, type inference, and validation for each one.

Hook name validation

Unrecognized hook names in hook.Add and hook.Remove trigger a diagnostic:
hook.Add("PlayerSpan", "MyAddon", function(ply) -- ❌ did you mean PlayerSpawn?
    -- ...
end)
GLuaLS recognizes base GMod hooks from the built-in hook definitions. It also tracks custom hooks in your workspace when your code calls hook.Run("MyCustomHook", ...).
For custom hooks, autocomplete works after code uses the hook once. Go to definition, parameter type inference, and other advanced hook features need a GM: function for that hook.

Parameter type inference

When GLuaLS knows the hook name, it types the callback parameters:
hook.Add("PlayerSpawn", "MyAddon", function(ply)
    --                                       ^^^
    --                                       typed as Player automatically
    local hp = ply:Health() -- ✅ typed, Player method
end)

hook.Add("EntityTakeDamage", "MyDamageHook", function(target, dmg)
    -- target: Entity
    -- dmg: CTakeDamageInfo
    -- All typed from the GMod wiki annotations
end)

Callback snippet completion

When you type hook.Add("HookName", and trigger completion on the callback argument, GLuaLS inserts a typed function snippet:
-- Accepting completion for hook.Add("PlayerSpawn") inserts:
hook.Add("PlayerSpawn", "MyAddon", function(ply)
    -- YOUR CODE
end)
The snippet includes the correct number of parameters with their names.

GM: hook definitions

Files in a gamemode context, such as gamemodes/*/gamemode/, define GM as the gamemode table. GLuaLS recognizes function GM:HookName() as a hook override:
function GM:PlayerSpawn(ply)
    -- ply is typed as Player ✅
    self.BaseClass.PlayerSpawn(self, ply)
end
A GM: hook definition is required for advanced features to work on custom hooks.

hook.Run and hook.Call completions

When typing the first argument to hook.Run or hook.Call, GLuaLS autocompletes from known hook names:
hook.Run("PlayerSpawn", ply)  -- "PlayerSpawn" autocompletes

Custom hooks

GLuaLS also tracks hooks created by your code or your library:
-- Somewhere in your code:
hook.Run("MyAddon.OnPlayerAction", ply, action)

-- Elsewhere:
hook.Add("MyAddon.OnPlayerAction", "handler", function(ply, action)
    -- ply and action types are inferred from call sites
end)
For advanced features (parameter type inference, hover descriptions, function validation), define a GM: method for each custom hook.

VSCode integration

Hook intelligence works when you use hook.Add, hook.Run, or define GM: methods. Configure validation behavior in the settings panel. See GMod settings for the full reference.

Configuration

Control hook validation and mappings through the gmod section in .gluarc.json, especially gmod.hookMappings and the global diagnostics settings.
SettingDefaultDescription
gmod.hookMappings.methodToHook{}Map method names to hook names (e.g. "Think": "Think")
gmod.hookMappings.emitterToHook{}Map custom emitter functions to hook names
gmod.hookMappings.methodPrefixes[]Custom method prefixes for inferred GM: hook methods
diagnostics.disable[]Disable individual diagnostics (e.g., "gmod-invalid-hook-name")