- Home /
Need help using GUI.Button/ input storage
It's actually probably more of a javascript question. Basically, I am trying to write a script to apply to a "Safe" object, which the player must open using a number combination.
So I set up a script with a keypad that comes up on the screen when the player hits the designated action button. It's just a bunch of simple GUI.Buttons. I'm trying to figure out how I can store the numbers that the player punches in through the keypad into an array, and check to see if it is the right combination. I've got a basic psudo-code for it, but I'm not very good with javascript, so I was wondering if anyone has experience with this type of script, or if they have any tips?
Here is what I have now (or at least what is referring for to the keypad. it's kind of a mixture between psudo-code and javascript right now... sorry.):
var intarray = int[4];
boolean right = false;
function buttonListener(int x)
{
if (button1 == pressed)
intarray[x] = 1;
else if (button2 == pressed)
intarray[x] = 2;
else if (button3 == pressed)
intarray[x] = 3;
else if (button4 == pressed)
intarray[x] = 4;
else if (button5 == pressed)
intarray[x] = 5;
else if (button6 == pressed)
intarray[x] = 6;
else if (button7 == pressed)
intarray[x] = 7;
else if (button8 == pressed)
intarray[x] = 8;
else if (button9 == pressed)
intarray[x] = 9;
else if (button0 == pressed)
/ntarray[x] = 0;
}
function OnGUI()
{
if (showGUI)
{
button1 = GUI.Button(new Rect(50,150,50,50), "1");
button2 = GUI.Button(new Rect(100, 150, 50, 50), "2");
button3 = GUI.Button(new Rect(150, 150, 50, 50), "3");
button4 = GUI.Button(new Rect(50, 200, 50, 50), "4");
button5 = GUI.Button(new Rect(100, 200, 50, 50), "5");
button6 = GUI.Button(new Rect(150, 200, 50, 50), "6");
button7 = GUI.Button(new Rect(50, 250, 50, 50), "7");
button8 = GUI.Button(new Rect(100, 250, 50, 50), "8");
button9 = GUI.Button(new Rect(150, 250, 50, 50), "9");
buttonDash = GUI.Button(new Rect(100, 300, 100, 50), "-");
button0 = GUI.Button(new Rect(50, 300, 50, 50), "0");
buttonEnter = GUI.Button(new Rect(50, 350, 150, 50), "Enter");
GUI.Box(new Rect(Screen.width/2-50, 150, 200, 50), "Enter Pin Number");
while(i < 4)
{
buttonListener(i)
i++;
}
if intArray== //code
win
else
lose
for(i=0, i<4, i++)
{
if(array[i]==code[i])
right = true;
else
break;
}
}
All help is welcome :) Thanks
Answer by robertbu · May 06, 2013 at 12:20 AM
Rather than use integers, just use a string. No need for your array or listener code. So if you had a input string:
if (GUI.Button(new Rect(50,150,50,50), "1"))
inputString += "1";
When they press the enter button:
if (GUI.Button(new Rect(50, 350, 150, 50), "Enter")) {
if (inputString == "5426") {
// Do whatever for a correct combinaton
}
else {
inputString = ""; // Clear for next try
// Dispaly error message???
}
}
robertbu, I really like this idea and I am trying to implement it. Here is what I have, and I am just trying to get it to return the result to the console. Any idea why it won't work? I can't really see anything, unless inputString shouldn't be a variable, and is actually something im supposed to import?
var correct = false;
var inputString = "";
function OnGUI()
{
if (showGUI)
{
if (GUI.Button(new Rect(50,150,50,50), "1"))
inputString += "1";
else if (GUI.Button(new Rect(100,150,50,50), "2"))
inputString += "2";
else if (GUI.Button(new Rect(150,150,50,50), "3"))
inputString += "3";
else if (GUI.Button(new Rect(50,200,50,50), "4"))
inputString += "4";
else if (GUI.Button(new Rect(100,200,50,50), "5"))
inputString += "5";
else if (GUI.Button(new Rect(150,200,50,50), "6"))
inputString += "6";
else if (GUI.Button(new Rect(50,250,50,50), "7"))
inputString += "7";
else if (GUI.Button(new Rect(100,250,50,50), "8"))
inputString += "8";
else if (GUI.Button(new Rect(150,250,50,50), "9"))
inputString += "9";
else if (GUI.Button(new Rect(50,300,50,50), "0"))
inputString += "0";
GUI.Box(new Rect(Screen.width/2-50, 150, 200, 50), "Enter Pin Number");
if (GUI.Button(new Rect(50, 350, 150, 50), "Enter")) {
if (inputString == "5317") {
// Do whatever for a correct combinaton
correct = true;
return correct;
}
else {
inputString = ""; // Clear for next try
// Dispaly error message???
GUI.Box(new Rect(Screen.width/2-50, 250, 200, 50), "Try Again");
}
Your keypad/combo logic is fine. Your display logic for your error message has issues. With GUI, you have to display the error message every frame. The way it is structured now, it will only display the message for the single frame the enter is pressed. Here are a few quick and dirty changes to your code to display messages:
#pragma strict
var correct = false;
var inputString = "";
var incorrect = false;
function OnGUI()
{
if (true)
{
if (GUI.Button(new Rect(50,150,50,50), "1"))
inputString += "1";
else if (GUI.Button(new Rect(100,150,50,50), "2"))
inputString += "2";
else if (GUI.Button(new Rect(150,150,50,50), "3"))
inputString += "3";
else if (GUI.Button(new Rect(50,200,50,50), "4"))
inputString += "4";
else if (GUI.Button(new Rect(100,200,50,50), "5"))
inputString += "5";
else if (GUI.Button(new Rect(150,200,50,50), "6"))
inputString += "6";
else if (GUI.Button(new Rect(50,250,50,50), "7"))
inputString += "7";
else if (GUI.Button(new Rect(100,250,50,50), "8"))
inputString += "8";
else if (GUI.Button(new Rect(150,250,50,50), "9"))
inputString += "9";
else if (GUI.Button(new Rect(50,300,50,50), "0"))
inputString += "0";
GUI.Box(new Rect(Screen.width/2-50, 150, 200, 50), "Enter Pin Number");
if (correct)
GUI.Box(new Rect(Screen.width/2-50, 250, 200, 50), "You are correct");
if (incorrect) {
if (inputString == "")
GUI.Box(new Rect(Screen.width/2-50, 250, 200, 50), "Try Again");
else
incorrect = false;
}
if (GUI.Button(new Rect(50, 350, 150, 50), "Enter")) {
if (inputString == "5317") {
// Do whatever for a correct combinaton
correct = true;
//return correct;
}
else {
inputString = ""; // Clear for next try
incorrect = true;
}
}
}
}