# How to create console commands

Console commands are method attributes, that means that you just have to add `[ConsoleCommand(name: "...", docs:"...")]` or `[ConsoleCommand("...","...")]` above your method.\
The **name** argument is the command name itself.\
The **docs** argument is the command description.

{% hint style="info" %}
**Your method does not need to be public but it needs to be static!**
{% endhint %}

If you want to get the arguments of your command when its ran you can simply add a string\[] parameter named `args` as shown below.

```csharp
[ConsoleCommand(name: "useful", docs: "this command is useful.")]
public static void MyCommand(string[] args)
{
    Debug.Log("You entered " + args.Length + " arguments!");
}
```

The method can also have a string return type and will display the returned value as shown below.

```csharp
[ConsoleCommand(name: "useful", docs: "this command is useful.")]
public static string MyCommand(string[] args)
{
    return "You entered " + args.Length + " arguments!";
}
```

As said above, commands needs to be static, so in order to access your non static variables you can use the mod instance as shown below.

```csharp
public string myNonStaticVariable = "some stuff";
public MyModType instance;

void Start(){
    instance = this;
}

[ConsoleCommand(name: "useful", docs: "this command is useful.")]
public static string MyCommand(string[] args)
{
    return "My Variable : " + instance.myNonStaticVariable;
}
```

{% hint style="info" %}
The commands are automatically registered and automatically unregistered when the mod is unloaded.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://api.raftmodding.com/modding-tutorials/how-to-create-console-commands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
