Flexible INVENTORY SYSTEM in Unity with Events and Scriptable Objects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello youtube today i want to show you guys how we can walk in two items and pick them up and have them actually saved in an inventory system this is going to be able to support item stacking so here you see it says blue gem total stack is now four but i will say up front you'll notice i'm not showing any display of the inventory and that's because there's just so many different ways of doing it i'm going to show you that in the next video but regardless of how you want to display it this is a good way of at least storing the data i just posted an entire video on how i handle my pickup and collection system but to rehash really quick i have an interface called i collectible that has a public void collect method i have this gem class that inherits for my collectible and it currently just has an event called on-gem collected a collect method we are forced to implement where we destroy the game object and fire the event and then on our player we have this collector script where we basically check for an eye collectible component and if we find one we tell it to collect itself that's all i'm going to cover there watch the video if you are confused okay so let's start this tutorial and so we actually want to create three separate scripts we want to create an inventory script we want to create an inventory item script and we want to create an item data script and without going into the scripts yet the only thing i want to do in the hierarchy here is right click go to create empty i'm going to call this new game object in inventory and we'll attach the inventory script and that's it so you should have created three scripts and an inventory game object now let's go into our first script which is going to be item data so in this item data script this is basically going to be as it's aptly called it's just going to hold a couple data values for our items and this is actually going to be a scriptable object not a mana behavior so we can create these in the editor at the top we'll say create asset menu and then the only things i'm going to store here is a public string display name and a public sprite icon so the display name and an icon with that we can then go into our assets folder and right click and go to create and you'll now see item data at the top so i can make something like blue gem data as a scriptable object asset and in the display name i can put blue gem and the icon i'll put in my art asset for the blue gem feel free to add as many fields as you want for your items but these are two really basic ones you should probably have and the icon is useful for when you want to display this in an inventory slot in the ui we can use that icon okie dokie so we made a scriptable object item data and then we actually made one called blue gem data but now let's actually go into our inventory item script all right so i emptied out our inventory item script and i can get rid of mana behavior because this is just going to be its own little class here and we can add serializable to the top of our class it'll complain until you add using system okay so what do we want our inventory items to worry about right think about an inventory item as a specific slot in a ui of an inventory well we know we're going to need some item data so we can say public item data and then this is optional but we probably also want to know how many of the same item we picked up or like how many are in the stack if you don't want items to be stacked in your game then just don't do this but if you do then we probably need an integer of how many of the current item we have in our inventory we can say public int stack size and then we want to make a constructor so we'll say public inventory item we'll pass in a item data value i'll call this item and then in here what we want to do is set our item data variable to item and then we want to increment the stack so i'll just create two methods right here add to stack where we increment our stack size and remove from stack where we decrement from our stack size very simple just utility methods and then once we create an item and store it as an inventory item well we want to add to stack because our constructor is actually creating an inventory item so every time we create one well we should increase the stack size so we have our item data which is just a name and an icon and then we have an inventory item that basically says hey we have that item data and how many of them we have so far so now let's actually have an inventory where we store inventory items okay so i have our inventory script here it's a mono behavior and it's attached to an inventory game object right now it doesn't have to be a mono behavior by the way this could just be its own separate class and then you don't have to worry about attaching it to anything in game i'm gonna use amount of behavior because i'm gonna assign some events to it on a enable and so that just makes sense for me but it you really don't have to so we need a list of inventory items that's the basics right we have this inventory item class and this is what's actually going to be directly stored in our inventory these inventory items so we could say public list of type inventory item and we'll call this our inventory now in your game if you don't care about this stack size integer you don't have to do this next part but if you do then we also want to have a dictionary so that we can handle stacking our items so i can say private dictionary of type item data as the key and then the inventory item as the value and i can call this item dictionary right and so what we're going to be able to do is every time we try and add an item to our inventory well in order to make inventory items we need to pass in an item data which is going to be the key right so if we're passing in say that blue gem data we'll say hey is it in the inventory yet we'll check the dictionary if there's no results then we'll create it for the first time and we'll add this inventory item as a value to the dictionary otherwise if we do blue gem data and we actually find it because it's like the fourth one we're picking up we'll just tell it to increment the stack we don't need to actually store like 10 blue gem inventory items we just need to store one and then tell it to increase its stack size makes sense we could just initialize a new list of inventory items and a new dictionary have item data inventory item and then we just need to have an add and remove method right so we could say public void add and we need to take in an item data now like i just described we want to check to see if this item data is already in our dictionary right does this already exist in the inventory and so we could say if item dictionary dot try get value and then we'll pass in oops forgot to make a variable sorry so this would be item data or whatever you want to name it all right so we'll try and get the value of item data does this exist in our dictionary if it doesn't find anything it's going to return null and we won't enter this if block but if it does find something then we want to actually reference that inventory item value being stored on the dictionary and so in c sharp we can say out inventory item and i'll call this item get rid of that parentheses there you go right so we try and get the value of item data and if we find something then let us use that inventory item right away and in here what we could say is item that we got from the dictionary dot add to stack so we found one and we increased its stack size otherwise if it doesn't exist yet in the inventory then we want to create a new inventory item so we'll say inventory item new item equals new inventory item and we'll pass in our item data we'll take our inventory list and tell it to add the new item as well which again is all you really need to do if you don't care about stack size but since i do care about stack size i'll say item dictionary dot add and then for the key we'll say item data right and then for the inventory item we'll say new item i feel like i've already said item too many times so once again do we have this in our inventory yes we do increase its stack size no we don't well create a new inventory item and then store it in the list and the dictionary so that next time we pick up the same item we can just increase its stack size all right and then we just need a way to remove these as well so we can say public void remove we want to pass in an item data as well call that item data and then i'm just going to copy paste this if block statement it's the exact same as add and here what we can say is item dot remove from stack so if you had four of these and you lost one your stack would go down to three but let's say we only had one of these in our inventory and then we removed it and now it's at zero then we need to remove it from our item list in our item dictionary so we could say if the item dot stack size is equal to zero then we know we need to remove it from our inventory so we'll say inventory dot remove item and then we need to remove it from the item dictionary so we'll say item dictionary dot remove item data is the key so a little lengthy to go through but really not too bad just a few lines of code and now we're completely done with our inventory system so now we have these gems out here in our scene right they have a circle collider 2d with is trigger set to true and they have this gem script so we have this gem script right this is from my last video let's update this so when we collide with this gem and we fire on gem collected well then we want to tell our inventory to add but right now you'll see that the only types of methods that can respond to on gem collected is an action which has a void return type and no arguments if we look at add we'll see we have a void return type here but we need to pass in an item data argument so in order to define that as the type of methods that can respond to this event we need to create what's called a delegate so i'm actually just going to copy paste this method signature of add as a reference for you guys and i'll paste this here and so we need to define a delegate that has a void return type and an item data argument so i can say public delegate avoid return type the name of our delegate doesn't really matter it's just a way to reference this method signature so i could say handle gem collected and then it takes a item data argument right so void and void that works the name's irrelevant and then item data matches what we have item data we should be good and then instead of action here what we want to do is take types of handle gem collected hopefully that makes sense to you delegates are kind of weird to wrap your mind around the first few times you see them but they are very simple the name's irrelevant it's really just the return types and the arguments that are important and so now you'll notice that it's actually complaining when we call on gem collected it's saying there's no argument of item data being provided so we actually need to provide something what we want to do is make a reference to the scriptable objects so we can say public item data and then i'll call this gem data and we can pass gem data into the event on our gem prefab in the scene now i can click and drag this blue gem data scriptable object we made before and put it in our gem data variable now okay so this gem data is going to hold our item data in our inventory on enable we want to say gem dot on gem collected plus equals add and then on disable we can just say gem.jump collected minus equals add so we're safe and removing listeners when we don't need to be calling them but we can ignore disable for now so what's happening here is when we fire on gem collected we're gonna pass in that gem data to add and we'll try and see if that's in our dictionary if it is then we'll increase our stack in our inventory if it's not we'll create a gem item so i can add a debug log statement here saying how many is in our total stack and i can add another one here for when it's our first time being added to the inventory i'll say it's the first time so now i can walk into all of these different gems we can check the console and you'll see at the top it says added blue gem to the inventory for the first time blue gems what's on our scriptable object as a name and then all the other times it's now the stack is two three four five six and so that's it that's all you need to set up your inventory system it's a little bit of work but this is a clean flexible approach of doing it that uses scriptable objects interfaces and events so it's definitely intermediate level i'm sure if you're a beginner you might have some questions so comment down below i'm happy to help walk you through any of this stuff and obviously i'm not going to cover the visual here like i mentioned at the beginning that's for next time but the main point is you now have this inventory items class and a list of them in your inventory and in here you're going to be able to pull off like names icons and the stack size which is probably the three most important things you need in any inventory display so like the video if this helped you out thanks for watching i'll see you guys in the next one [Music] you
Info
Channel: BMo
Views: 74,277
Rating: undefined out of 5
Keywords: bmo, unity, tutorial, inventory, system, inventory system, unity inventory system, inventory unity tutorial, how to setup an inventory system unity, how to setup an inventory unity, how to make an inventory unity, how to inventory unity, beginner, c#, lesson, learn, how to, game dev, game development, storage, events, scriptable objects, unity scriptable objects, unity3d, unity2d, 3d, 2d, unity inventory tutorial, inventory tutorial, inventory system unity tutorial
Id: geq7lQSBDAE
Channel Id: undefined
Length: 13min 7sec (787 seconds)
Published: Thu Mar 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.