No video

Flexible INVENTORY SYSTEM in Unity with Events and Scriptable Objects

  Рет қаралды 78,542

BMo

BMo

Күн бұрын

Full Disclaimer: Displaying an inventory is not shown in this video - but everything you need to setup an inventory to display in the first place will be covered.
Using Interfaces/Scriptable Objects/Events we can setup a pretty flexible Inventory System in a clean way that will provide a lot of utility and longevity to your projects.
Every game needs to store some sort of items, and in this Unity Tutorial you'll leave with something you'll use quite a bit I imagine.
I only go into some basic fields, the fun part for you will be adapting it to fit the needs of your game.
COMMENT if you're having trouble or have a better way of doing inventories!
➤Followup Inventory Display Tutorial: • Simple Inventory UI in...
➤Previous Pickup / Collectible Tutorial: • Flexible Pickup / Coll...
✨Want to support the channel?
Buy me a coffee ☕: ko-fi.com/bmoli
SUBSCRIBE for all the follow up videos and to become rich and famous and beautiful
➤LIKE the video if you enjoyed, it really helps the channel!
➤Join our DISCORD SERVER to become a god amongst men: / discord
Thanks for watching!
#bmo #unity #tutorial #inventory

Пікірлер: 84
@sak5986
@sak5986 3 ай бұрын
First off, great tutorial, loved it. The only thing I'm wondering is how to implement a Use/remove functionallity with this. I've already tried event based, Raycast, buttons etc, but nothing has worked so far, so was just wondering. Anyway, great video BMo, hope to see even more in the future
@evanmiller2438
@evanmiller2438 5 ай бұрын
First of all, thank you BMo, this is an incredibly elegant and flexible system that you demonstrated. Thanks for the added delegate example, that helped me a heck of a lot to finally understand delegates (still not 100%). Posting this part for anyone else that might come across this tutorial and had similar trouble as I did understanding a few parts. I had a lot of trouble understanding the difference between the usage of the "inventory" list and the "itemDictionary" dictionary in the Inventory script. First of all, how you implement either of these entirely depends on how you will use the data from your inventory system later on. One thing that I got hung up on is that I thought these 2 variables were somehow linked together. They are not, they are independent. If you look in the inspector, you will only see the list. Even if you were to make the dictionary public, or serialize it, you wouldn't see it because Unity does not display dictionaries in the inspector. I believe BMo goes on in the next tutorial to use these two variables differently which is why he created both of them. I was so confused because to me personally I would have just looped through the dictionary keys instead of creating the list at all.
@user-pc2vv6ue8v
@user-pc2vv6ue8v 4 ай бұрын
Just wonderfullll, love it and first time watching your videos, now gonna subscribe and open notifs...
@user-pc2vv6ue8v
@user-pc2vv6ue8v 4 ай бұрын
And please keep creating more videos/contents for game development
@dibaterman
@dibaterman 2 жыл бұрын
So for people as dumb as I am here is how Events are working in this very simple illustration provided in the video: Starts with > OnTriggerEnter > Looks for if the other has ICollectible if it does it fires off Collect > Collect fires off the Event inside of the collected item > Your Inventory was listening for this and fires off Add. What I don't see is a way to specific an inventory. So if you have npcs with their own inventory, this would technically give all the npcs inventory the same item regardless of who set off the event? I consider it a big deal for me if I can read the code well enough to understand that-that is likely the case, but I'll know for sure when I test it after finishing the video.
@joshw5242
@joshw5242 2 жыл бұрын
I love your content! You're helping out with stuff that I have questions with and show it in a clear and concise way!
@mandamoon9149
@mandamoon9149 2 жыл бұрын
My happy inventory gets BIG when I see a new BMO video 😊
@ayeitskhi7549
@ayeitskhi7549 Жыл бұрын
Easy to follow along and packed with everything you need to get started another great video !
@BMoDev
@BMoDev Жыл бұрын
Thanks 👍
@phambaoha170
@phambaoha170 Жыл бұрын
very intuitive and easy to understand
@KaanErayAKAY
@KaanErayAKAY Жыл бұрын
Great tutorial, great explanation. I hope your channel grows more.
@Glori4n
@Glori4n 2 жыл бұрын
Thank you for the great insight!
@juanan_zzz
@juanan_zzz Жыл бұрын
Amazing tutorial. Thank you so much.
@unbrokenbrony5618
@unbrokenbrony5618 2 жыл бұрын
Doing God's work with your videos man!
@psy_gamer
@psy_gamer 2 жыл бұрын
Very good video! The system is quite simple and convenient.
@stefansigurdsson2146
@stefansigurdsson2146 2 жыл бұрын
Nice and clear tutorial!
@frostymug3123
@frostymug3123 2 жыл бұрын
Best Unity tutorial series I've come across props!
@Stompin40
@Stompin40 2 жыл бұрын
Another banger
@kaisonlee71
@kaisonlee71 2 жыл бұрын
Nice video man!! Can you make a crafting system using Scriptable object for your next content?
@suicune2001
@suicune2001 2 жыл бұрын
Thanks! This was awesome. :)
@puntalic
@puntalic 2 жыл бұрын
Would by nice if the code alse accounted for a maximum amount of inventory space, like slots, or max stacks. Not hard to implement, but thought that part was missing. Maybe something for a follow up.
@umapessoa6051
@umapessoa6051 2 жыл бұрын
Hi, thanks for the video, isnt easier to do a "public static event Action OnGemCollected" instead of a delegate with a parameter?
@BMoDev
@BMoDev 2 жыл бұрын
You can this was an excuse to explain delegates which i get asked about constantly
@umapessoa6051
@umapessoa6051 2 жыл бұрын
@@BMoDev oh alright, thanks !
@sevazakharenko8176
@sevazakharenko8176 2 жыл бұрын
Great video, very helpful! One thing I am currently struggling with in designing my game is being able to save the game with a complex inventory system such as this. Would you be able to point me towards a tutorial you may have surrounding this topic? I have a solution to this that seems rather tedious, but it seems like there are very few data types you can save using the "System.Serializable" tag. But I imagine there must be some way of easily converting scriptable objects or something into simple data types and then saving those to a binary file. Either way, any help would be awesome, thank you!
@molecular_master
@molecular_master 2 жыл бұрын
Ah man, why haven't you made this series earlier? This is a really clean and readable method compared to mine which is like spaghetti with meatballs :D
@darknside
@darknside 2 жыл бұрын
Nice content, thx man, i view from Russia
@adygombos4469
@adygombos4469 2 жыл бұрын
At 11:00 I think Action does the same thing.
@andrueanderson8637
@andrueanderson8637 2 жыл бұрын
Quick question about ICollectable + Gem as a class: Isn't "Gem" a game object? Making a MonoBehaviour called "Gem" seems to imply that the game object "is a" gem (inheritance) vs "has a" gem (composition). On the surface this seems to go against the notion of composition over inheritance. What am I missing?
@dibaterman
@dibaterman 2 жыл бұрын
Gem is attached to a gameObject thus making it a GameObject.. I think the disconnect here is that Gem is also and SO and Gem is also a list object. Similar data exists at the same time. In short the Gem on the screen, is not the Gem in the inventory, it's just a representation of data. The Gem class if I recall correctly is there to be picked up, so that technically is an ItemPickUp script that he named GemScript. Because it's a monobehavior you can do all kinds of things with it too, for example, since you are using SO you can set the items icon sprite and then on the pickup script tell the SpriteRenderer to use the icon as its sprite.
@eileeng2492
@eileeng2492 2 жыл бұрын
Interesting topic Thank you
@anmaavr8315
@anmaavr8315 Жыл бұрын
I love your content! Thank you for the awesome video! Is it possible to reference the scene to the Scriptable object instead of the item icon?
@farsquadfilmsltd.5997
@farsquadfilmsltd.5997 Жыл бұрын
is there a way to do an inventory check? (i.e. you need a specific key to open a door.)
@export_dev9164
@export_dev9164 2 жыл бұрын
Wouldnt it be better to have a general collectable pickup event so you wouldnt have to subscribe to the pickup event of every single collectable item you have in your game?
@samserious5483
@samserious5483 2 жыл бұрын
Very clean, thanx
@AsaFalyRayyan
@AsaFalyRayyan Ай бұрын
Great tutorial! But what if I have more than 1000 collectible items(gem, gold, silver, so on), do I have to create all script for each item called gem.cs gold.cs silver.cs, or is there a better way?
@hipppiejesus4567
@hipppiejesus4567 2 жыл бұрын
when did he script blueGemData ?
@PrototypeMarl
@PrototypeMarl Жыл бұрын
Either this doesn't work with Unity 2021.3.8f1 or I'm just doing something wrong
@sexymonkey30
@sexymonkey30 Жыл бұрын
it worked for me
@PandoraBox06
@PandoraBox06 Жыл бұрын
For whoever want a mix between item stackable and not here is the thing i did to achieve that (not sure if its the best but it work) In ITEMDATA add the following bool after your other variable : public bool isStackable; in INVENTORY above everything in using add : using System.Linq; and in INVENTORY still change the method Add(ItemData itemData) to : public void Add(ItemSO itemData) { var thisItem = inventory.FirstOrDefault(x => x.itemData == itemData); if (thisItem != null && thisItem.itemData == itemData && thisItem.itemData.stackable) { thisItem.AddToStack(); OnInventoryChange?.Invoke(inventory); } else { InventoryItem newItem = new(itemData); inventory.Add(newItem); OnInventoryChange?.Invoke(inventory); } } and for Remove method : public void Remove(ItemSO itemData) { var thisItem = inventory.FirstOrDefault(x => x.itemData == itemData); if (thisItem != null && thisItem.itemData == itemData) { thisItem.RemoveFromStack(); if(thisItem.stackSize == 0) { inventory.Remove(thisItem); } OnInventoryChange?.Invoke(inventory); } }
@disruptive_innovator
@disruptive_innovator 2 жыл бұрын
Thanks for the tutorial. Why use a List and a Dictionary at the same time? Maybe just use the Dictionary?
@BMoDev
@BMoDev 2 жыл бұрын
The List is useful for things like ordering/sorting your inventory, but again everything can be adapted to what you need for your situation. If it doesn't offer you any utility, scrap it.
@disruptive_innovator
@disruptive_innovator 2 жыл бұрын
@@BMoDev Makes sense. Thanks.
@Corummo
@Corummo Жыл бұрын
With this kind of approach, every single collectible item has a separate class with an inner event to subscribe to. So if I add a new item to my game, I have to create a separate class with its own new event to subscribe. And what if my game has to manage 1000 collectibles? This method doesn't take any advantage from the ScriptableObject real purpose, i.e. decoupling data from the source code. The ideal implementation would be limited just to the creation of a new ScriptableObject, leaving the core code untouched. That's the purpose of ScriptableObjects, as clearly demonstrated by a very old Brackey's video on the subject.
@mr.pillow
@mr.pillow 2 жыл бұрын
can u do 2-player controls?
@employerslave
@employerslave 10 ай бұрын
for example I have a key and I use it on the door how can I delete it from inventory?
@Gears2Game
@Gears2Game 9 ай бұрын
Get reference to the Inventory and on your on use key logic, call the Remove(itemdata) - your key itemData.
@DarkkneZPanda
@DarkkneZPanda Жыл бұрын
it seems subscribing to the events for the audio and text scripts from the previous video has errors because its a different delegate. Any way to fix this?
@novachi09
@novachi09 2 жыл бұрын
Don’t you think it would be better to just stick to a dictionary and use the List of values instead of the inventory list? It would make it a single source of truth for the whole inventory. I might be wrong tho since I’m a beginner when it comes to C# and Unity but for me it would make sense.
@Xu0mi_
@Xu0mi_ 8 ай бұрын
Hi! And how to make gui inventory in which the Icon and the Amount of the item will be recorded?
@TimelordSnowy
@TimelordSnowy Жыл бұрын
how to do this without events? Im not picking up items. But this is like exactly how I want my inv to function. But I keep getting a nullreference when i try to add items
@darkman237
@darkman237 2 жыл бұрын
What if you wanted separate types of items like a weapon, an armor, something consumable like a potion?
@Community-Compute
@Community-Compute Жыл бұрын
Add an enum to the ItemData Scriptable Object that stores an item type. That's a simple way.
@chritsfootballs
@chritsfootballs Жыл бұрын
You can also make an abstract itemData class then make different scriptable objects with different fields
@darkman237
@darkman237 Жыл бұрын
@@chritsfootballs That's a cool idea! Could you give an example?
@chritsfootballs
@chritsfootballs Жыл бұрын
@@darkman237 coding with unity has a video on it in his part one. int eh scriptable object inventory System
@darkman237
@darkman237 Жыл бұрын
@@chritsfootballs Thanks!
@bidjo666
@bidjo666 2 жыл бұрын
Hello, what about saves? How to save Scriptable Objects in a build.
@mysterycube9891
@mysterycube9891 2 жыл бұрын
hey man thank you for helping beginners. I have a question how to separate 2 game object with same script? i have 2 object and both of them have enemy script and on Death method i Instantiate an effect with event Action but when i kill one of them the effect plays for both of them even though one of them is still alive.
@firebiscuitgaming7624
@firebiscuitgaming7624 2 жыл бұрын
hard to say without looking at your code but my intuition is the bug might be in the code that decides which enemy to target for the death event.
@SeverskiyS
@SeverskiyS Жыл бұрын
maybe you use static events?
@anonym78654
@anonym78654 Жыл бұрын
Is it Working on 3d game
@butchinator1988
@butchinator1988 Жыл бұрын
Am I being an idiot? Where are the items being stored? Im not reaching AddNewItem as the inventory is already populated but I cant physically see the list. Is it stored somewhere in Unitys memory?
@blinken77
@blinken77 Жыл бұрын
Hi! Great tutorial but i am trying to make it so that if you try to pick up two swords of the same type you can only pick up one. Can you help me with this if it is possible?
@heribertosalais4678
@heribertosalais4678 Жыл бұрын
What is the meaning of the operator "?." on 0:45 in the line "OnGemCollected?.Invoke();" ?
@Worrior314
@Worrior314 Жыл бұрын
It is the short hand writing for: if (OnGemCollected != null) { OnGemCollected.Invoke(); } if he doesn’t check for null and nothing is subscribed to the OnGemCollected event, it would throw an error message.
@godzillakingkongvenom
@godzillakingkongvenom 2 жыл бұрын
🔥🔥🔥🥦🎮
@officiallambs
@officiallambs 2 жыл бұрын
Epic!
@urpwnned
@urpwnned Жыл бұрын
i have 0 clue what the hell "item" is referencing
@SeverskiyS
@SeverskiyS Жыл бұрын
isnt static events a horrible idea?
@JAKEAVALON-rg8xm
@JAKEAVALON-rg8xm 5 күн бұрын
I just used easy method GameObject flase and true😂
@heyreefes
@heyreefes 2 жыл бұрын
Naisu
@mike_b10
@mike_b10 2 жыл бұрын
🙏
@dkordy
@dkordy Жыл бұрын
Unbelievable, but all you developers. Everyone, literally. Something always goes wrong in the tutorial, and I struggle with that something for days afterwards. For example, here in this Inventory tutorial everything is perfect and you explained everything perfectly, but in the end when I created Inventory, and in Update() succeeded on some key when I press to hide it and display it Inventory - when I pick up the Item, of course it is not displayed in Inventory because for example the Inventory -panel is inactive at the time. So, in order for it to be displayed when I pick up an Item, I have to have the Inventory Panel Active (visible) all the time, otherwise it won't process - Slot wont be Upadated. Do you understand me? No Updates on the Inventory slot when the Inventory Panel is not active. And now I have to waste days and days until I find the answer to how to do it. You could have just mentioned - If you don't want the Inventory panel to be always visible, then you should do this and that in order for the slot to be updated. And of course you didn't say that, you left it out, and that's what everyone, literally everyone does, always leaves something out. Not everyone wants their inventory panel to be visible all the time... How i now to resolve that problem? Can you tell me?
@dkordy
@dkordy Жыл бұрын
PLEASE!!!!
@verenabecker2724
@verenabecker2724 Жыл бұрын
This is one of the most entitled posts I've seen in a long time. Dude, there are a billion different small issues that will crop up for different people implementing this in different circumstances and maybe using different versions of Unity. It makes you seem incredibly ungrateful if you're this cranky about a free tutorial not catering to your exact needs. No one here owes you their time and expertise.
@fatihemirhangungor8297
@fatihemirhangungor8297 2 жыл бұрын
What's your equipment ? Like keyboard ?
@BMoDev
@BMoDev 2 жыл бұрын
I have a custom mechanical keyboard, but brown switches
@Reaxgrim
@Reaxgrim 2 жыл бұрын
nm fixed it :D lol
@form2105
@form2105 2 жыл бұрын
I sub pog
Simple Inventory UI in Unity With Grid Layouts
18:59
BMo
Рет қаралды 22 М.
The Power of Scriptable Objects as Middle-Men
17:41
samyam
Рет қаралды 123 М.
Whoa
01:00
Justin Flom
Рет қаралды 31 МЛН
WHO CAN RUN FASTER?
00:23
Zhong
Рет қаралды 45 МЛН
20 Advanced Coding Tips For Big Unity Projects
22:23
Tesseract
Рет қаралды 178 М.
Unity INVENTORY: A Definitive Tutorial
35:21
Coco Code
Рет қаралды 110 М.
Be CAREFUL with Scriptable Objects!
8:27
Code Monkey
Рет қаралды 79 М.
I Made A Platformer Game But You're The Enemy
6:12
BMo
Рет қаралды 553 М.
Hack your brain with Obsidian.md
11:53
No Boilerplate
Рет қаралды 1,9 МЛН
A Great Way To Setup POWERUPS In Your Unity Game
13:43
Game Architecture Tips - Unity
19:52
Dapper Dino
Рет қаралды 138 М.