Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Gribbler on Fri 08/06/2007 12:38:19

Title: inventory problem
Post by: Gribbler on Fri 08/06/2007 12:38:19
I'm totally fresh with scripting here so don't laugh me off. I want and inventory item (1) to contain another item(2) so when a player looks item1 (in any room in the game) he will say that he found something in it and receive item2. When he will look again he will say that there's nothing more in it. Here's my code:

// script for Inventory item 5 (shirt): Look at inventory item
  if (key_in_shirt == 0) {
player.Say("Yuuck I hate green color.");
player.Say("Hey there's something in a pocket.");
player.AddInventory(iKEY);
player.Say("It's a key!");

}
if (key_in_shirt == 1) {
  player.Say("There's nothing more in a pocket.");
}


I also put this in Global Script:

int key_in_shirt;
export key_in_shirt;


With the above code Player gets intem2 every time he looks at item1. What should I change to make him say that there's nothing more and not to receive item2 any more.
Big Thanks in advance to any response!
Title: Re: inventory problem
Post by: Ashen on Fri 08/06/2007 12:46:54
You need to change the variable to make the second condition run:


// script for Inventory item 5 (shirt): Look at inventory item
if (key_in_shirt == 0) {
  player.Say("Yuuck I hate green color.");
  player.Say("Hey there's something in a pocket.");
  player.AddInventory(iKEY);
  player.Say("It's a key!");
  key_in_shirt = 1; // <--
}
else if (key_in_shirt == 1) { // 'else if' stops this from running after the first condition
  player.Say("There's nothing more in a pocket.");
}


If key_in_shirt is only going to be used in the Global Script, you don't need the export line (if you get rid of it, don't forget to delete the import line, too).
Title: Re: inventory problem
Post by: Gribbler on Fri 08/06/2007 13:02:06
Thank's a lot. It worked perfectly well.
Title: Re: inventory problem
Post by: CodeJunkie on Fri 08/06/2007 13:46:06
You should be able to lose the key_in_shirt variable altogether since AGS has it's own variables for inventory items.  This should work too:


// script for Inventory item 5 (shirt): Look at inventory item
if (player.InventoryQuantity[1] == 1) {
  player.Say("Yuuck I hate green color.");
  player.Say("Hey there's something in a pocket.");
  player.AddInventory(iKEY);
  player.Say("It's a key!");
}
else player.Say("There's nothing more in a pocket.");
}


Where in InventoryQuanity[1] the 1 means the inventory item number.  You can also use the iKEY instead of a number if you like.
Title: Re: inventory problem
Post by: Khris on Fri 08/06/2007 15:03:46
1. It's "iKey".

2. It's "== 0".

3. That solution is dirty and error-prone. As soon as the key is gone (the player looses it, etc.), it will reappear in the shirt.
Don't do that even if you are absolutely sure that the player will keep the key until the very end.
Doing it using a variable is the only safe way.
Title: Re: inventory problem
Post by: CodeJunkie on Fri 08/06/2007 21:22:51
Good points Khris, what a terrible answer that was!