# Commands

You can create your own commands.

<figure><img src="https://2580249083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtMR8cCnFq2WqljWgSgJA%2Fuploads%2FNghL95DE6VKsvVVY1OIo%2Fimage.png?alt=media&#x26;token=e1167a4b-07eb-4935-83d6-f055317aaccb" 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="https://2580249083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtMR8cCnFq2WqljWgSgJA%2Fuploads%2Fn58DvI2Yaq2YrZKMR3ca%2Fimage.png?alt=media&#x26;token=72ad9014-1ba1-4720-b7d5-d6a766d79914" 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="https://2580249083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtMR8cCnFq2WqljWgSgJA%2Fuploads%2FNyC9AdVFhxh1lZtuSy1F%2Fimage.png?alt=media&#x26;token=4fd6b7bc-3e22-487c-9482-fc86733db277" 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` ](https://tim-entertainment.gitbook.io/tim.console/commands/parttype-c)for each part and our formula should look like this:

<figure><img src="https://2580249083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtMR8cCnFq2WqljWgSgJA%2Fuploads%2Fs9GpTOt4G0YtPBUPVrwe%2Fimage.png?alt=media&#x26;token=23fc8eab-7f68-41b1-a8aa-e2308d38763e" 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 ](https://tim-entertainment.gitbook.io/tim.console/commands/cmdinputresult-c)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="https://2580249083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtMR8cCnFq2WqljWgSgJA%2Fuploads%2FAtnerKy1aLfM4iXpNWqd%2Fimage.png?alt=media&#x26;token=9756fca6-2ff3-45e3-a14f-8f0198709191" alt=""><figcaption><p>Write console command</p></figcaption></figure>

<figure><img src="https://2580249083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtMR8cCnFq2WqljWgSgJA%2Fuploads%2FnnckTdVvPs0bqS6tiYpc%2Fimage.png?alt=media&#x26;token=75f14e65-704c-4ff3-9ed8-7602f2467ce0" alt=""><figcaption><p>spawned balls</p></figcaption></figure>
