Commands

You can create your own commands.

Every command has its formula!

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

How to create command

For example we want to create command that will spawn balls in our scene.

It contains 2 parts: sentence "Spawn_balls" + Integer "count"

Spawn_balls [int: count]

1. Create CmdFormula

Create CmdFormula field in your script.

using TIM;
using UnityEngine;

public class DemoScript : MonoBehaviour
{
    public CmdFormula SpawnBallsFormula;
}

Select SpaceType: Space or Underline _

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

Select PartType for each part and our formula should look like this:

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 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!

Write console command
spawned balls

Last updated