Conditional not working w/using item on object.

Started by Duckbutcher, Mon 20/02/2017 14:32:25

Previous topic - Next topic

Duckbutcher

Hi, this is kind of difficult to explain but here I go..!

I've got a fire as an object in a room, and my character can throw various items into it. However, at one point he'll be carrying a large item (item 14 in the below script), and I want him to not be able to use any other inv items on the fire while he is carrying this item. Here is a sample of the script I've been using - this is the script for using item 6 on the fire, though the script is near identical for the other items.

Code: ags


  //USE INV
    else if(UsedAction(eGA_UseInv)) {
          if(player.ActiveInventory==inventory[6]){
            if (cEgo.InventoryQuantity[14] > 0){
            player.Say("Not while I'm carrying this thing.");
          }
          
      else player.Walk(280, 130, eBlock);
      player.FaceDirection(eDirectionRight);
      cEgo.LockView(48);
      player.LoseInventory(iplun);
      Wait(10);
      obang.SetView(28);
      obang.Visible=true;
      obang.Animate(0, 3, eOnce, eBlock);
      obang.Visible=false;
      Wait(10);
      player.UnlockView();
      cHip.FaceCharacter(cEgo);
      cHip.Say("What was that?!");
      player.FaceCharacter(cHip);
      player.Say("Nothing!");
      player.Say("Fire just burped, that's all.");
      GiveScore(1);
        }
    }



My problem is that he displays the correct dialogue line ("not while I'm carrying this thing") and then goes ahead and does the action anyway. I've done a similar script for most other items as well, and it does the same thing - and then runs the "else unhandled" part of the script too. I fear it might be a closing brace issue, but can't seem to sort it.  I'd appreciate any advice you might have!

Khris

The "else" will only affect the player.Walk() command.
All following commands will run no matter what. You need to group them using curly braces.

Code: ags
  else if(UsedAction(eGA_UseInv)) {
    if(player.ActiveInventory==inventory[6]){
      if (cEgo.InventoryQuantity[14] > 0){
        player.Say("Not while I'm carrying this thing.");
      }
      else {
        player.Walk(280, 130, eBlock);
        player.FaceDirection(eDirectionRight);
        cEgo.LockView(48);
        player.LoseInventory(iplun);
        Wait(10);
        obang.SetView(28);
        obang.Visible=true;
        obang.Animate(0, 3, eOnce, eBlock);
        obang.Visible=false;
        Wait(10);
        player.UnlockView();
        cHip.FaceCharacter(cEgo);
        cHip.Say("What was that?!");
        player.FaceCharacter(cHip);
        player.Say("Nothing!");
        player.Say("Fire just burped, that's all.");
        GiveScore(1);
      }
    }
  }


Note my use of INDENTATION.

Also note that I'm fairly certain that you can again shorten the code you use significantly, by using the proper order of tests.

The very first thing you should check is the dealbreaker, the carrying of inventory #14. Only if that test fails do you check for the actual item that was used. That way you'll again avoid copious amounts of duplicate code.

Code: ags
  else if(UsedAction(eGA_UseInv)) {
    
    if (player.InventoryQuantity[iLog.ID] > 0) {
      player.Say("Not while I'm carrying this thing.");
      return; // exit
    }

    // normal handling
    player.Walk(280, 130, eBlock);
    player.FaceDirection(eDirectionRight);

    if (player.ActiveInventory == iGasoline) {
      // ...
    }
    else if (player.ActiveInventory == iChicken) {
      // ...
    }
    else Unhandled();
  }


Note that I'm using iInventoryName.ID in order to avoid meaningless numbers, and I replaced "cEgo" with "player".

Snarky

This is essentially the same mistake as the last problem you posted about: http://www.adventuregamestudio.co.uk/forums/index.php?topic=54414

You need to learn how to use braces and indentation. If you indent the code correctly (which just means following some very simple rules), errors like this become very easy to spot.

Duckbutcher

Thanks for the advice guys, and yes I'm still learning. Cheers again!

SMF spam blocked by CleanTalk