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!
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).
Thank's a lot. It worked perfectly well.
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.
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.
Good points Khris, what a terrible answer that was!