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?
// Shows how to read typing input from the keyboard
// (eg. the user entering his name).
// You need to attach this script to a GUIText object.
function Update () {
for (var c : char in Input.inputString) {
// Backspace - Remove the last character
if (c == "\b") {
if (guiText.text.Length != 0)
guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);
}
// End of entry
else if (c == "\n") {
Application.LoadLevel ("FPSTutorial1");
}
// Normal text input - just append to the end
else {
guiText.text += c;
}
}
}
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.
Am I even close, using this particular inputString script? Any help is greatly appreciated!
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…
using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;
public class CheatInput : MonoBehaviour
{
private string accumulatedCheatInput;
private const int cheatInputMaxLength = 20;
void Update()
{
// You may only want to handle cheat input when the game is paused or when a special key is being held down.
// In this example we just do it all the time.
HandleCheatInput();
}
void HandleCheatInput()
{
if (Input.inputString.Length > 0)
{
accumulatedCheatInput += Input.inputString;
if (DidEnterCheat("testcheat"))
{
Debug.Log("Test cheat entered at time " + Time.time + "!");
}
// put as many DidEnterCheat() blocks as you like
if (accumulatedCheatInput.Length > cheatInputMaxLength)
accumulatedCheatInput = accumulatedCheatInput.Substring(accumulatedCheatInput.Length - cheatInputMaxLength);
}
}
bool DidEnterCheat(string cheatCode)
{
Match match = Regex.Match(accumulatedCheatInput, cheatCode);
if (match.Success)
{
accumulatedCheatInput = "";
return true;
}
return false;
}
}
Maybe some kind person can convert it into JavaScript and post it on the wiki.
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:
var thePassword : String = "wolf";
function Update () {
var inputChar : String = Input.inputString;
// Backspace - Remove the last character
if (inputChar == "\b") {
if (guiText.text.Length != 0)
guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);
}
// End of entry
else if (inputChar == "\n") {
// Success
if (guiText.text == thePassword) {
Application.LoadLevel ("FPSTutorial1");
}
// Failure - reset text
else {
guiText.text = "";
}
}
// Normal text input - just append to the end
else {
guiText.text += inputChar;
}
}
I renamed c just 'cause I prefer more descriptive variable names. Otherwise, like I said, you were almost there.
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:
function OnCollisionEnter (collision : Collision){
animation.Play ();
}
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.