util.input.register

Client

Registers a named console command handler that fires when the matching command is executed


Syntax

local status = util.input.register(
    name,
    exec
)

Handlers are scoped to the environment that registered them and are automatically cleaned up when that environment is destroyed.

  • Multiple handlers — the same command name can have any number of handlers registered independently.
  • Console fallback — typing an unrecognized command into the console dispatches it here automatically;
    there's no need to call util.input.execute yourself just to support console input.

Parameters

TypeNameDescription
stringnameCommand name to listen for
functionexecHandler function to invoke when the command is executed

Returns

TypeNameDescription
boolstatustrue on successful registration, false otherwise

Callback: exec

exec(args)
TypeNameDescription
string[]argsArray of string tokens passed after the command name

Examples

Register a basic teleport command
util.input.register("teleport", function(args)
    if #args < 3 then
        core.engine.print("warn", "Usage: teleport <x> <y> <z>")
        return
    end

    local x, y, z = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
    if not x or not y or not z then
        core.engine.print("error", "teleport: x, y, z must be numbers")
        return
    end

    core.engine.print("info", "Teleporting to", x, y, z)
end)
Register a command with no arguments
util.input.register("respawn", function(args)
    core.engine.print("info", "Respawning player")
end)
Register a handler and store a reference for later removal
local function on_debug_toggle(args)
    core.engine.print("info", "Debug overlay toggled")
end

util.input.register("debug", on_debug_toggle)

-- Remove when no longer needed
util.input.unregister("debug", on_debug_toggle)

On this page