(Photo) How could I find items separated by stacks within my inventory to craft another item?

Hello! I’ve been racking my brains trying to create a cohesive logic for a crafting system, so I decided to ask the community for help this time.

How could I query my array to remove 3 of these items that are separated into stacks like this:
Crafting

How could I tell my inventory to search for the items (even if they’re spread out across the inventory) and remove the correct amount, which in this case would be 3/3 cloths to craft a bandage, for example.

Any help is a light in the right direction. Thanks!

Perhaps setup a counter variable/map for required items, query the array for the item and addit to that count.

I’d iterate through your inventory with a for each loop with break

(a reverse for each loop with break allows you to remove indexes if you wish, but you have to make a custom macro for that (I have one and can paste code if needed.) If you don’t need to remove indexes, you can use regular for each with break)

For each item in the inventory, determine if the item is the desired one. If it is, increment the “have” variable with the quantity (don’t remove the item(s) yet, because we may not have enough in the inventory, then we would have to put everything back. If additional efficiency is needed, you can put the indexes of the found material in a separate array to iterate through in a moment) If “have” exceeds the “needed” variable, break the loop.

If we have the necessary quantity, we move through the inventory again, removing the items this time (here we can use the gathered indexes if we have them.)

End of day how you do something is dependent on the structure of the data being read and modified. I have a Database Administration (DBA), Software/Web development background. I organize data the way I would for a large relational database.

Arrays, TMaps, Structs are in a simple comparison basically DB Tables.

Key-Value Pair

  • 0: some value
  • 1: some value
  • 2: some value

Or

  • Parent ID (Key), {Element: Value, Element; Value}
  • Parent ID (Key), {Element: Value, Element; Value}
  • Parent ID (Key), {Element: Value, Element; Value}

Data Tables are in essence a database table built from structs (column name and type).

Parent ID (Key) would be Row Name, Requirements a Column.


Inventory systems are mainly organized simple data. Name of thing (Key/ID) and how many. Complex data for “things” such as mesh, icon, stats/specs is typically stored in a data table using a Name type for look up and retrieval. That being said most setups should be based on a Structure of data that’s easily read and updated.

Best practice is to fully scope out the “Things” you can have in an inventory and then group them into types. Craft components, Consumables, Ammo, Healing, Weapons… Doing so will allow you to simplify the data as much as possible. Such as creating a dedicated inventory structure for each Type. Small sets of things are easier and faster to update/read, than large sets.

For crafting items you need data to tell you what crafting components are needed and how many of each. The simplest structure would be… Item Name/ID, Quantity

Bandages:
Cloth, 3
Can Tab, 2 (metal clips to hold bandage wrap in place)

For this you’d use a Struct array.

If you go the Type groupings route your struct would look something like the following.

image

This structure contains an Enum to tell your code which container (Array, TMap, Struct) these inventory items are stored in.

Example

Each Container would hold Inventory Data for the Type.

image


Validating you can craft and then crafting results in something like…

image


Going the Type Groups route “Can” increase the amount of code depending on the containers structure. You’d have to create a dedicated function/macro for each group type that varies. Yet the functions themselves should be lightweight, thus negligent.


Example functions output Item Name and available Quantity for UI presentation. Say you want to create a Bandage but don’t have enough of something. That information should be presented to the client.

Anyway, this is just food for thought. Hope it’s helpful.