How to make an Inventory System with Unity

Поделиться
HTML-код
  • Опубликовано: 15 янв 2025

Комментарии • 64

  • @choppedandspewed
    @choppedandspewed 3 года назад +2

    My man just STARTED with writing tests. Never subbed so fast in my life.

  • @JackieCodes
    @JackieCodes 3 года назад +6

    Thank you thank you thank you! This code is golden, I learned so much using it! I had already built my own inventory system but after watching this video I had to switch over because your code practices are so admirable. I will definitely give you credit in my next devlog! Thank you again.

  • @fka8159
    @fka8159 2 года назад +2

    Thanks for this! I tried to find these exact solutions by myself and came pretty far until It kinda stalled - I went online to look for inspiration and was stunned and encouraged that the architecture I came up with so far mirrored your approach pretty closely. Your code will serve as a great reference to fix and enhance my own code.

    • @fluffygamedev
      @fluffygamedev  2 года назад

      I'm glad this tutorial was helpful ! :)
      If there's anything you think should be done differently, I'd be curious to know. 😁

  • @iii-tf6rc
    @iii-tf6rc 3 года назад +5

    You definitely deserve more subs

    • @fluffygamedev
      @fluffygamedev  3 года назад +1

      Thank you for saying that! :)
      Currently, the sub count is fairly low since this channel is still new.
      Currently, my goal is to improve the quality of my videos over time.
      I'll be glad to hear of any improvement I can make.
      And if there are some subjects you'd like me to cover, I'll be more than happy to do so. ;)

    • @jerongunn4416
      @jerongunn4416 3 года назад +1

      @@fluffygamedev Subscribed ^^ Ty for the video.

  • @zezooalzezoo5200
    @zezooalzezoo5200 3 года назад +5

    We need more , keep it up please

  • @gone3281
    @gone3281 3 года назад +5

    i find tons of tutorials for mouse... need more for controller

    • @fluffygamedev
      @fluffygamedev  3 года назад +2

      Good point! Definitely a subject I can keep for a future video! 😁

  • @Steeksify
    @Steeksify 2 года назад +1

    How about a Resident Evil or Diablo style inventory, where items can take up more than 1 slot?

    • @fluffygamedev
      @fluffygamedev  2 года назад +1

      Good question !
      The way I'd implement that kind of inventory is a bit counter intuitive. The idea is that that kind of inventory differs from others only by its UI. The back end (the Inventory System) does not care about data like the position of the items in the inventory. That's some UI specific data.
      As such, the way I'd implement this type of inventory, the slots would not correspond to the individual squares in the inventory grid but rather to the various item stacks within the grid. As such, The number of slots could change as the game progresses. For example, and empty inventory would have 0 slots. But an inventory with 7 different item stacks would contain 7 slots.
      In practice, to implement this kind of inventory, I'd have some UI-specific class to map item stacks/slots to their position in the grid. The dimensions of those stacks could be in a DataComponent in the Inventory Item GameObject (see part 2 to find out more about those).
      I hope this helps! :)
      If you want we can discuss all this further on Discord. ;)

    • @Steeksify
      @Steeksify 2 года назад

      @@fluffygamedev I sort of get what you're saying, but I am a total noob so a lot of it went over my head. I'd feel really guilty bothering you about it more than this, since I'd need explanations for basically everything and that's a bit too much work to ask of someone lol It at least gets me sort of in a direction to start looking.

  • @OliverJacked
    @OliverJacked 3 года назад +6

    Hey Fluffy, I'm using your inventory system and really enjoying it. Although, I am trying to figure out how to return a list of the items that the player has in their inventory, from within the player script and reference some of the data within those items. There's a few different ways I could go about it, but was wondering what you would suggest!

    • @fluffygamedev
      @fluffygamedev  3 года назад +2

      Sorry for the late reply. ^^'
      If that can help, we can discuss this by DM on Twitter or Discord.
      Anyway, first and foremost, what kind of data do you need in the inventory items? Should this be done on update? Or are there some events after which it can be handled (ex: when a slot is updated, when an item is looted)?
      In all cases, you can access the inventory by getting the InventoryHolder from the player script. Then, with the inventory, you can call Inventory.FindAll or Inventory.ForEach to manipulate all the slots you need.
      Alternatively, you can register a callback to some or all the slots OnItemChange delegate. That way, you can do your stuff only when the callbacks are called.

    • @OliverJacked
      @OliverJacked 3 года назад +1

      @@fluffygamedev Hey, no problem. Figured out what to do shortly after posting the question. I would like to add you on discord though.

  • @summorandogamor6826
    @summorandogamor6826 2 года назад

    I was wondering how we add items to our inventory? I have the Scriptable Object I just don't know how to add them.

  • @smokinglife8980
    @smokinglife8980 3 года назад +2

    How would we go about equipping weapons,items,etc from the inventory

    • @fluffygamedev
      @fluffygamedev  3 года назад +1

      The way I'd do it with this system it to have some special Inventory Slots that are handled outside of the inventory.
      We could have one slot for each element that can equip an item (L-Hand weapon, R-hand weapon, helmet, ...). And whenever we detect a change on one of those slots, we update the player model and possibly even some components on the player (ex: for stats).
      If you want, I can make a part 2 to this video to explain equipping a bit more.

    • @smokinglife8980
      @smokinglife8980 3 года назад

      @@fluffygamedev that part 2 would be helpful but I am going to try and figure it out on my own too just for learning experience

    • @sl1nger
      @sl1nger 3 года назад +1

      @@fluffygamedev Go for it ;-) you got my sub :p

    • @perssontm1628
      @perssontm1628 3 года назад +1

      @@fluffygamedev Yes, please make a part two, that would be awesome!

  • @branidev
    @branidev 3 года назад +3

    Let me say two things : 1.) Let's be friends! 2.) I struggle weeks to get my Spell book and drag & drop spells to the action bar, so far i have done all the scripts for Ability System and through (SO) i can easy Add a new spell, then i make Drag & Drop so i can drag UI icons from Spell Book to the Action bar... but then I struggle how to pass this data and I hope this video is gonna help me achieve what i need! Great Content man!

    • @fluffygamedev
      @fluffygamedev  3 года назад

      Heya!
      I hope so too that this video will prove to be useful to you! :)
      If you have some specific questions, feel free to ask.
      And if need be, you can also DM me for some questions regarding the code of your own project. ;)

    • @branidev
      @branidev 3 года назад

      @@fluffygamedev I would like to DM you but can't send you message on Twitter and RUclips doesn't have DM option.

    • @fluffygamedev
      @fluffygamedev  3 года назад

      @@branidev I just realized it was not activated on my Twitter account. ^^'
      It should be all good now ;)

  • @zezooalzezoo5200
    @zezooalzezoo5200 3 года назад +1

    Thanks a lot very useful

  • @jarekshark1356
    @jarekshark1356 3 года назад +1

    So how to use 2 or more inventories at the same time? with drag and drop ofc.

    • @fluffygamedev
      @fluffygamedev  3 года назад

      To use 2 inventories, we'd need to create a new UI element to handle multiple inventories (we can most likely reuse the InventoryUIController here). This UI element could be shown by triggering an event specifying both inventories. One could come from the InventoryHolder on the Main Character and the other from an InventoryHolder on another object like a chest.
      Once the UI element is displayed, the slots of both inventories will be displayed. So it'll be possible to drag/drop items from one slot to another (regardless of the inventory).

    • @jarekshark1356
      @jarekshark1356 3 года назад

      @@fluffygamedev thx alot. w8ting for ur next video

    • @fluffygamedev
      @fluffygamedev  3 года назад +1

      @@jarekshark1356 If all goes well, it should arrive by the end of the week or early next week. ;)

  • @minkey1337
    @minkey1337 3 года назад +1

    You are referencing to games like Stardew Valley and Minecraft, but both of them have networking, where one player can put items in a chest and another sees them, and can move them around.
    It complicates stuff a lot for me, and I am struggling with how to use Unity Mirror, and its SyncList to create a storage system similar to these games.
    Would you consider doing some Unity Mirror tutorials if you are comfortable with it?
    Thanks a bunch

    • @fluffygamedev
      @fluffygamedev  3 года назад +1

      This is very true! Handling replication can definitely complexify things.
      I must confess I have never worked with Unity Mirror. It could be interesting to learn though! I'll add that to my roadmap.
      Thank you for the suggestion!

    • @minkey1337
      @minkey1337 3 года назад

      @@fluffygamedev Would love to see Unity networking videos with your high quality of content!
      It seems like a niche with big demand, not many people touch this subject on youtube

    • @CineKiosk
      @CineKiosk 3 года назад

      did you find anything?! im trying to make chest sync with mirror and stuck so badly xD

    • @fluffygamedev
      @fluffygamedev  3 года назад

      @Octevel
      Hi !
      The Unity Mirror video I have planned is still pretty low priority compared to other videos I have planned.
      If it can help, we can discuss replication of the inventory via DM on Twitter or Discord. That way, we can find a solution before I get to making a video. 😉

  • @James-zp5ly
    @James-zp5ly 3 года назад +1

    I would really appreciate some more guidance of what to do with each script (like where to attach them and what the canvas needs), if it doesnt take too much time i (and possibly others) would really appreciate somehelp with;
    1 - what to do with the five folders we downloaded (assets, logs, packages, project settings and user settings)
    2 - how to implement the scripts from the scripts folder in assets
    3 - at 2:05 you started creating a new script, is that needed for something in particular? what makes it different from the rest of the scripts?
    I appreciate this video very much and i understand you created each and every script and folder but i am a little overwhelmed with what to do; for reference i am trying to recreate your 'minecraft like' inventory

    • @fluffygamedev
      @fluffygamedev  3 года назад +4

      Thank you for the detailed comment!
      I tend to ramble a bit too much about the code without explaining how it can be used in Unity. That is clearly something I want to improve for my next videos. I have a part 2 planned for this video. In it, I could take the time to come back at some parts to explain better.
      For now, here are a few replies:
      1 - The files in the github repository are part of the Unity Project. This project can be opened in Unity Hub by clicking on "Add" and then selecting the folder containing all the files.
      2 - Hopefully, this may be clearer if you open the Unity project.
      Here's a quick overview of the various components:
      * InventoryHolder: goes on any entity that has an inventory (player, chests, ...)
      * InventoryUIController: goes on the UI game object showing the Inventory.
      * InventorySlotUIController: goes on a prefab of the Inventory slot game objects. Using this prefab, we can then spawn slots from the InventoryUIController.
      * InventoryCursorController: goes on a UI game object that follows the cursor.
      * And all the classes in Scripts/Inventory/Core are classes that are used by the other components (so they aren't visible in the inspector).
      3 - This specific file is a Unit test. I use it to test the functionality of several classes. The idea here was to have a TDD (Test Driven Development) approach. Those tests aren't executed when launching the game, so technically, it probably wasn't necessary to mention them in the video. (TDD could be the subject of another video though)
      I hope this helps. :)

    • @James-zp5ly
      @James-zp5ly 3 года назад

      @@fluffygamedev Thank you very much! I'll definitely let you know how it works out, hope you know this has saved me many hours of copying people's code line by line, with something that i can use on most projects from now on.

  • @jamesgausepohl2669
    @jamesgausepohl2669 3 года назад

    So I’m new to coding, and I’ve got all the code and stuff but now it won’t let me put anything in the slots on the inventory holder thing

    • @fluffygamedev
      @fluffygamedev  3 года назад

      Heya, what do you do to add items in slots? Right now, the only way to do so is to trigger a loot event that will then be caught by the inventory holder.
      If it can help, we can discuss that more by DM on twitter or discord.

  • @HyproxLP
    @HyproxLP 3 года назад

    Great Video, Thank you!

  • @Pakietrejser
    @Pakietrejser 3 года назад +1

    good stuff

  • @DaRk5612Fromlv
    @DaRk5612Fromlv 3 года назад +1

    good video tnx

  • @James-zp5ly
    @James-zp5ly 3 года назад

    Just a quick question, I've been looking for about half an hour now and cant find the script that sets the text (when the object is actually in one of the slots) to that grey; i need the text that says "apple 1" to appear white as the background inventory image i am using is the same colour grey unfortunately, thank you

    • @fluffygamedev
      @fluffygamedev  3 года назад +1

      Heya!
      The text in inventory slots is handled by InventorySlotUIController. However, that script doesn't change the color of the text. To change the color of the text, you can modify the prefab used to spawn slots (In the Minecraft example the prefab file is Assets/Prefabs/Minecraft/InventoryMinecraftSlot.prefab).

    • @James-zp5ly
      @James-zp5ly 3 года назад

      @@fluffygamedev Thank you very much, it worked :)

  • @josuramos9777
    @josuramos9777 3 года назад

    I've been a lot of hours trying to edit your scripts to do my project.
    My objective is to use your inventory but taking things from the ground. My idea is to give a trigger to a gameobject and when my player triggers with the object add an item to the inventory, but I dont know how to add a function of OnTriggerEnter in your scripts, I've tried a lot of ways but I cant, thanks for your help :)

    • @fluffygamedev
      @fluffygamedev  3 года назад

      Heya ! :)
      To add items to the inventory, you can call *RaiseLootItem* in the *InventoryChannel*.
      So what you can do, is create a new script that would look roughly like this:
      (There's a commented section to check if it is the player has entered the trigger zone. You'll have to put your own code here. Usually, what I do is check if the *other* collider has a player component).
      ```
      using UnityEngine;
      public class LootableItem : MonoBehaviour
      {
      [SerializeField]
      private InventoryChannel m_InventoryChannel;
      [SerializeField]
      private InventorySystem.InventoryItem m_LootableItem;
      private void OnTriggerEnter(Collider other)
      {
      if (/*Check here if it is the player that has entered the trigger zone*/)
      {
      m_InventoryChannel?.RaiseLootItem(m_LootableItem);
      }
      }
      }
      ```
      Alternatively, you can create a generic script that detects if the player has entered the zone. But instead it invokes a *UnityEvent*. Then you can plug the loot event in the *InventoryChannel* directly in the in inspector.
      I hope this helps ! :)

    • @josuramos9777
      @josuramos9777 3 года назад +1

      @@fluffygamedev I love you man :) It works

  • @holyxskillz1793
    @holyxskillz1793 3 года назад +1

    How can I make it so instead of the inventory opening by clicking the button, it would activate when I press the button "I".

    • @fluffygamedev
      @fluffygamedev  3 года назад

      To open the inventory via input, I'd have a script check on update if the input is pressed.
      When the input is pressed, you can then call RaiseToggle in the InventoryUIChannel.
      There are several ways that script could be implemented. The easiest would be to handle that in a MonoBehaviour.
      Alternatively, if you have a state machine handling the states of the game, you can check the input in the code of a state.

  • @Croccolucho
    @Croccolucho 3 года назад

    Hey man, does this use prefabs? Like, when i go off the game and get back it will keep my inventory objects?

    • @fluffygamedev
      @fluffygamedev  3 года назад

      Heya !
      It does indeed use prefabs but mostly to spawn items in the UI.
      There currently is nothing done to save the inventory.
      (That could be the subject of a video though 🤔)
      To save the inventory, I'd probably save the data stored in the Inventory class.
      That way, when loading, all I have to do is load the data to the inventory class.
      The UI will then automatically be updated.

  • @322ss
    @322ss 3 года назад +1

    Thanks for the video! Anyway, IMHO all this has been covered by several tutorials. I wish someone would cover topics like how to create inventory for different item types, how to store unique items (items with stats like buffs and wear that are unique to that instance) and how to save/load such inventories. Also, not many inventory tutorials talk about special slots like weapon/armor slots that can only take specific item kinds, that might be stored in inventory.

    • @fluffygamedev
      @fluffygamedev  3 года назад +1

      Thank you for the feedback. :)
      Fun that you'd mention that. The next video I plan on releasing is a part 2 to this video. And it's going to focus on special slots (weapons/gear) and how they can affect stats. I also plan on making a video on saving/loading soon. 😁

  • @lieutenantundercover9329
    @lieutenantundercover9329 3 года назад +1

    Not very clear for beginners..

    • @fluffygamedev
      @fluffygamedev  3 года назад +2

      Indeed! This tutorial isn't targeted at beginners, so I should at least specify that.

  • @joeman123964
    @joeman123964 3 года назад +1

    only thing I'd recommend for new programmers is to NOT leech off this code. he covers the simple jist of the code, but this isn't a in depth tutorial. you can't just copy-paste code logic and say, "I'm a game dev!" you'll need to actually understand what classes are in the first place. what a catch throw is etc. otherwise, sure you'll have an inventory system, but you'll have no idea how to edit it or update to fit your own game. I'm saying this due to the number of very simplistic questions people are needing help with. such as accessing the inventory with a key input. that is very basic code knowledge. if you don't know how to do that, then you shouldn't be copy-pasting algorithms this complex. start off small first, then jump to the big boy stuff.

    • @fluffygamedev
      @fluffygamedev  3 года назад

      Good point !
      This tutorial was initially designed for an intermediate level.
      In the future, I should probably make my tutorials more newcomer friendly or be clear about the level.

    • @joeman123964
      @joeman123964 3 года назад

      @@fluffygamedev this video was excellent in my opinion. Most viewers tend to be new programmers, so I bet if you make a beginner tutorial you'll get more views or feedback. And yeah if you label the tutorials based on difficutly that would be original I have't seen a content creator do that yet.