# Commands

You can create your own commands.

<figure><img src="/files/rE5KY41X72B9vD1BRZAd" alt=""><figcaption></figcaption></figure>

Every command has its formula!

You create formulas by your self. Commands can be of any complexity and structure!

<figure><img src="/files/XsBQDV5OMY0gpWyjFRk8" alt=""><figcaption></figcaption></figure>

## How to create command

For example we want to create command that will spawn balls in our scene.&#x20;

It contains 2 parts: <mark style="color:blue;">sentence</mark> <mark style="color:yellow;">"Spawn\_balls"</mark> + <mark style="color:blue;">Integer</mark> <mark style="color:yellow;">"count"</mark>

<mark style="color:yellow;">Spawn\_balls \[int: count]</mark>

### 1. Create `CmdFormula`

Create `CmdFormula` field in your script.

```
using TIM;
using UnityEngine;

public class DemoScript : MonoBehaviour
{
    public CmdFormula SpawnBallsFormula;
}
```

<figure><img src="/files/00we3UwPtAZ8rhGTlrfi" alt=""><figcaption></figcaption></figure>

Select **SpaceType**: Space   or Underline `_`

➕ You can add new part (or remove e excess part) and customize it. We need 2 parts.

Select [`PartType` ](/tim.console/commands/parttype-c.md)for each part and our formula should look like this:

<figure><img src="/files/aCWJ0cAY9q6QD14W6eK4" alt=""><figcaption></figcaption></figure>

### 2. Create a method that will be called when executing the command

```
using TIM;
using UnityEngine;

public class DemoScript : MonoBehaviour
{
    public CmdFormula SpawnBallsFormula;

    private void OnSpawnBallsCommand(CmdInputResult result)
    {
        
    }
}
```

[CmdInputResult ](/tim.console/commands/cmdinputresult-c.md)contains user's input string already separated in parts and parsed to necessary types! So to get balls count you just need to get value from Part that contains balls count value:

```
using TIM;
using UnityEngine;

public class DemoScript : MonoBehaviour
{
    public CmdFormula SpawnBallsFormula;
    public GameObject BallPrefab;

    private void OnSpawnBallsCommand(CmdInputResult result)
    {
        int ballsCount = result.Parts[1].Integer;
        
        for (int i = 0; i < ballsCount; i++)
        {
            Instantiate(BallPrefab);
        }
    }
}
```

### 3. Register your command

You need to register your command in Console

```
using System;
using TIM;
using UnityEngine;

public class DemoScript : MonoBehaviour
{
    public CmdFormula SpawnBallsFormula;
    public GameObject BallPrefab;

    private void Start()
    {
        TIM.Console.RegisterCommand(SpawnBallsFormula, OnSpawnBallsCommand);
    }

    private void OnSpawnBallsCommand(CmdInputResult result)
    {
        int ballsCount = result.Parts[1].Integer;
        
        for (int i = 0; i < ballsCount; i++)
        {
            Instantiate(BallPrefab);
        }
    }
}
```

## ✅ All done!

<figure><img src="/files/WtAzh1JmeH56ezGoGNXO" alt=""><figcaption><p>Write console command</p></figcaption></figure>

<figure><img src="/files/1cwM5KzjYJfWICu27dlc" alt=""><figcaption><p>spawned balls</p></figcaption></figure>


---

# 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://tim-entertainment.gitbook.io/tim.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.
