room1.asc(131): Error (line 131): Parse error: unexpected 'else'

Started by Mixand, Thu 14/05/2009 12:28:58

Previous topic - Next topic

Mixand

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.");

}

GarageGothic

Code: ags
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.");
  }

Khris

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.

Mixand

Thankyou very much guys. I just started and I've gotten a lot of help.

GarageGothic

Just to clarify, there's no such thing as "proper indentation". There's a multitude of different indent standards, 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.

Khris

I see, sorry. Well, by proper indentation I meant anything that makes clear which two brackets match each other.

SMF spam blocked by CleanTalk