getting too many points (solved)

Started by BlueAngel, Tue 29/12/2009 17:00:52

Previous topic - Next topic

BlueAngel

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.

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

Calin Leafshade

You need to put curly brackets around the 'else' action like this//


Code: ags
 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.


Khris

It's beneficial to get indentation right from the start. There are several styles though, my favorite one is this:

Code: ags
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:

Code: ags
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:

Code: ags
}
}
}


So guys, no matter what you use in your own code, if you post scripts here, please use proper indentation. ;)

Calin Leafshade


SMF spam blocked by CleanTalk