Two text boxes (SOLVED)

Started by sloppy, Fri 10/03/2006 15:02:55

Previous topic - Next topic

sloppy

Here's something I want to try.  At some point in my game an NPC asks my player character a question that he has to get right.  For this a text box GUI come up and I have it like this:
Code: ags

function interface_click(int interface, int button) {
  txtUserInput.GetText(input);
   ParseText (input);
  if (Said("Answer1")) {
    Display("Yes, that's correct.");
    gText.Visible=false;
  
}
 else if (Said("Answer2")) {
    Display("Yes, that's correct.");
    gText.Visible=false;
    }

  else {
    Display("No, that's not right.");
    gText.Visible=false;
        txtUserInput.SetText("");
  }
}

And there are about 4 possible answers that would work.  What I would like but can't figure out is how to have two text boxes where the player has to enter 2 of the 4 answers to get the "correct" display. 


sloppy

Yes I saw this thread.  Maybe I'm missing something but while it's a similar question it doesn't answer my specific question at all.

monkey0506

Maybe (if I understand you correctly), you could do this:

Code: ags
txtUserInputA.GetText(input);
  ParseText(input);
  if (Said("Answer 1")) {
    txtuserInputB.GetText(input);
    ParseText(input);
    if (Said("Answer 2")) {
      Display("Yes, that's correct.");
      gText.Visible = false;
    }
  }


I'm not sure what the exact situation you're trying to achieve here is, but hopefully you can get the basic gist of it from that.

BTW, what version of AGS are you using?  Because if you are using AGS 2.71 or later (2.71 is the latest version, 2.72 is currently in BETA), I would suggest using new-style strings (not necessary, just easier to work with IMO):

Code: ags
Parser.ParseText(txtUserInputA.Text);
  if (Parser.Said("Answer 1")) {
    Parser.ParseText(txtUserInputB.Text);
    if (Parser.Said("Answer 2")) {
      Display("Yes, that's correct.");
      gText.Visible = false;
    }
  }

sloppy

No this isn't quite what I'm looking for.  What I'm hoping is that when the question is asked, the GUI pops up that has 2 text boxes on it.  There are 4 possible answers that can be given but the player only has to provide 2 answers - one in each text box.
Is that possible?

Ashen

You can't have two TextBoxes active at once, so you'll need that thread (or something similar) to get around that.

Then it's just a case of making sure both answers are correct, and you were most of the way there in your first post. You could either directly check the TextBoxes:
Code: ags

string input;
string input2;
txtUserInputA.GetText(input);
txtUserInputB.GetText(input2);
if (StrCaseComp(input, "Answer1") == 0 && StrCaseComp(input2, "Answer3") == 0) {
  // Do Stuff
}

(Or, if you want to use ParseText, something like monkey said).

Or, you could use variables, e.g.:
Code: ags

  if (Said("Answer1")) { // Right answer for first TextBox
    Display("Question 1 correct.");
    GotAns1 = 1
}
  // Etc - other 'else' conditions  for first TextBox

  if (Said("Answer3")) { // Right answer for second TextBox
    Display("Question 2 correct.");
    GotAns2 = 1
}
  // Etc - other 'else' conditions  for second TextBox
  if (GotAns1 == 1 && GotAns2 == 1) {
    //Do stuff
  }
  else {
    // Not got both right, reset for another go
    GotAns1 = 0;
    GotAns2 = 0;
  }
  gText.Visible = false;
I know what you're thinking ... Don't think that.

sloppy

Yes, that works.  Thank you!

sloppy

Actually I have one additional problem.Ã,  This scripting works:
Code: ags

function interface_click(int interface, int button) {
string input;
string input2;
txtUserInput.GetText(input);
txtUserInputB.GetText(input2);
if (StrCaseComp(input, "Answer1") == 0 && StrCaseComp(input2, "Answer2") == 0) {
Ã,  Ã,  Ã,  Ã,  Display("Those answers are correct.");
Ã,  Ã,  gText.Visible=false;
Ã,  }

Ã,  else {
Ã,  Ã,  Display("Those are incorrect.");
Ã,  Ã,  gText.Visible=false;
Ã,  Ã,  txtUserInput.SetText("");
Ã,  Ã,  txtUserInputB.SetText("");
Ã,  }
}

But if one of the answers is incorrect and the player starts again and brings the GUI back up, you can no longer type anything into the text boxes.Ã,  And nothing happens when you push "enter".Ã,  The GUI stays and you have to quit the game to start again.

Ashen

Are you using the script from the other thread (or something like it), to disable one or both of the TextBoxes? If so, are you ENABLING them again, before you turn the GUI back on?
I know what you're thinking ... Don't think that.

sloppy

#9
Yeah, that was it.Ã,  Thanks for pointing that out.Ã,  I was going crazy trying to figure it out.


sloppy

Sorry about this but I keep finding new things wrong as I try different possibilities.
Like I mentioned, I want 4 possible answers but only 2 are necessary to give.  But I would like it if you can put any of the answers in any of the textboxes to get the right answer.  Right now if Answer 1 is put into the second textbox, and Answer 2 is put into the first, you get the "Incorrect" display. 
I'm trying to figure out the scripting so that 2 out of 4 answers are needed but in either textbox.

Here's what I tried but doesn't work:
Code: ags

function interface_click(int interface, int button) {
string input;
string input2;
string input3;
string input4;
txtUserInput.GetText(input);
txtUserInput.GetText(input2);
txtUserInput.GetText(input3);
txtUserInput.GetText(input4);
txtUserInputB.GetText(input);
txtUserInputB.GetText(input2);
txtUserInputB.GetText(input3);
txtUserInputB.GetText(input4);
if (StrCaseComp(input, "Answer1") == 0 && StrCaseComp(input2, "Answer2") == 0) && StrCaseComp(input3, "Answer3") == 0 && StrCaseComp(input4, "Answer4") == 0) ) {
        Display("Those answers are correct.");
    gText.Visible=false;
  }

  else {
    Display("Those are incorrect.");
    gText.Visible=false;
    txtUserInput.SetText("");
    txtUserInputB.SetText("");
  }
}

So any help would be appreciated.

Ashen

#11
That's pretty over-complicated with all those strings. Not to mention all 4 of them will be the answer in txtUserInputB so any comparision is a bit useless.

What if you try it as you had it, but add an OR condition to allow for the answer to be in either TextBox, e.g. (untested):
Code: ags

  if ((StrCaseComp(input, "Answer1") == 0 || StrCaseComp(input2, "Answer1") == 0) && (StrCaseComp(input, "Answer2") == 0 || StrCaseComp(input2, "Answer2") == 0)) {
    //I.e., if "Answer1" is in either box, and "Answer2" is in the other
        Display("Those answers are correct.");
    gText.Visible=false;
  }


You might need to fiddle with the OR conditions a little, or split it up into two conditions, e.g. (pseudocode):
Code: ags

  if(Answer1 in either) {
    if (Answer2 in either) {
      Display("Correct!");
      gText.Visible = false;
    }
  }
I know what you're thinking ... Don't think that.

sloppy

Okay, I see what you're doing, but you had the brackets a bit off.
Code: ags

if ((StrCaseComp(input, "Answer1") == 0 || StrCaseComp(input2, "Answer1") == 0) && (StrCaseComp(input, "Answer2") == 0 || StrCaseComp(input2, "Answer2") == 0)) {
Display("Those are correct.");
}


This works for two possible answers, but when I continue this line with Answer3 & Answer4, it says the line is too long.Ã,  Doing it this way doesn't work either:
Code: ags

if ((StrCaseComp(input, "Answer1") == 0 || StrCaseComp(input2, "Answer1") == 0) && (StrCaseComp(input, "Answer2") == 0 || StrCaseComp(input2, "Answer2") == 0)) {
Display("Those are correct.");
}
else if ((StrCaseComp(input, "Answer3") == 0 || StrCaseComp(input2, "Answer3") == 0) && (StrCaseComp(input, "Answer4") == 0 || StrCaseComp(input2, "Answer4") == 0)) {
Display("Those are correct.");
}

I knew it wouldn't work because this doesn't allow for any combination of the 4 answers, but I thought I'd try it.
Is there a way to do this?

Ashen

#13
Ah, yes - I copy/pasted bits of it, must've accidentally included the brackets.

So 3 & 4 are also valid? Sorry, I though they were the 'Wrong' answers - really must read things before posting.
Having any two of the four might be a bit complex for a single condition, so what if you try using variables as well, e.g.:
Code: ags

int GotRight;
if (StrCaseComp(input, "Answer1") == 0 || StrCaseComp(input2, "Answer1") == 0) GotRight ++;
if (StrCaseComp(input, "Answer2") == 0 || StrCaseComp(input2, "Answer2") == 0) GotRight ++;
if (StrCaseComp(input, "Answer3") == 0 || StrCaseComp(input2, "Answer3") == 0) GotRight ++;
if (StrCaseComp(input, "Answer4") == 0 || StrCaseComp(input2, "Answer4") == 0) GotRight ++;
//i.e. will add one for each correct answer.

if (GotRight == 2) {
  Display ("Those are correct.");
  gText.Visible = false;
}
else {
  Display("Those are incorrect.");
  gText.Visible=false;
  txtUserInput.SetText("");
  txtUserInputB.SetText("");
}
I know what you're thinking ... Don't think that.

sloppy

That works nicely!  Thank you!

sloppy

Sorry to bring this topic up again, but I had one more idea and just a bit of a problem making it happen.Ã,  I'd like there to be a second (maybe even a third) textbox GUI that could pop up depending on which question the player chooses.Ã, 
Code: ags

int GotRight;
int GotRighta;
if (StrCaseComp(input, "Answer1") == 0 || StrCaseComp(input2, "Answer1") == 0) GotRight ++;
if (StrCaseComp(input, "Answer2") == 0 || StrCaseComp(input2, "Answer2") == 0) GotRight ++;
if (StrCaseComp(input, "Answer3") == 0 || StrCaseComp(input2, "Answer3") == 0) GotRight ++;
if (StrCaseComp(input, "Answer4") == 0 || StrCaseComp(input2, "Answer4") == 0) GotRight ++;
//i.e. will add one for each correct answer.

if (GotRight == 2) {
Ã,  Display ("Those are correct.");
Ã,  gText.Visible = false;
}
else {
Ã,  Display("Those are incorrect.");
Ã,  gText.Visible=false;
Ã,  txtUserInput.SetText("");
Ã,  txtUserInputB.SetText("");
}


if (StrCaseComp(input, "Answer1a") == 0 || StrCaseComp(input2, "Answer1a") == 0) GotRighta ++;
if (StrCaseComp(input, "Answer2a") == 0 || StrCaseComp(input2, "Answer2a") == 0) GotRighta ++;
if (StrCaseComp(input, "Answer3a") == 0 || StrCaseComp(input2, "Answer3a") == 0) GotRighta ++;
if (StrCaseComp(input, "Answer4a") == 0 || StrCaseComp(input2, "Answer4a") == 0) GotRighta ++;

if (GotRighta == 2) {
Ã,  Display ("Those are correct.");
Ã,  gSecondText.Visible = false;
}
else {
Ã,  Display("Those are incorrect.");
Ã,  gSecondText.Visible=false;
Ã,  UserInput.SetText("");
Ã,  UserInputB.SetText("");
}


But the way I've got it, putting in the correct answers for the first GUI with result in the "correct" Display.Ã,  And then the "incorrect" Display pops up after because the second GUI conditions were not met.Ã,  It seems it should be a simple matter of keeping the actions to one GUI or the other at a time, but I just can't think of it.

Ashen

#16
You've got all of that in interface_click still, right?
If so, it's just a matter of adding conditons to seperate the two GUIs:
Code: ags

function interface_click (int interface, int button) {
  if (interface == TEXT) {
    string input, input2;
    int GotRight;
    txtUserInput.GetText(input);
    txtUserInputB.GetText(input2);
    if (StrCaseComp(input, "Answer1") == 0 || StrCaseComp(input2, "Answer1") == 0) GotRight ++;
    if (StrCaseComp(input, "Answer2") == 0 || StrCaseComp(input2, "Answer2") == 0) GotRight ++;
    if (StrCaseComp(input, "Answer3") == 0 || StrCaseComp(input2, "Answer3") == 0) GotRight ++;
    if (StrCaseComp(input, "Answer4") == 0 || StrCaseComp(input2, "Answer4") == 0) GotRight ++;
    //i.e. will add one for each correct answer.
 
    if (GotRight == 2) {
      Display ("Those are correct.");
      gText.Visible = false;
    }
    else {
      Display("Those are incorrect.");
      gText.Visible=false;
      txtUserInput.SetText("");
      txtUserInputB.SetText("");
    }
  }
  else if (interface == SECONDTEXT) {
    string input, input2;
    int GotRight;
    UserInput.GetText(input);
    UserInputB.GetText(input2);
    if (StrCaseComp(input, "Answer1a") == 0 || StrCaseComp(input2, "Answer1a") == 0) GotRight ++;
    if (StrCaseComp(input, "Answer2a") == 0 || StrCaseComp(input2, "Answer2a") == 0) GotRight ++;
    if (StrCaseComp(input, "Answer3a") == 0 || StrCaseComp(input2, "Answer3a") == 0) GotRight ++;
    if (StrCaseComp(input, "Answer4a") == 0 || StrCaseComp(input2, "Answer4a") == 0) GotRight ++;
    //i.e. will add one for each correct answer.
 
    if (GotRight == 2) {
      Display ("Those are correct.");
      gSecondText.Visible = false;
    }
    else {
      Display("Those are incorrect.");
      gSecondText.Visible=false;
      UserInput.SetText("");
      UserInputB.SetText("");
    }
  }
}


(Check through it before you run it, to make sure I've got all the GUI/TextBox names right.)
I know what you're thinking ... Don't think that.

sloppy

This may be a dumb question, but what's TEXT and SECONDTEXT? GUI names?

Ashen

#18
Yup. They should be the names you gave the two guis to get the Script-o-names gText and gSecondtext. Unless they're just example names? Basically they'll be the usual name you refer to the GUI as (e.g. gText) without the first g and all capitalised.
You could also use the GUI.ID property if you'd rather, e.g. if (interface == gText.ID).
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk