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 callutil.input.executeyourself just to support console input.
Parameters
| Type | Name | Description |
|---|---|---|
string | name | Command name to listen for |
function | exec | Handler function to invoke when the command is executed |
Returns
| Type | Name | Description |
|---|---|---|
bool | status | true on successful registration, false otherwise |
Callback: exec
exec(args)| Type | Name | Description |
|---|---|---|
string[] | args | Array of string tokens passed after the command name |
Examples
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)util.input.register("respawn", function(args)
core.engine.print("info", "Respawning player")
end)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)