Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Mixand on Thu 14/05/2009 12:28:58

Title: room1.asc(131): Error (line 131): Parse error: unexpected 'else'
Post by: Mixand on Thu 14/05/2009 12:28:58
Can somebody please tell me why I'm getting this error?
"room1.asc(131): Error (line 131): Parse error: unexpected 'else'"


function hHotspot5_UseInv()

{

if(player.ActiveInventory==iShards)

Jason.Walk(83, 137, eBlock);

Display("You scrape the two shards to create a spark and make a fire.");

player.LoseInventory(iShards);

Leaves.Visible = false;

fire.Visible = true;

}

function hHotspot4_UseInv()

{

if(player.ActiveInventory==iLeaves)

Jason.Walk(83, 137, eBlock);

Display("You put the leaves into the empty campfire.");

player.LoseInventory(iLeaves);

Leaves.Visible = true;

}

else Display("You can't throw that into the river.");

}
Title: Re: room1.asc(131): Error (line 131): Parse error: unexpected 'else'
Post by: GarageGothic on Thu 14/05/2009 12:31:53
function hHotspot4_UseInv() {
  if(player.ActiveInventory==iLeaves) { //you were missing a  bracket here
    Jason.Walk(83, 137, eBlock);
    Display("You put the leaves into the empty campfire.");
    player.LoseInventory(iLeaves);
    Leaves.Visible = true;
    }
  else Display("You can't throw that into the river.");
  }
Title: Re: room1.asc(131): Error (line 131): Parse error: unexpected 'else'
Post by: Khris on Thu 14/05/2009 12:40:40
Mixand:

If you want to execute more than one command only if a certain condition is true, you have to enclose them in { and }.
The same goes for the contents of a function; after "function name(...)" you open it using a {, put all the commands that form the contents of the function, then close it with a }.

Proper indentation helps greatly with that.
Look at GarageGothic's code for an example of proper indentation (although the very last } needs to be moved to the left).
This will increase readability and help prevent errors.

I'd also recommend using the code tag.
Title: Re: room1.asc(131): Error (line 131): Parse error: unexpected 'else'
Post by: Mixand on Thu 14/05/2009 12:58:17
Thankyou very much guys. I just started and I've gotten a lot of help.
Title: Re: room1.asc(131): Error (line 131): Parse error: unexpected 'else'
Post by: GarageGothic on Thu 14/05/2009 13:57:36
Just to clarify, there's no such thing as "proper indentation". There's a multitude of different indent standards (http://en.wikipedia.org/wiki/Indentation_style), and it's highly subjective which one is more readable (I prefer what the Wikipedia article calls "banner style", though I had no idea there was a name for it - it's just what seemed most natural when I started scripting in AGS).
Your main concern should be to choose a style that makes the logic of the code clearer for you, and hence makes the code easier to troubleshoot. But of course it's a good idea to use a structure that others can also understand - especially if you plan on posting code on the forums when asking for help.
Title: Re: room1.asc(131): Error (line 131): Parse error: unexpected 'else'
Post by: Khris on Thu 14/05/2009 16:54:23
I see, sorry. Well, by proper indentation I meant anything that makes clear which two brackets match each other.