Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Password as Trigger

Discussion in 'Scripting' started by terransage, Sep 30, 2006.

  1. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    How would one (me in particular) go about using typed-in passwords to bring about events, like animations, destroyed objects, instantiations, loadLevels and the like? (It's going to be a major part of an "adventure/ wordgame" I'm working on.) I tried manipulating the following Input.inputString script (for typing in your name to trigger an action), but couldn't figure out the correct syntax for getting Unity to recognize a particular password leading to an action. I was able to load a level using it, but this script works for any name (plus "Enter") that's typed in. How do I get the engine to recognize only one particular word and no other?
    Code (csharp):
    1. // Shows how to read typing input from the keyboard
    2. // (eg. the user entering his name).
    3. // You need to attach this script to a GUIText object.
    4. function Update () {
    5.     for (var c : char in Input.inputString) {
    6.         // Backspace - Remove the last character
    7.         if (c == "\b") {
    8.             if (guiText.text.Length != 0)
    9.                 guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);
    10.         }
    11.         // End of entry
    12.         else if (c == "\n") {
    13.            Application.LoadLevel ("FPSTutorial1");
    14.         }
    15.         // Normal text input - just append to the end
    16.         else {
    17.             guiText.text += c;
    18.         }
    19.     }
    20. }
    Let's say I wanted to use the word "wolf" as a password. I tried to change the else if statement to (c=="wolf\n") and (c=="wolf" + "\n"), among other manipulations, but I don't know what I'm doing. :cry:

    Am I even close, using this particular inputString script? Any help is greatly appreciated!
    :)
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    What you want to do is accumulate the text input into a string and then use something like a regular expression to see if there is a match to your secret code.

    I had this laying around, I'm really sorry I didn't have time to test it or turn it into JavaScript but it is pretty straight forward and I think it'll be useful and better than nothing...

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Text.RegularExpressions;
    4.  
    5. public class CheatInput : MonoBehaviour
    6. {
    7.     private string accumulatedCheatInput;
    8.     private const int cheatInputMaxLength = 20;
    9.    
    10.     void Update()
    11.     {
    12.         // You may only want to handle cheat input when the game is paused or when a special key is being held down.
    13.         // In this example we just do it all the time.
    14.         HandleCheatInput();
    15.     }
    16.    
    17.     void HandleCheatInput()
    18.     {
    19.         if (Input.inputString.Length > 0)
    20.         {
    21.             accumulatedCheatInput += Input.inputString;
    22.            
    23.             if (DidEnterCheat("testcheat"))
    24.             {
    25.                 Debug.Log("Test cheat entered at time " + Time.time + "!");
    26.             }
    27.            
    28.             // put as many DidEnterCheat() blocks as you like
    29.            
    30.             if (accumulatedCheatInput.Length > cheatInputMaxLength)
    31.                 accumulatedCheatInput = accumulatedCheatInput.Substring(accumulatedCheatInput.Length - cheatInputMaxLength);
    32.         }
    33.     }
    34.    
    35.     bool DidEnterCheat(string cheatCode)
    36.     {
    37.         Match match = Regex.Match(accumulatedCheatInput, cheatCode);
    38.        
    39.         if (match.Success)
    40.         {          
    41.             accumulatedCheatInput = "";
    42.             return true;
    43.         }
    44.        
    45.         return false;
    46.     }
    47. }
    48.  
    Maybe some kind person can convert it into JavaScript and post it on the wiki. :D :D :D :D

    -Jon
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You were close...the only real problem was that "c" contained only the most recently entered character, so it will never equal an entire word.

    Try this:

    Code (csharp):
    1.  
    2. var thePassword : String = "wolf";
    3.  
    4. function Update () {
    5.     var inputChar : String = Input.inputString;
    6.  
    7.     // Backspace - Remove the last character
    8.     if (inputChar == "\b") {
    9.         if (guiText.text.Length != 0)
    10.             guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);  
    11.     }
    12.     // End of entry
    13.     else if (inputChar == "\n") {
    14.         // Success
    15.         if (guiText.text == thePassword) {
    16.             Application.LoadLevel ("FPSTutorial1");
    17.         }
    18.         // Failure - reset text
    19.         else {
    20.             guiText.text = "";
    21.         }
    22.     }
    23.     // Normal text input - just append to the end
    24.     else {
    25.         guiText.text += inputChar;
    26.     }
    27. }
    28.  
    I renamed c just 'cause I prefer more descriptive variable names. Otherwise, like I said, you were almost there.

    --Eric
     
  4. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    Thank you for your quick answers! I'll put them to the test and see what happens (first I have to get some sleep). :eek: (Sorry, wrong Emoticon.)
     
  5. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    Thank you again, Eric! I think I owe you some royalties on my million-dollar-selling game. It worked perfectly. I also added DontDestroyOnLoad to the text object and tried the code out on other levels. Still worked well. I wish I had some script I can give you in return, but all I've got is this little gem:
    Code (csharp):
    1.  
    2. function OnCollisionEnter (collision : Collision){
    3.     animation.Play ();
    4. }
    :oops:

    Thanks also, Aarku. Unfortunately, I didn't even know what language it was (Ç# or Boo?--I guessed C#), and I got the message that it couldn't be loaded. I'm just getting the feel of some pretty primitive JS scripting.