Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Jojowl80 on Sun 20/09/2020 01:04:39

Title: I only want to get one item from a location
Post by: Jojowl80 on Sun 20/09/2020 01:04:39
Code (ags) Select



function hTopDrawer_AnyClick()
{
  if (Verbs.UsedAction(eGA_WalkTo)) {

}else if(Verbs.UsedAction(eGA_Pull)) {
   player.Say("Try Opening it.");
   
     
}else if(Verbs.UsedAction(eGA_Push)) {
    player.Say("I don't need to Push that");

}else if(Verbs.UsedAction(eGA_Open)) {

    player.Say("Okay.");
    sDrawer.Play();
    player.Say("There was a Fork in the Drawer.");
    player.AddInventory(iFork);
    Fork1 = true;
   
}if (Fork1 == true){
    player.Say("Nothing Else.");
   
}else if(Verbs.UsedAction(eGA_Close)) {
    player.Say("Already closed.");
}
}



If I open the drawer again it just gives me the item again. However if I right click it says "nothing else"
Title: Re: I only want to get one item from a location
Post by: Mandle on Sun 20/09/2020 01:43:20
Try something like this:

else if(Verbs.UsedAction(eGA_Open)) {
   
   if (Fork1 == true){
    player.Say("Nothing Else.");
    return;
    }

    player.Say("Okay.");
    sDrawer.Play();
    player.Say("There was a Fork in the Drawer.");
    player.AddInventory(iFork);
    Fork1 = true;
Title: Re: I only want to get one item from a location
Post by: Jojowl80 on Sun 20/09/2020 02:17:51
worked perfectly thanks!. didn't know about *return*
Title: Re: I only want to get one item from a location
Post by: Cassiebsg on Sun 20/09/2020 16:35:02
just for future reference, you could also use a Game.DoOnceOnly. That would save you for creating a variable just for the effect, or you could check if the player has the item in inventory (though if he lost it when using it and you didn't want him to get a new fork, then the first option it best.  ;)
Title: Re: I only want to get one item from a location
Post by: Mandle on Mon 21/09/2020 05:30:32
Quote from: Jojowl80 on Sun 20/09/2020 02:17:51
worked perfectly thanks!. didn't know about *return*


No worries. "Return" is a pretty important tool so I'm glad you have it now. It will save you many headaches in coding!
Title: Re: I only want to get one item from a location
Post by: Khris on Mon 21/09/2020 08:15:24
You were pretty close, but you had the Fork check in the Verbs check level as opposed to inside the eGA_Open block.
I'm going to go out on a limb and suggest that using proper indentation will help immensely with understanding the involved logic here:

Code (ags) Select
function hTopDrawer_AnyClick() {

  if (Verbs.UsedAction(eGA_WalkTo)) {
  }
  else if (Verbs.UsedAction(eGA_Pull)) {
    player.Say("Try Opening it.");
  }
  else if (Verbs.UsedAction(eGA_Push)) {
    player.Say("I don't need to Push that");
  }
  else if (Verbs.UsedAction(eGA_Open)) {

    // all this is inside the eGA_Open block, as indicated by the curly braces AND INDENTATION
    if (Fork1 == true) {
      player.Say("Nothing Else.");
    } else {
      player.Say("Okay.");
      sDrawer.Play();
      player.Say("There was a Fork in the Drawer.");
      player.AddInventory(iFork);
      Fork1 = true;
    }

  }
  else if (Verbs.UsedAction(eGA_Close)) {
    player.Say("Already closed.");
  }
  else Verbs.Unhandled(); // deal with remaining verbs!
}


See how the visual aspect clarifies instantly what's happening here?