How To Script An Inventory System

I have been having a heck of a time with this. I tried to get help from ChatGBT, but the code is always very complex and has bugs that seem incurable.

Is there a tutorial on Unity Learn for putting together an Inventory in the simplest way possible?

I’m getting frustrated enough to use ChatGBT and then the code is so complex, I can barely read it, and it’s not correct.

For example, I managed to get something to work where I had some sprites show up in a toolbelt, and I was trying to store the items in a List<>, but it was destroying the item, and then storing it in the List<>, so the items were “Missing (Item)”

Any and all recommendations would be greatly appreciated. I want to do an inventory system like the one on 7 Days To Die.

Problem is inventories aren’t simple; they’re inherently complex. And they require a very solid programming understanding, much more than an understanding of Unity.

Chat GPT isn’t going to be much use. The amount of moving parts required is too much for its tiny memory. An inventory like the one you’re describing is still likely made of dozens if not hundreds of moving parts.

If you’re struggling with coding an inventory system, let along understanding the code, you need to take a step back and work on your programming understanding, alongside working on simpler projects to reinforce this.

Once you have a better understanding of C#, an inventory system will come naturally.

2 Likes

I appreciate your response, but I do understand C#. I’m having trouble finding an example of an efficient way to script an inventory. All the nonsense the AI gave me is way over complicating it.

I like to see examples. Do you know of a tutorial video that is a good example of building an inventory.

Also, the 7 Days To Die inventory is only stacking items in slots with dragging and dropping. I don’t want the splitting stacks feature. I don’t know for sure, but I doubt that has hundreds of moving parts.

Probably not actually. Spiney knows what he’s talking about.

Here is my blurb about it:

These things (inventory, shop systems, character customization, dialog tree systems, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

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.

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

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:

https://discussions.unity.com/t/826141/4

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:

https://discussions.unity.com/t/908126/3

1 Like

Organisation is more important than ‘efficiency’ (I’m not sure what that actually means here) when it comes to inventory systems. Encapsulation, SRP, SOLID code in general is what brings together an inventory.

And I can say for sure it in all likelihood does have hundreds of moving parts. Drag and drop alone is tons of complexity on the UI end, on top of all the invisible back-end. Even games like Subnautica that have a fairly simple inventory have a lot going on, speaking from poking about its code (ugly as its codebase is).

You could look for yourself by rooting around the code in its .dlls.

I feel like you’re asking for an easy solution for something where there is none.

I’m mean this in the nicest possible but if you’re asking these questions, then you might want to revise this stance.

Youtube vids only get you so far with this kind of stuff, as they’re always very distilled down solutions to larger problems. To properly cover an inventory system would require a whole youtube series, and I don’t know of any.

Outline your requirements first. Write them down so you understand what you want your system to do. Then start a project in isolation and work with the smallest scope that you think you can manage.

And rely less on chat GPT and follow your own intuition.

3 Likes

I think you need to find something better to do than not answer questions and tell people they can’t do it. You haven’t offered any help whatsoever. You’re just being another no name online telling me I can’t do it and I need to revise this and I need to do that.

Bottom line: You have no idea how to help me and you have no recommendations. Stop putting that on me and move on before I report you.

Thank you very much for your help and recommendations.

You can’t do it until you increase your programming skills, so you should work on that. Improve yourself to the point where you understand ChatGPT code. Threatening people that are trying to help you is not a good idea either, plus it is plain rude.

You can’t find am example, because it is a thing slightly above basic level, meaning usually step-by-step instructions are not provided. For example, knapsack problem is a known training exercise practiced by programmers, dealing with it will give you ideas for inventory, plus it is an a case of MVC, which is a fairly common GUI paradigm.

https://en.wikipedia.org/wiki/Model–view–controller

An inventory where each item takes a slot and not several could be implemented as an 1d or 2d array or any class that acts like one, for example, a Dictionary that maps an index to an item can be used as inventory. The point here that items you store in inventory are not game objects, and seeing you get “Missing” there you’re trying to stuff game objects there which is wrong. This thing is closely related with idea of Model/View and Model/View/Controller architecture, and as such an object stored in inventory is not an in-world item, but a record that refers to item information, for example, stored as a ScriptableObject, which says what item is called, what prefab is used for in-world representation, and what prefab is used to display the item on inventory screen. The inventory screen acts as a view that reflects internal state of inventory storage, and when you interact with it, actions are reflected in the inventory storage. So inventory screen acts as a view that spawns prefabs acting as item icons within UI, and removes them when item is gone, and when you throw an object out of inventory, it spawns an in-game prefab. If you have an “inventory” then you probably have “containers”, and they’ll have their own inventories, and your UI shoudl work with any of them, which is what MVC or MV architecture is for. It is a typical GUI paradigm which is often used in frameworks such as Qt to implement list views, tree views, grids and so on.

3 Likes

Dude, you aren’t entitled to get free advice on the forums. If you don’t like what you get you can always find someone to teach you for money. There are plenty of courses out there. And then they are have to put up with your sense of entitlement, they are getting paid for it.

With that said, I googled for you.

https://www.youtube.com/watch?v=dAqOiecPCg8

I also quickly scrubbed it through for you, it seems sensible at first glance, but I won’t spend an hour and a half watching entry level video tutorials for you, so if something doesn’t work, come back here, paste your code, tell us where you stuck and we may be able to help with that.

6 Likes

Here’s a written recent tutorial where the first line says: surprisingly difficult in Unity… which is a fair warning:

1 Like