Commands Basics - DAndrewBox/GM-EzConsole GitHub Wiki
Create your first command
On this section, you will learn how to create your first command. This command will be called trace
and will print a message on the console.
To create your first command, you can do so by following these steps:
-
Open the
ezConsole_custom_commands
script file in your project. -
Create a new function inside this file with the following name
console_command_alert
.
/// @func console_command_alert(args)
/// @param {array} args
function console_command_alert(_args) {
show_message_async(_args[0]);
}
-
As you can see, the function should start with
console_command_
followed by the command name. In this case, the command name isalert
. The_args
parameter is an array of strings that contains the arguments passed to the command. You can access the arguments by using the_args
array. For example, if you want to access the first argument, you can do so by using_args[0]
. -
Now you should create a new console command using the constructor
EzConsoleCommand
. To add you arguments to the command, you should use the constructorEzConsoleArgument
orEzConsoleArgumentWithOptions
. You can see the meaning of each parameter on the constructors page.
This command constructor has the following format:
new EzConsoleCommand(
"alert",
"",
"Shows an alert on screen.",
console_command_alert,
[new EzConsoleCommandArgument("message", "Message to show")]
);
- Once you have created the command, it would automatically be added to the list of commands. You can now run your project and type
alert
on the console, followed by amessage
. If everything went well, you should see the alert with your message on screen.
You can also skip the step 4 by adding the commands via file. You can see how to do that on the next section.
Import base commands
By default, the extension comes with a set of base commands that you can use to test the extension.
The commands are imported automatically when the extension is imported into your project and are saved in the default_commands.json
file.
Sidenote: If you want to disable the import of the base commands, you can do so by removing
"default_commands.json"
from theezConsole_files
array on theezConsole_configurations
script and deleting thedefault_commands.json
file.
The following table shows the list of base commands included on the default_commands.json
file:
Command | Description | Arguments | Required arguments |
---|---|---|---|
help | Show the list of commands. | [command] | [false] |
message | Shows a message on screen and pauses the game. | [message] | [true] |
fullscreen | Toggles fullscreen mode. | [false/true] | [true] |
game | Choose to end or restart the game. | [end/reset] | [true] |
create | Create an object. | [object_name, x, y, depth] | [true, false, false, false] |
instances | Show the list of active instances. | [object_name] | [true] |
clear | Clear the console. | ||
set | Set a variable on an instance. | [instance_id, variable_name, value] | [true, true] |
get | Gets a variable or all variables from an instance. | [instance_id, variable_name] | [false] |
delete | Delete an instance. | [instance_id] | [true] |
fps | Shows or toggles the current FPS. | [false/true] | [false] |
debug_view | Shows or hides the debug views. | [false/true] | [false] |
Import custom commands from file
If you want to import custom commands from a file, you can do so by following these steps:
- Create a new file with the
.json
extension on the included console folder. - Add and create the commands you want to import to the file.
- Open the script
ezConsole_configurations
. - Add the filename of the file into the
ezConsole_files
array on the script. - Create a callback function for each command you want to import on the
ezConsole_custom_commands
script. (Or elsewhere, but remember which is the callback for the command). - Run your project and press
F1
(by default) to open the console. - Type
help
to see the list of commands.
You should see the commands you imported. If you don't, check if you followed the steps correctly and the file is in the correct folder.
Remember that the commands should have this format:
{
"name": "command_name",
"alias": "command_short_name",
"desc": "Short description of the command",
"args": [
{
"name": "argument_name",
"desc": "Short description of the argument",
"required": "true or false",
"type": "asset type of the argument by following the table on the previous section",
"options": ["array", "with", "options"]
}
],
"callback": "Function to call when the command is executed"
}
[!IMPORTANT] The
short
field was changed toalias
in versions 1.3.0+. If you have a command with a short name, you should use thealias
field instead.
You can see some examples of a command on the default_commands.json
file. The following table shows the meaning of each field:
Field | Description | Required |
---|---|---|
name | The name of the command. | Yes |
alias | The alias of the command. (short before v1.3.0) |
No (use "" to disable) |
desc | The description of the command. | No |
args | An array of arguments of the command. | No |
args.name | The name of the argument. | Yes |
args.desc | The description of the argument. | No |
args.required | True if the argument is required, false otherwise. | No |
args.type | The asset type of the argument. | No |
args.options | An array with the options available. Only valid if type is option |
No |
callback | The function to call when the command is executed. | Yes |
Import custom commands from code
If you want to import custom commands from code, you can do so by following these steps:
- Open the
ezConsole_custom_commands
script. - Create the logic for the callback functions for the commands you want to add.
- In the second
#region
, use theEzConsoleCommand()
constructor as shown on the step 3 of the first section. - Run your project and press
F1
(by default) to open the console. - Type
help
to see the list of commands.
Now you should see the commands you imported. If you don't, check if you followed the steps correctly.