Search Unity

Help - Value Chat Commands

Discussion in 'Scripting' started by nevaran, Jul 31, 2015.

  1. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    Does anyone have any idea how to make chat commands with values?

    For example: /spawn box 5 -- to spawns 5 boxes

    All posts and videos i have seen have only /spawn box - or just /spawn, which is sadly not what i require :/
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Split the string by space into an array and treat each element as an argument.

    Code (csharp):
    1.  
    2. String[] command = chatText.split(' ');
    3.  
    4. // [0] = /spawn, [1] = box, [2] = 5
    5. if(command[0] == "/spawn")
    6. {
    7.     if(command.Length == 3)
    8.     {
    9.         if(command[1] == "box")
    10.         {
    11.             int boxCount = 1;
    12.             int.TryParse(command[2], out boxCount);
    13.  
    14.             spawnBox(boxCount);
    15.         }
    16.     }
    17.     else if(command.Length == 2)
    18.     {
    19.         if(command[1] == "box")
    20.              spawnBox(1); // spawn 1 box as default if you don't specify a number
    21.     }
    22.     else
    23.     {
    24.         Debug.Log("Invalid arguments");
    25.     }
    26. }
    27.  
     
    nevaran likes this.
  3. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    Thanks, ill try it out now!