Calling Commands
Implicit and explicit parameters
For an example we define the "set_player_color" command with "body" and "head" (optional) parameters with several color options, as shown here:
The most basic syntax for calling commands is with implicit parameters. Implicit means we don't specify the parameter 'key' and simply infer this from the order they're listed:
<keyword> [value1] [value2] (etc..)
So for the above "set_player_color" command this:
set_player_color red blue
... will set the "body" to "red" and the "head" to "blue". However what if you want to specify only the "head" parameter, and not the "body" parameter? For this you can use explicit parameter syntax:
set_player_color head=blue
Explicit parameters syntax is as follows:
<keyword> [parameter1 key]=[value1] [parameter2 key]=[value2] (etc..)
You can use both implicit and explicit parameter syntax in the same string under one contraint: all implicit parameters need to come first.
In script you can handle these optional parameters as such:
private void HandlePlayerColorCommand(ICommandSender sender, Command command)
{
Parameters parameters = command.parameters;
if (parameters.TryGet("body", out string bodyColor))
{
player.SetBodyColor(bodyColor); // Example
sender.LogMessage("Set player body color to: " + bodyColor);
}
if (parameters.TryGet("head", out string headColor))
{
player.SetHeadColor(headColor); // Example
sender.LogMessage("Set player head color to: " + headColor);
}
}
Calling a command from script
To execute a command string from script, call CosmosConsole.QueueExecuteString:
console.QueueExecuteString("set_player_color red blue");
If the command string is succesfully parsed, it'll either executed locally on the next CosmosConsole.Update call or send over the network.
- If the console is in client-mode, and the command's execution type (defined in the console configuration) is either "Call from client, execute on server" or "Call from client or server, execute on server", then the command string will be send over the network.
- Otherwise the command will be executed locally.
Autocomplete
One powerful utility when entering commands is autocomplete. It can suggest both command keywords from the moment you type the first letter, as well as implicit and explicit parameters that have predefined list of values.
Command keyword suggestion
Implicit parameter value suggestions
Explicit parameter key and value combination suggestions
The autocomplete selection controls are different between the ingame console UI, editor window client and standalone client due to conflicting hotkeys and differences in UI setup, but are all powered by the same CommandAutoCompleteProvider under the hood.