Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: newwaveburritos on Sat 17/07/2021 03:58:26

Title: Grouping inventory items together [SOLVED]
Post by: newwaveburritos on Sat 17/07/2021 03:58:26
I have a sort of crafting/cooking puzzle in the game where the player gets into the refrigerator to pull ingredients to cook different recipes.  I don't want the player leaving the room with these ingredients for the sake of easing inventory management.  I have it working well enough with a dummy character being the inventory.  I think maybe I should have tried using Khris' crafting module instead of the weird way I started doing it but I think there's a useful lesson here for me and maybe somebody else, as well.  I want to be able to strip the player's inventory of all of the cooking inventory items easily when they leave but I don't know what those are going to be and while I could just strip the player's inventory of every possible item this feels needlessly messy.  I read about structs and arrays but these aren't variables I'm tracking so those don't really seem like a natural fit to me, either.  Well, that and I don't know how to use those, either.

I think there may be a way to set the particular inventory item's custom property to something like "Cooking" and then call that to pull out all the items with that property being true but I don't really see how to do that, either.  Thanks again for all your help!

EDIT: I guess all the inventory items are already in an array; that's what that inventory[1] and so forth refers to.  If I group all of the "cooking" inventory items together on the inventory list then I can keep them organized but I may end up adding some items later so I'd still want a way to manage this.  The only way I can think of still seems a little off:

Code (ags) Select
player.LoseInventory(inventory[14&&15&&16&&17&&18]);
Title: Re: Grouping inventory items together
Post by: Cassiebsg on Sat 17/07/2021 09:43:01
If your inventory items are sequential, you can just do a while loop for those inventory ID.

Or like you said, use a custom property for inventory items, set them to "Cooking" (or some other name), and then use again a while loop to check all the inventory items the player has at the "leave room function". Check if the property is equal to "Cooking" (custom properties names do not show up on auto-complete, so avoid a complicated name). Check InventoryItem.GetTextProperty in the manual.  :)
Title: Re: Grouping inventory items together
Post by: newwaveburritos on Sat 17/07/2021 16:39:55
I don't completely understand how I can get the item out of the inventory with a custom property without already having identified the item.  What I mean is that I want all of the items with that property out but I can't figure out how to call every inventory item like inventory[all of them] or something like this:

Code (ags) Select

if (player.HasInventory(inventory["Cooking"]){
  player.LoseInventory(inventory["Cooking"]);
}
Title: Re: Grouping inventory items together
Post by: Matti on Sat 17/07/2021 17:42:45
You can cycle through all items and check each item's property.

Untested code:
Code (ags) Select

for (int i = 0; i < invWindow.ItemCount; i++) {
  if (inventory[i].GetTextProperty("Cooking")
  {
    // do stuff, e.g. lose the item
  }
}
Title: Re: Grouping inventory items together
Post by: newwaveburritos on Sat 17/07/2021 19:01:18
I think I understand the concept here since I ended up using some for loops earlier in the game but I can't make this one work.  I did capitalize "InvWindow" It tells me:

GlobalScript.asc(2035): Error (line 2035): must have an instance of the struct to access a non-static member
Title: Re: Grouping inventory items together
Post by: Cassiebsg on Sat 17/07/2021 19:54:16
Try using the name of your inventory window instead.
Title: Re: Grouping inventory items together
Post by: newwaveburritos on Sun 18/07/2021 03:32:28
Quote from: Cassiebsg on Sat 17/07/2021 19:54:16
Try using the name of your inventory window instead.

Okay, that was definitely the problem and this is working but I still can't make it lose the item with the property.  This will make the player lose items 1-9. I know I'm missing something obvious here but I sure can't see it.


Code (ags) Select

for (int i = 0; i < invCustom.ItemCount; i++) {
  if (inventory[i].GetProperty("Cooking")==true){
    player.LoseInventory(inventory[i]);
  }
}


EDIT:

Okay, one obvious thing I was missing was that I had the property set to true for every inventory item.  When I switch it to the appropriate items: inventory[14] through inventory[18] it gets rid of inventory[14] and inventory[15] which baffles me even further.
Title: Re: Grouping inventory items together
Post by: Matti on Sun 18/07/2021 19:05:02
So the player loses items 14 and 15, but not items 16-18? That sounds strange. Right now I don't have the time to look into this/test it myself, but maybe tomorrow.
Title: Re: Grouping inventory items together
Post by: Cassiebsg on Sun 18/07/2021 20:27:33
Here's a related topic (except  in this case the point is to empty every item, but the way to check on the for loop, is what matters.

https://www.adventuregamestudio.co.uk/forums/index.php?topic=56858.msg636600942#msg636600942

Try it out and see if that helps.
Title: Re: Grouping inventory items together
Post by: Matti on Mon 19/07/2021 01:11:36
Does it work if you use Game.InventoryItemCount instead of invCustom.ItemCount?
Title: Re: Grouping inventory items together
Post by: newwaveburritos on Mon 19/07/2021 03:10:56
Quote from: Matti on Mon 19/07/2021 01:11:36
Does it work if you use Game.InventoryItemCount instead of invCustom.ItemCount?

This code gets rid of everything but the last item marked "Cooking."

Code (ags) Select

for (int i = 0; i < Game.InventoryItemCount; i++) {
  if (inventory[i].GetProperty("Cooking")==true){
    player.LoseInventory(inventory[i]);
  }
}


The post linked by Cassiebsg though provided a clue though that if I change it to:

Code (ags) Select

for (int i = 0; i <= Game.InventoryItemCount; i++) {
  if (inventory[i].GetProperty("Cooking")==true){
    player.LoseInventory(inventory[i]);
  }
}


it does what I want it to and purges all the correct inventory items.  I added an item after the last one just to be sure and it stays in the inventory so I think this is working as intended.  Thanks for all your help, everybody!
Title: Re: Grouping inventory items together [SOLVED]
Post by: Khris on Mon 19/07/2021 08:33:15
Inventory items are 1-based, so you need

Code (ags) Select
  for (int i = 1; i <= Game.InventoryItemCount; i++) {
    if (inventory[i].GetProperty("Cooking")) player.LoseInventory(inventory[i]);
  }