Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: jmjm on Sun 07/08/2016 23:26:11

Title: Text Parser error: Too many nested if/else statements
Post by: jmjm on Sun 07/08/2016 23:26:11
Hi, all!

I'm making a text parser game and while writing my global parser commands I've finally gotten the error "too many nested if/else statements". Unfortunately I'm not even close to writing all my parser commands for my game!

I read a post with a similar issue http://www.adventuregamestudio.co.uk/forums/index.php?topic=41044.0 (http://www.adventuregamestudio.co.uk/forums/index.php?topic=41044.0) and I tried making separate functions for my text parser function to split up how many if/else statements are in each, but I can't seem to make the game parse the text beyond the first function.

I tried having my first text parser function have an else statement that stores the String text like so:

Code (ags) Select

else
    input2 = input1;


and then have the following function include:

Code (ags) Select

if (input2 == input1)
{
    Parser.ParseText(input2);

        if (Parser.Said("eat apple."))
            {
              Display("Yummy!");
            }

        else if (Parser.Said("eat orange."))
            {
              // etc.
            }

        else
            CallRoomScript(1);
}


But that doesn't seem to do anything. The game runs and recognizes the parses in the first function, but nothing in the second function, nor does it call the room script. When running the game, when I enter "eat apple" the game returns nothing and no error message, just continues to run as before.

I'm self-taught so apologies if I've asked something dumb or didn't format the stuff right. The code sections aren't copy-pasted in, I'm just showing the basic stuff in hopes that it makes things easier to see.

Any help (or suggestions of game engines that would be better suited!) would be much appreciated! :)
Title: Re: Text Parser error: Too many nested if/else statements
Post by: Snarky on Mon 08/08/2016 02:41:36
I've never actually used the AGS parser, but I'm pretty sure the problem is here:

Quote from: jmjm on Sun 07/08/2016 23:26:11
Code (ags) Select

else
    input2 = input1;


I'm not sure quite what you're trying to do here, but it doesn't look right. ;)
(You already have the string as input1; you don't need another copy of it. If you just want to keep track of whether something is true or false, use a bool flag.)

This bit also looks pretty dodgy:

Quote from: jmjm on Sun 07/08/2016 23:26:11
Code (ags) Select
if (input2 == input1)
{
    Parser.ParseText(input2);

The issue here is that it looks like you're calling Parse.ParseText() a second time. There's really no point, if you've already run it with input1. The Parser won't reset itself until you call it with some other argument, and it could even conceivably be that calling the parser twice in a single cycle might break it.

(Edit: D'oh! How did I miss that? The immediate cause of the error is that you can't use == to compare two strings. Still, I think the solution suggested here is better than just changing it to String.CompareTo().)

First of all: Do you really need the nested if/else-ifs? Presumably there's only one case that will return true for the Parser.Said() tests, so you could really just have a long list of if statements, without any elses. The only problem would be the final CallRoomScript() fallback, but you could always set a flag for whether it should run or not.

Here's another alternative, which is how I might structure the code:

Code (ags) Select

bool parse1()
{
  if(Parser.Said("eat apple")
  {
    Display{"Yummy!");
  }
  else if(Parser.Said("eat orange")
  {
    // ...
  }
  // other cases
  else
    return false;
  return true;
}

bool parse2()
{
  // Same structure as parse1()
}

// parse3(), parse4(), etc. As many as you need

// Place in the script where you check the parser:
{
  // ...
  Parser.ParseText(input1);
  if(!parse1() && !parse2()) // etc.
  {
    CallRoomScript(1);
  }
}
Title: Re: Text Parser error: Too many nested if/else statements
Post by: Crimson Wizard on Mon 08/08/2016 03:25:07
Quote from: Snarky on Mon 08/08/2016 02:41:36
Edit: D'oh! How did I miss that? The immediate cause of the error is that you can't use == to compare two strings.

You can, AGS supports this operator for strings. That is mentioned in CompareTo manual entry.
Title: Re: Text Parser error: Too many nested if/else statements
Post by: Snarky on Mon 08/08/2016 08:01:34
Ah bugger! I even had a whole long argument about that (and why I think it's a bad idea) a while ago. :-[

In my defense, it was almost 4 in the morning when I wrote that...
Title: Re: Text Parser error: Too many nested if/else statements
Post by: jmjm on Mon 08/08/2016 11:08:06
Hooray!

Snarky, I tried your suggestion on how to structure the code and it worked like a charm. I'd been struggling for days! (nod)

Thanks for the feedback, folks. Very much appreciated. Massive sigh of relief! :-D