Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: BlueAngel on Tue 29/12/2009 17:00:52

Title: getting too many points (solved)
Post by: BlueAngel on Tue 29/12/2009 17:00:52
Could someone please tell me what I did wrong here?
It seems like the player can get points for taking cheese again tho its already in the inventory. The player dont get more cheese in the inventory - just more points.

function oFridgeOpen_Interact()
{
 if (player.HasInventory(iCheese)){
     Display("You already taken some cheese out of the fridge");
 }
 else
     player.AddInventory(iCheese);
     Display("You take some cheese out of the fridge");
     GiveScore(5);
}
Title: Re: getting too many points
Post by: Calin Leafshade on Tue 29/12/2009 17:03:21
You need to put curly brackets around the 'else' action like this//


function oFridgeOpen_Interact()
{
  if (player.HasInventory(iCheese)){
      Display("You already taken some cheese out of the fridge");
  }
  else
{
      player.AddInventory(iCheese);
      Display("You take some cheese out of the fridge");
      GiveScore(5);
}
}



otherwise the only bit being affected by else is the first command after it.
Title: Re: getting too many points
Post by: BlueAngel on Tue 29/12/2009 17:14:59
 :D
It worked! Thanks so for helping me!
Title: Re: getting too many points (solved)
Post by: Khris on Tue 29/12/2009 18:18:24
It's beneficial to get indentation right from the start. There are several styles though, my favorite one is this:

function oFridgeOpen_Interact() {
  if (player.HasInventory(iCheese))
    Display("You already taken some cheese out of the fridge");
  else {
    player.AddInventory(iCheese);
    Display("You take some cheese out of the fridge");
    GiveScore(5);
  }
}

(I also removed the brackets around the single command after the if, beginners might leave them there.)

Others prefer moving the opening bracket to the start of the next line so corresponding brackets are always at the same column:

function oFridgeOpen_Interact()
{
  if (player.HasInventory(iCheese))
    Display("You already taken some cheese out of the fridge");
  else
  {
    player.AddInventory(iCheese);
    Display("You take some cheese out of the fridge");
    GiveScore(5);
  }
}


This can save you loads of time when debugging simple errors. :)

I always start shuddering when I see something like this:

}
}
}


So guys, no matter what you use in your own code, if you post scripts here, please use proper indentation. ;)
Title: Re: getting too many points (solved)
Post by: Calin Leafshade on Tue 29/12/2009 18:38:27
sorry dad..  :P