Search Unity

In-Game Command Recognition

Discussion in 'Scripting' started by SomeGuy22, Aug 30, 2011.

  1. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    I have a chat box in my game and I would like to be able to make it so that it can recognize specific commands from the player.

    I already have it so that you can enter to start the command, and I know I can use if command == "whatever command I want", but how would I recognize specific words in a string? Like /tp in minecraft teleportation?

    Thanks in advance!
     
  2. joshimoo

    joshimoo

    Joined:
    Jun 23, 2011
    Posts:
    266
    1 - Check the first character for your command string (/) if(command[0] == "/") and remove the command character.
    2 - Tokenize the String (stringsplit on spacebar or other)
    3 - Switch on command[1]
    4 - implement your case statements (via delegates or other command handler methods)

    Give this a try.
    If you cannot get it to work, I will post some code later.
     
  3. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Hmm. Thanks for the post! Looks like it'll work... Hold on...
     
  4. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Um, sorry for the double post, but it seems string.Split is not working well.

    Code (csharp):
    1.  
    2. var command : String[];
    3.    
    4. command = textField.Split(" ", [0]);
    5.  
    EDIT: It says No appropriate version of 'String.Split' for the argument list '(String, int[])' was found.

    EDIT... AGAIN: Fixed! Thanks joshimoo, works perfectly! It had to be " " [0] with no comma!
     
    Last edited: Aug 30, 2011