if if else scripting woes

Started by ManicMatt, Thu 29/01/2015 14:39:23

Previous topic - Next topic

ManicMatt

Help! Why does my game play the very last line "Why are you showing me this crap?" when the inv item 16 is used? It doesn't do that with the other two items.

Code: ags
function cPainter_UseInv()
{
  if (player.ActiveInventory == inventory[16]) {
    cEgo.Walk(120, 124, eBlock);
    cEgo.LockView(18);
    cEgo.Animate(0, 20, eOnce, eBlock);
    cEgo.UnlockView();
    character[0].FaceLocation(12, 124);
    cPainter.Say("&10 What's this?");
    cEgo.Say("&191 Pea and egg soup with croutons.");
    cPainter.Say("&11 Nice!");
    cPainter.LockView(76);
    cPainter.Animate(0, 40, eOnce, eBlock);
    cPainter.UnlockView();
    cPainter.Say("&12 Ugh, I don't feel so good.");
    cPainter.Say("&13 I NEED TO GO TOILET!");
    player.LoseInventory(inventory[16]);
    player.AddInventory(inventory[17]);
    character[22].LockView(75);
    character[PAINTER].Walk(-40, 132,eBlock,eAnywhere);  
    character[EGO].Walk(88, 145,eBlock,eAnywhere); 
    SetCharacterView(EGO, 24);
    AnimateCharacter(EGO, 0, 4, 9);
    Wait(30);
    ReleaseCharacterView(EGO);
    object[0].Visible = false;
    SetCharacterView(EGO,  61);
    character[EGO].Walk(66, 139, eBlock, eAnywhere);
    character[EGO].Walk(55, 149, eBlock, eAnywhere);
    character[EGO].Walk(42, 290, eBlock, eAnywhere);
    character[EGO].Walk(42, 340, eBlock, eAnywhere);
    cPainter.Say("&9 It's no good, I have to go NOW!");
    player.ChangeRoom(13, 660, 125); 
  }
  if (player.ActiveInventory == inventory[14]) {
    cEgo.Walk(120, 124, eBlock);
    character[0].FaceLocation(50, 127);
    cPainter.Say("&17 An old half-eaten stale sandwhich? You trying to poison me?!");
  }
  if (player.ActiveInventory == inventory[15]) {
    cEgo.Walk(120, 124, eBlock);
    character[0].FaceLocation(50, 127);
    cPainter.Say("&18 Soup? Have you got any croutons to go with that?");
  }
  else
  {
    cEgo.Walk(120, 124, eBlock);
    character[0].FaceLocation(50, 127);
    cPainter.Say("&14 Why are you showing me this crap?");
  }
}

Gilbert

As that particular line is spoken in the 'else' part of the 'if (player.ActiveInventory == inventory[15])' clause, that part will be executed whenever the item used is not #15, as in the case when the item used is #16. I don't know why it's not spoken(as you mentioned) when the item used is #14 though.

This happens for item #16 specifically because the 'player.ChangeRoom()' function will not be executed immediately at where it's called, as stated in the manual:
QuoteIMPORTANT: This command does not change the room immediately; instead, it will perform the actual room change once your script function has finished (This is to avoid problems with unloading the script while it is still running). This means that you should not use any other commands which rely on the new room (object positionings, and so on) after this command within the same function.

If that else part is to be executed only when the item used is not either #14, 15 or 16, you should do a proper chain of if-else if's:
Code: ags

  if (player.ActiveInventory == inventory[16]) {
    //Blah!
  } else if (player.ActiveInventory == inventory[14]) {
    //Blah Blah!!
  } else if (player.ActiveInventory == inventory[15]) {
    //Blah Blah Blah!!!
  } else {
    //Blah Blah Blah Blah!!!!
  }
}


Snarky

Please note how I've reformatted your code and added the code tag, and how much easier that makes it to understand the logical structure of your code. Always do this!

To answer your question, are you sure it doesn't do the same thing with inventory[14]? Because what's going on here is that the final "else" only applies to the last if: if (player.ActiveInventory == inventory[15]). So as long as it's not 15, it will run the else block. What you want is "else if":

Code: ags
function cPainter_UseInv()
{
  if (player.ActiveInventory == inventory[16]) {
  // ...
  }
  else if (player.ActiveInventory == inventory[14]) {
  // ...
  }
  else if (player.ActiveInventory == inventory[15]) {
  // ...
  }
  else {
  // ...
  }
}


What this does is to make sure that no part of the code runs if any of the other parts has already run.

(Gah! Shouldn't have got distracted in the middle of posting this!)

ManicMatt

Ah I am wrong, it DOES say the bottom line after inv item 14 is used! Sorry, I'll try to remember that Snarky, thanks for editing it.

Thanks Iceboty and Snarky, I tried using else if at the bottom before at one try - wrong way round! Doh!

I should be able to apply this new (or is that forgotten!) knowledge to another character in my game that I'd missed interactions for!

Just when you think it's done and dusted... (wrong)

SMF spam blocked by CleanTalk