sourcemod.console — Console interaction.

Module Contents

Flag Description
SM_REPLY_CONSOLE
The command was executed in the console.
SM_REPLY_CHAT
The command was executed in chat (by ”!cmd” or “/cmd” – depending on your server’s chat trigger configuration)
create_convar(name, value[, description[, flags[, min[, max]]]])

Creates a new console variable.

Parameters:
  • name (str) – Name of the ConVar
  • value (str) – The value of the ConVar. As the internal representation of a ConVar’s value is a string, stringization is left to the user.
  • description (str) – (Optional) Description of the ConVar
  • flags (FCVAR Constants) – (Optional) Flags that change how a ConVar is handled. Use FCVAR Constants, such as FCVAR_CHEAT, etc.
  • min (float) – The lowest numeric value this ConVar can be set to. Pass None to ignore this field.
  • max (float) – The highest numeric value this ConVar can be set to. Pass None to ignore this field.
Return type:

ConVar

Returns:

A valid ConVar object on success, None otherwise. See ConVar Objects

find_convar(name)

Finds the specified ConVar. Returns None if the ConVar could not be found.

Parameters:
  • name (str) – The name of the ConVar to retrieve
Return type:

sourcemod.console.ConVar

Returns:

A valid ConVar object on success, None otherwise.

print_to_server(message)

Sends a message to the server console. This is the same as print message, so long as sys.stdout remains unchanged by your plug-in.

reg_concmd(name, callback[, description="", flags=0])

Registers a new console command or hooks an existing one.

Parameters:
  • name (str) – Name of the ConCommand
  • callback (callable) – A function to call when the ConCommand is executed.
  • description (str) – (Optional) Description of the ConCommand
  • flags (FCVAR Constants) – Flags that change how the ConCommand is handled.

callback is the function to call when the console command is executed. This callback should have one parameter: a ConCommandReply object.

reg_srvcmd(name, callback[, description="", flags=0])

This command is the same as reg_concmd, except that the command can only be called by the server console or RCON.

server_command(command)

Executes a command is if it were run on the server console.

Parameters:
  • command (str) – Command to execute
server_execute()

Executes every command in the server’s command buffer now, rather than once per frame.

FCVAR Constants

These constants describe how the Source engine should handle console commands and variables. Most of the descriptions below come directly from the Source SDK.

Name Description
FCVAR_NONE The default; no flags at all
FCVAR_UNREGISTERED If this is set, don’t add to linked list, etc.
FCVAR_LAUNCHER Defined by launcher.
FCVAR_GAMEDLL Defined by the game DLL.
FCVAR_CLIENTDLL Defined by the client DLL.
FCVAR_MATERIAL_SYSTEM Defined by the material system.
FCVAR_PROTECTED A server cvar that contains sensitive data, such as a password. When its value is requested, it sends 1 if it’s not empty/zero, and 0 otherwise.
FCVAR_SPONLY This cvar cannot be changed by clients connected to a multiplayer server.
FCVAR_ARCHIVE Set to cause this cvar to be saved to vars.rc
FCVAR_NOTIFY Notifies players when the cvar’s value is changed. For example, this flag is set for sv_cheats
FCVAR_USERINFO Changes the client’s info string.
FCVAR_PRINTABLEONLY This cvar’s string cannot contain unprintable characters (e.g., used for player name, etc.)
FCVAR_UNLOGGED If the cvar has FCVAR_SERVER set, don’t log changes to the log file / console if we are creating a log
FCVAR_NEVER_AS_STRING Never try to print that cvar.
FCVAR_REPLICATED Server setting enforced on clients.
FCVAR_CHEAT Only useable in singleplayer / debug / multiplayer & sv_cheats 1
FCVAR_STUDIORENDER Defined by the studiorender system.
FCVAR_DEMO Record this cvar when starting a demo file.
FCVAR_DONTRECORD Don’t record this command in demo files.
FCVAR_PLUGIN Defined by a 3rd party plugin.
FCVAR_DATACACHE Defined by the datacache system.
FCVAR_TOOLSYSTEM Defined by an IToolSystem library.
FCVAR_FILESYSTEM Defined by the file system.
FCVAR_NOT_CONNECTED Cvar cannot be changed by a client that is connected to a server.
FCVAR_SOUNDSYSTEM Defined by the soundsystem library.
FCVAR_ARCHIVE_XBOX Cvar written to config.cfg on the Xbox.
FCVAR_INPUTSYSTEM Defined by the inputsystem DLL.
FCVAR_NETWORKSYSTEM Defined by the network system.
FCVAR_VPHYSICS Defined by vphysics.

ConVar Objects

ConVar.flags

The bitstring of FCVAR_* flags on this console variable

ConVar.lower_bound

The lower bound of this ConVar.

ConVar.name

Read-only! The name of the ConVar.

ConVar.upper_bound

The upper bound of this ConVar.

ConVar.value

The value of the ConVar. This will always be a string. If you try to set it as something else, it will try to str() it.

ConVar.hook_change(callback)

Creates a hook that is called when this console variable’s value is changed.

Parameters:
  • callback (callable) – The function to call when the ConVar is changed.

The callback should have the prototype callback(cvar, oldvalue, newvalue), where cvar is the ConVar object representing the ConVar that was changed, oldvalue is the previous value of the ConVar as a string, and newvalue is the value being assigned to the ConVar as a string.

ConVar.reset()

Resets the console variable to its default value.

ConVar.unhook_change(callback)

Removes a ConVar change hook from the update list. callback will no longer be called when the ConVar is changed.

Parameters:
  • callback (callable) – The active hook to remove
Raises ViperException:
 

No active hook on the ConVar, or an invalid or unregistered callback supplied.

ConCommandReply Objects

ConCommandReply.name

The name of the ConCommand.

ConCommandReply.args

The arguments passed when the ConCommand was executed, as a list.

ConCommandReply.argstring

The full argument string sent to the server. This includes any quotes, as well.

ConCommandReply.client

The Client whom executed the ConCommand.

ConCommandReply.reply_to

How the command was executed. This is either SM_REPLY_CHAT or SM_REPLY_CONSOLE

ConCommandReply.reply(message)

Replies to the client whom executed the ConCommand in the way they executed the ConCommand (either in the console or in chat).

Parameters:
  • message (str) – The message to send