Changing the image of an inventory item

Started by Mouth for war, Fri 17/07/2009 14:59:42

Previous topic - Next topic

Mouth for war

Hey! In my little game the player has just recieved 5 goldcoins...the inventoryimage is 5 goldcoins as well. When I go to the store and buy something for 1 goldcoin...can i change the image to 4 goldcoins or do I have to remove the 5goldcoin inventory item completely and replace it like:
cEgo.LoseInventory(i5goldcoins);
cEgo.AddInventory(i4goldcoins) ;  ?

My last option is to stick with the 5 goldcoinimage but have him display "I have 5 goldcoins" or "I have 4 goldcoins" etc. whenever you've purchased something. If you have a good solution please let me know. I looked in the manual for InventoryItem.Graphic but didn't really know what to do anyway...I'm quite new to scripting so...call me stupid if you want to :D
Thanks!
mass genocide is the most exhausting activity one can engage in, next to soccer

GuyAwesome

#1
Having an item for each amount of coins seems like a waste of Items, to me. Why not just have one (iGoldCoin) and use player.InventoryQuantity to check how many they have? You could rename the item (InventoryItem.Name) as well as changing the graphic. To make things easier, I'd make a couple of functions to handle the changes for you
Code: ags

function AddGold(int GoldToAdd) {
  while (GoldToAdd > 0) {
    player.AddInventory(iGoldCoin);
    GoldToAdd --;
  }
  if (player.InventoryQuantity[iGoldCoin.ID] == 1) iGoldCoin.Name = "1 Gold Coin";
  else iGoldCoin.Name = String.Format("%d Gold Coins", player.InventoryQuantity[iGoldCoin.ID]);
  if (player.InventoryQuantity[iGoldCoin.ID] <= 5) iGoldCoin.Graphic = 31 + player.InventoryQuantity[iGoldCoin.ID];
  else iGoldCoin.Graphic = 37;
}

function AddGold(int GoldToLose) {
  while (GoldToLose > 0) {
    player.LoseInventory(iGoldCoin);
    GoldToLose --;
  }
  if (player.InventoryQuantity[iGoldCoin.ID] == 1) iGoldCoin.Name = "1 Gold Coin";
  else iGoldCoin.Name = String.Format("%d Gold Coins", player.InventoryQuantity[iGoldCoin.ID]);

  if (player.InventoryQuantity[iGoldCoin.ID] <= 5) iGoldCoin.Graphic = 31 + player.InventoryQuantity[iGoldCoin.ID];
  else iGoldCoin.Graphic = 37;
}


Then, you only need to use AddGold(5); LoseGold(1);, etc, and everything is handled for you.
NOTE: This assumes you have graphics for 1,2,3,4 and 5 coins in sprites 32-36 (31 + amount of coins held), and a generic graphic for 'more than 5' as 37. I recommend you keep the graphics in consecutive slots like this - it's easier to code like above, than having to jump all over the place - but you can have as many as you want/need. And obviously, it doesn't matter what sprite numbers they are, just change the code as needed.
NOTE 2: Untested. I'm not sure what'll happen if you try to lose more gold than you've got, but it'd be easy enough to add a check to prevent that if it causes problems...

Mouth for war

Thanks...actually you're only supposed to buy 2 things in the store so changing inventoryitem entirely wasn't so hard to do. or timewasting...seems like the easiest choice but many thanks anyway :)
mass genocide is the most exhausting activity one can engage in, next to soccer

GuyAwesome

Oh if it's just for a one-off interaction, then yeah, my way is probably overkill. I thought you were after a more general money system, sorry. Hopefully you can pick something of use out of it anyway...

SMF spam blocked by CleanTalk