Using Multiple items on a hotspot

Started by Jojowl80, Wed 17/06/2020 03:48:51

Previous topic - Next topic

Jojowl80

Hey so I have two Items I want the player to have before he can make coffee

Code: ags


function hCoffeMaker_UseInv()
{
  if(player.HasInventory(iCoffee) && player.HasInventory(iCcup))
  player.Say("Giddyup!");
  sBrew.Play();
  Wait(150);
  player.Say("Oh boy, Coffee's ready!");
  sPour.Play();
  Wait(150);
  player.LoseInventory (iCcup);
  player.AddInventory(iCcupfull);
}  


problem is if I use either item singularly it also makes the coffee. Want to make it so he has to have both items before he makes it

Gilbert

That's because you did not enclose the codes after if(...) in braces {}, so that only the "Giddyup!" line was affected by the if condition, and the remaining lines would be executed regardless of the conditions.

So:
Code: ags


function hCoffeMaker_UseInv()
{
  if(player.HasInventory(iCoffee) && player.HasInventory(iCcup))
  {
    player.Say("Giddyup!");
    sBrew.Play();
    Wait(150);
    player.Say("Oh boy, Coffee's ready!");
    sPour.Play();
    Wait(150);
    player.LoseInventory (iCcup);
    player.AddInventory(iCcupfull);
  }
}  


Matti

Also, right now the player could use any item to make the coffee. If you want them to only make coffee when using either the cup or the coffee on the coffeemaker, then add an extra condition:

Code: ags

function hCoffeMaker_UseInv()
{
  if (player.ActiveInventory == iCoffee || player.ActiveInventory == ICcup)  // <---
  {
    if(player.HasInventory(iCoffee) && player.HasInventory(iCcup))
    {
      player.Say("Giddyup!");
      sBrew.Play();
      Wait(150);
      player.Say("Oh boy, Coffee's ready!");
      sPour.Play();
      Wait(150);
      player.LoseInventory (iCcup);
      player.AddInventory(iCcupfull);
    }
  }
}


SMF spam blocked by CleanTalk