> For the complete documentation index, see [llms.txt](https://docs.pentafloat.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.pentafloat.com/documentation/commands/dynamic-commands.md).

# Dynamic commands

If you want even more control over your commands, they can also be added and removed at runtime by accessing the `CommandExecutor` class directly.

## Adding commands at runtime <a href="#adding-commands-at-runtime" id="adding-commands-at-runtime"></a>

Any delegate can be turned into a command using `CommandExecutor.AddCommand`. The method simply requires a name for the command, along with a delegate.

{% tabs %}
{% tab title="Action delegate" %}

```csharp
private void Start()
{
    // The method doesn't return anything, so we'll create an Action.
    CommandExecutor.AddCommand("hello", new Action<string>(Hello));
}

private void Hello(string message)
{
    Debug.Log(message);
}
```

{% endtab %}

{% tab title="Func delegate" %}

```csharp
private void Start()
{
    // The method returns a value, so we'll create Func in this case.
    CommandExecutor.AddCommand("multiply", new Func<int, int, int>(Multiply));
}

private int Multiply(int a, int b)
{
    return a * b;
}
```

{% endtab %}

{% tab title="Lambda expression" %}

```csharp
private void Start()
{
    // Alternatively you could create the delegate with a lambda expression. 
    CommandExecutor.AddCommand("hello", new Action<string>((string message) =>
    {
        Debug.Log(message);
    }));
}
```

{% endtab %}
{% endtabs %}

## Removing commands at runtime

Commands can also be removed at runtime. Even those that were created using the `[Command]` attribute. To do so, use `CommandExecutor.RemoveCommand`. The example below shows a great way to keep the player engaged for longer.

```csharp
CommandExecutor.RemoveCommand("quit");
```

{% hint style="info" %}
Note

If you've removed commands that were made using the `[Command]` attribute, such as any built-in command, you can restore all of them with `CommandExecutor.Scan()`.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.pentafloat.com/documentation/commands/dynamic-commands.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
