Hey there! In my code I’m trying to make a list of like items. I have a list that every time an item is collected it adds it to the list. That works fine, but I want to group like items together and count how many there are.
public class InventoryManager : MonoBehaviour
{
public List<string> whichItem;
string itemList;
public string[] whichItems;
public List<int> groupsOfObjects;
public int howMany;
public int itemInt;
IEnumerable<String> methodNames = typeof(String).GetMethods().Select(x => x.Name);
IList<String> names = new List<string>() { "ToString", "Format" };
public List<int> possibilities;
// Start is called before the first frame update
void Start()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Item")
{
itemList = collision.GetComponent<Item>().itemList;
itemInt = collision.GetComponent<Item>().itemInt;
int count = whichItem.Where(x => itemList.Contains(x)).Count();
howMany = count;
if(possibilities.Contains(count) == false )
{
possibilities.Add(count);
}
whichItem.Add(itemList);
Debug.Log(whichItem.Contains(itemList));
}
}
// Update is called once per frame
void Update()
{
Debug.Log(whichItem.Count);
Debug.Log($"grouping {groupsOfObjects.Count}");
}
}
The result I’m hoping for would print out in the inspector would be for example:
Items in Inventory:
stick
rock
stick
brick
Number of each in inventory:
2 sticks
1 rock
1 brick.
PS: I have another script called Item that simply has:
public string itemList;
public int itemInt;
Thanks for your time! Help is very much appreciated and I’ll be sure to mention you on my YouTube Descon Games.
This is commonly called “stacking” so search for tutorials on that. This is all very well-traveled ground in the tutorial space. Stacking inventory stuff is extremely detailed work, starting with just the basic visual UI/UX design of what a “stacked” item looks like and how you can interact with it, etc.
Most of what is necessary for an inventory is not code so it’s poorly suited to posting here.
Here’s some more generic inventory reading.
These things (inventory, shop systems, character customization, dialog tree systems, crafting, ability unlock systems, tech trees, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.
The following applies to ALL types of code listed above, but for simplicity I will call it “inventory.”
Inventory code never lives “all by itself.” All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.
If you contemplate late-delivery of content (product expansion packs, DLC, etc.), all of that has to be folded into the data source architecture from the beginning.
Inventories / shop systems / character selectors all contain elements of:
a database of items that you may possibly possess / equip
a database of the items that you actually possess / equip currently
perhaps another database of your “storage” area at home base?
persistence of this information to storage between game runs
presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
interaction with items in the inventory or on the character or in the home base storage area
interaction with the world to get items in and out
dependence on asset definition (images, etc.) for presentation
→ what it looks like lying around in the world? In a chest? On a shelf?
→ what it looks like in the inventory window itself?
→ what it looks like when worn by the player? Does it affect vision (binoculars, etc.)
→ what it looks like when used, destroyed, consumed?
Just the design choices of such a system can have a lot of complicating confounding issues, such as:
can you have multiple items? Is there a limit?
if there is an item limit, what is it? Total count? Weight? Size? Something else?
are those items shown individually or do they stack?
are coins / gems stacked but other stuff isn’t stacked?
do items have detailed data shown (durability, rarity, damage, etc.)?
can users combine items to make new items? How? Limits? Results? Messages of success/failure?
can users substantially modify items with other things like spells, gems, sockets, etc.?
does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
etc.
Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.
Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.
Breaking down a large problem such as inventory:
If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).
Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.
Breaking down large problems in general:
The moment you put an inventory system into place is also a fantastic time to consider your data lifetime and persistence. Create a load/save game and put the inventory data store into that load/save data area and begin loading/saving the game state every time you run / stop the game. Doing this early in the development cycle will make things much easier later on.
Various possible inventory data structures in Unity3D:
@Kurt-Dekker Howdy! Thanks for responding! I see what you’re saying.(so I decided not to make my own inventory system when I could go to the asset store and get a free one) This topic I believe does belong in scripting because I’m not trying to display inventory just count it. I believe that I could get it working in the aforementioned script. Because that tracks which items I currently have. So what would be easiest would be to simply check for like items in the list and count those and make an integer for that stack. I believe it’s possible and it’s mostly just a coding project and looking at the inspector to see the variables. Thanks for the quick response! I found a really good asset pack but for future projects I want to make my own.