Grouping inventory items together [SOLVED]

Started by newwaveburritos, Sat 17/07/2021 03:58:26

Previous topic - Next topic

newwaveburritos

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
player.LoseInventory(inventory[14&&15&&16&&17&&18]);

Cassiebsg

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.  :)
There are those who believe that life here began out there...

newwaveburritos

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

if (player.HasInventory(inventory["Cooking"]){
  player.LoseInventory(inventory["Cooking"]);
}

Matti

#3
You can cycle through all items and check each item's property.

Untested code:
Code: ags

for (int i = 0; i < invWindow.ItemCount; i++) {
  if (inventory[i].GetTextProperty("Cooking")
  {
    // do stuff, e.g. lose the item
  }
}

newwaveburritos

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

Cassiebsg

Try using the name of your inventory window instead.
There are those who believe that life here began out there...

newwaveburritos

#6
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

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.

Matti

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.

Cassiebsg

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.
There are those who believe that life here began out there...

Matti

Does it work if you use Game.InventoryItemCount instead of invCustom.ItemCount?

newwaveburritos

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

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

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!

Khris

Inventory items are 1-based, so you need

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

SMF spam blocked by CleanTalk