Text parser riddle puzzle [SOLVED]

Started by mr.me, Sat 04/04/2009 04:36:08

Previous topic - Next topic

mr.me

I couldn't find this anywhere, there was a text parser section in the manual, but I couldn't figure it out.

Here's what I want:

Somewhere in the game there is a riddle, now you have to answer the riddle in a certain spot.
Now, player goes to spot, talks to item, and a text parser appears. If they answer right, they get the item and if not then they get a message like "try again". Also, I read this in the manual "InputBox command (but it has quite a short line length)." the answer to the riddle is short, so I want to use that instead of making a GUI if that's possible.

How do I go about doing this?

Trent R

Either of them would work. Input box is probably the easiest, but I like a custom Gui the best (cause then it'll fit in with the rest of your game more easily--unless none of that is customized)

The manual entry tells you exactly what you need to know. Game.InputBox returns a string, so, do something like this, along with a region (how you do 'on a certain spot' types of code):
Code: ags

//at the top of the script, outside any function
bool riddlecorrect=false;
String input;

//under the region code (walk onto)
Display("What is 6*7?"); //or whatever the riddle is
input=Game.InputBox("");


//under the room's rep_exec
if (input.CompareTo("42")==0) { //CompareTo will make it so it isn't case sensitive, but read the notes below
  riddlecorrect=true;
  input=null;
  //whatever happens when the riddle is correct, whether moving an object, changeroom, etc
}


Note: This code is untested, and incomplete. You'll want to add a check underneath the region code one whether or not it's been answered, otherwise you'll get an input box everytime you walk onto it. Or you could disable it(region.Enabled=false;) You'll also want to try to account for different types of answers (depending on the riddle). I tried to accomadate slightly for this by using the String.CompareTo, but you may have to take it further.



Of course, someone smarter will probably come along soon and show a better way.  :D
~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

OneDollar

#2
My version of the input box method:
Code: ags

//define your variables, you might need to make AnswerCorrect global to read it in other rooms
String Answer = "";  //The text the player types in
bool AnswerCorrect = false;  //whether the riddle has been answered correctly or not

//In the region code, or the lookat code or wherever you want to call the riddle
if (AnswerCorrect == false) {
  //Not already answered the riddle
  Display("I have a riddle for you, young squire, what is six multiplied by seven?");
  Answer = Game.InputBox("!What is 6 x 7?");
  //The exclamation marks gives you a cancel option as well, and adding the text shows the
  //question as part of the input box (used as a recap here)
  if (answer == "") {
    //A blank string will be returned if the player clicks cancel or doesn't enter anything
    player.Say("Erm... I'll get back to you on that");
  }else{
    Answer = Answer.LowerCase();
    //It's important that you set everything to upper or lowercase so that the answer isn't
    //case sensitive
    if ((Answer == "42") || (Answer == "forty two")) {
      //if the player answers "42" or "forty two" (or any capitalisation of it)...
      Display("That's correct! Have a cookie");
      player.AddInventory(iCookie);
      AnswerCorrect = true;
    }else{
      //entered something different
      Display("Sorry, that's not right, try again");
    }
  }
}else{
  //already answered
  Display("No more riddles today, go away!");
}


Pretty much the same as what Trent wrote, but with everything in one function and a couple of stumbling blocks taken out (case sensitivity, multiple answers that mean the same thing).

It would probably look nicer to make your own GUI instead of InputBox (which is mainly designed for debugging), but the principle is pretty much the same. You just have to show your GUI instead of the InputBox and check the entered answer when the player clicks the OK button on the GUI. You don't need the parser for this kind of thing, look up InputBox and string functions in the manual instead.

mr.me

Thanks Trent R's code worked perfectly.

Trent R

OneDollar's is actually better. A lot better.

~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

paolo

Quote from: Trent R on Tue 07/04/2009 00:00:47
OneDollar's is actually better. A lot better.

~Trent

Yes it is, except for the spelling mistake - it ought to be "forty two" (no "u", and it's hyphenated if you want to be really pedantic: "forty-two").

Sorry, OneDollar, I wouldn't normally pick up on this, but correct spelling is vital in games with text input.

OneDollar

Oh yeah, thanks for pointing that out Paolo. I guess the line should really be
Code: ags
if ((Answer == "42") || (Answer == "forty two") || (Answer == "forty-two")) {


Anyway, code corrected.

Dualnames

Quote from: OneDollar on Tue 07/04/2009 14:07:08
Oh yeah, thanks for pointing that out Paolo. I guess the line should really be
Code: ags
if ((Answer == "42") || (Answer == "forty two") || (Answer == "forty-two")) {


Anyway, code corrected.

You sure knew I'd dive in to post..yay for that..
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

SMF spam blocked by CleanTalk