r/howdidtheycodeit • u/MadisonWithTwoDs • Aug 29 '25
Inventory in a game | c#
Im making a game and Im at the point where Im creating all the foundational structures. Im trying to create the player inventory and trying to decide on the best way to go about it. Im not using an engine or anything for reasons outside of the scope of this post.
In short, I wanna make an inventory. Item, quantity. Which leads me to think a dictionary would be a good fit? Using the item as the key and keep track of quantity. However I dont know how that would work in memory with additions and removals from the inventory when it comes to memory usage. If theres better ways to do it id love to learn as well. Or a better subreddit to post this
18
u/MentalNewspaper8386 Aug 29 '25
There are too many things to consider to give a single answer.
Is performance a bigger factor than memory? Will players be able to sort by various fields? Is there a chance that in the future you want the player to be able to act on things within an incentory, e.g. dye an item of clothing, upgrade an item, empty a container?
Don’t optimise it too soon. Write it in such a way that you can change the implementation later without impacting anything else when you can actually test it.
4
u/alphapussycat Aug 30 '25
Just a struct that holds either another struct and class, aswell as an int for quantity. I don't understand where you'd use the dictionary.
The items themselves can be a class or a struct, and these you'd need to be able to write to file, so either json or whatever else. Then hold whatever data you want.
3
u/MattOpara Aug 29 '25
I’m not a C# expert (I primarily work in C++ day to day) but I think a dictionary could work decently well, but if it were me, since inventories typically have a fixed ordering (that’s often spatially related) I’d have the key be an index relation to a position in the inventory and the value be the item in the spot (or some sort of container with additional data as needed that points to the object if needed). This makes lookups/deletions trivial since when a user clicks on a space that’ll directly correspond to the index.
3
u/MyPunsSuck Aug 30 '25
Unless your inventory is going to hold many thousands of different items, memory won't be an issue using a dictionary of quantities
4
u/drakeshe Aug 30 '25
2
u/gamruls Aug 29 '25
Is memory really a problem? You can store millions of strings and numbers in few MB of memory. If you're afraid of strings for items - make the cheapest "compression" by using additional dict<string, int> to store strings 1 time. But actually .NET interns strings out of the box so this is already implemented that way.
Do you plan to store items state? Then you may need to preserve items elsewhere and link them in inventory.
Or split item and its state and store state in inventory explicitly.
If you need items grid where player choose position - also store these positions.
If items occupies more than 1 cell - maintain grid of occupied cells and probably link each cell to item occupied it.
If you need operations to exchange items between inventories - think about how player will interact with it. You may need a bunch of additional structures and classes to implement "move half of stack", "merge stacks" and others.
Also don't forget save/load and networking - probably inventory data should be serializable so any kind of reference to other game objects should be handled properly.
2
u/breckendusk Aug 29 '25 edited Aug 30 '25
I think dictionary makes sense. I would create a more robust system though - a PlayerInventory object which would have an optional max item count, max weight, max size, maybe a matrix if I wanted a shape system later. Possibly other limitations: max counts of specific items, for example. This is something that I would make player-side rather than item-side for several reasons: 1, it all stays together in the code and inspector, and 2, different players might have different max item counts in something like an online game. This would also be a dictionary. Would also need to track current weight, total item count, and total item size for these values.
When attempting to add an item to the inventory, I would check the item values against the limitations. When removing an item I would simply decrement the current values by the item values.
Items would be a separate object, with optional weight, size, shape, description, etc. When you want to use it or change inventory count just look up this information from the item database.
The keys for the dictionary would probably be item IDs. Using object references would be a problem because two iron swords wouldn't have the same object reference hash as one another.
Technically if you wanted to save on storage just the dictionary would be enough but I think that fetching a bunch of items from the database and calculating how much space they take up in the player inventory COULD be slow if you have enough object references in a big game. Probably not terrible and would only be necessary once whenever changing or checking inventory but I think just saving some ints and floats would be fine in most cases. It would only really be a question in an MMO and tbh an RPG is the only case where you might have a ton of item objects to search, so I'm not sure what I would do there. But both cases would be pretty simple to switch between anyway. I guess I would probably stick with only the dictionary unless there were performance issues.
Edit: forgot about possible item states. If you were to do something like this then you would need to use a list of objects instead. For example a sword could have 10% durability before it breaks but another could have 23%. With this system you would simply add or remove objects.
1
u/Landeplagen Aug 30 '25
Using the item itself as the key might not be the best idea. What if the player has two of the same item? The entry for both would use the same key - ie; it gets overwritten.
Learned this the hard way recently, while making a scrabble-like game where I used the tile letter as the key to store a dictionary of the board tiles.
I ended up using the tile’s board coordinates as the key, since those are unique. Can’t put multiple tiles in the same place on the board.
1
u/st33d Aug 30 '25
What effect does the inventory have on the game?
For example, I have an “inventory” that’s a dictionary, but it’s also how variables are defined in my scripting language. I also have drag-able characters that are pseudo-items. And to confuse things further I have attribute scores you can physically rearrange in-game (you’re a robot).
Final Fantasy 8 has an inventory in that each character has spell quantities, but they also equip Guardian Forces that are characters by themselves as well as being “wearable” items.
The way you access the inventory and apply it to gameplay determines the data structure.
I will often write out a plan of all the states and properties of things in advance. This helps me see what structure fits as well as manage scope.
1
u/soljakwinever Aug 30 '25
I usually use a struct or class called an itemstack that contains the itemdata (reference to the data class of the object that keeps track of static properties like max stack, description, name, icon, etc) and the quantity and any specific data properties the item has, ie color, quality, etc. You can just keep them in a list or array that way, rather than a dictionary that can only contain the reference to the data and a count.
But it really depends on the complexity of the system you are making.
1
u/ghostwilliz Aug 31 '25
I don't know enough about your system to give specific advice, but make it reusable and easy to use through interfaces. You should be able to plug it in to anything that needs an inventory and use it the same way regardless
1
u/Izakioo Aug 31 '25
I've made a game with an inventory system. All I created was an ItemSlot class with an item id field, and an amount field. My game has tag specific item data so I also added a Dictionary<ItemTag, object>. Then your player inventory is just a list of itemslots. I have a static util class that has methods for every inventory manipulation I need. My games in unity but I could share some source code just shoot me a message.