Creating The Backend | Unity Inventory System Tutorial - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello welcome to the first video in the inventory tutorial series if you haven't watched episode zero i highly recommend doing that because that's just going to go over kind of an introduction to the system uh demoing how it works as well as talking about kind of the skill level um that this tutorial is aimed at and how you can get the access to the files and all that sort of stuff so if you're coming across this video and this is the first one um i do recommend watching episode zero but with that said let's just get straight into uh episode one this first video or it may be two videos we'll see how we get on it's actually going to cover the system part of our inventory system the way to think of this is you know we've got um the code the code version all the one zeros this is the uh this system and then we've also got the um you know the ui element and it's actually going to display our inventory as you would expect in a game so there's going to be an inventory screen with different inventory slots each of these slots will have an item in it um and it'll have an amount of that item which is the stack amount so we're going to be covering this part in the first sort of one or two videos and then once we've got the framework for the system set up we'll start to build on the ui and as we need new functions um of the system from the ui for example um splitting a stack is something that we'll need to do we can go back and we can um add this to our system and expand our system but we need a framework first for the ui to have just something to display in the first place and the way the system is going to work is there's going to be three parts there's going to be a inventory holder think of this as uh you know we've got a player and then they've got their hotbar which you could think of as a utility belt they might have a backpack you see a holder can have multiple systems and that's different to the actual assist the inventory system is all of this but they we will have a class called inventor inventory system hopefully that's not confusing so we've got the code system and the ui sort of representation of this system and then we've got a inventory holder which is our player with a backpack and hot bar utility belt uh this could also be you know a chest will hold an inventory system so the system's going to be a container for all of our inventory slots so you can see on the ui representation we've got the inventory system and then it's made up of different slots so system and slots kind of go hand in hand here to talk about them so we've got the holder which is like a player or a chest that can have one or multiple systems and a system is made up of one or multiple slots so again the system is a container for slots and an actual slot is one of these individual squares so you know we've got our inventory slot which could have a it's a really bad sword but it's got a sword in it and they've got two swords in their inventory slot in another slot they may have a potion and they could have five potions and they can have as many slots as they want um in each system and you can have multiple systems so a chest would have a system with say 10 slots the hot bar has got um you know normally in a game you'd have a hot bar across the bottom of the screen like this each and they'd have a number of slots on it and the player base play press one two three four all the way through to zero on their keyboard so that's ten slots so a hot bar would have ten slots a backpack could maybe have twenty the chest could have uh 15 slots in it so each of these holders has the systems which have the slots inside them and hopefully this will make sense as we start to build upon this and it's this kind of system that allows us to have stackable items so each slot is going to keep track of the item it has and the stack size and we could define how big this stack size can get within the sort of items property so the item may be flagged to have a max stack size of uh five so our potion here say we've got a max stack size of five and we've got five in our inventory and then we pick up another potion on the ground um we can see that this is currently at its maximum capacity so we'll just fill the next slot with the potion that we've just picked up so hopefully this all makes sense if it doesn't or you want some more clarity let me know in the comments below and i can do some follow-up videos to talk about this as i said as we actually go through the code um hopefully this will all start to begin to make sense so let's just jump over into unity okay so this is just a blank 3d project within unity you know we could have had a 2d project it doesn't matter which pipeline you're using just create a new project um and just so you know not again not that it should matter but i'm using unity 2020.3.22 f1 the first thing we want to do is um you know we we need to define what exactly an item is uh just so we can have something to store in an inventory so i'm just gonna right click and go to create folder and i'm just gonna and i'm just gonna call this script and within our scripts folder i'm just gonna make a new folder called item scripts i'm just gonna right click here create c sharp script and i'm just gonna call this uh inventory item data i'm just going to open this up in visual studio so the first thing to do with our inventory item data is to get rid of monolith behavior and we're going to derive from scriptable object which means we don't need these and to make these uh visible in the inspector we just want to come up here and open the square brackets and go create asset menu uh menu name is equal to inventory system forward slash inventory item so now when we go back over to unity uh we go i'm gonna go back to our asset folder right click create uh folder and i'm just gonna call this scriptable objects and now we can right click create inventory system and we've got our inventory item which i'm just going to call test item you can see that there's no sort of properties on that just yet and so we can go ahead and create those for the inventory system um all we're gonna really care about here is uh we need a public sprite for the um items icon and we need a and we can define a public int for our maximum stack size so for this tutorial these are going to be the only things that we need to use however this is going to feed into the rest of your game presumably so um when defining an item some other stuff you may want is a public int um you know id a public string for the display name uh you could have a text area with a description and you know you can define whatever it is in your uh game an item is you may have an item type or a weapon type um or armor type you know whatever you want your inventory item to be that's that's down to you but the ones that we're gonna use actually in the inventory system is an icon so we can see what item we've got in the inventory and then we need a max stack size um which as i explained will define how many of the item an inventory slot can actually hold it's going to make sure that's saved and let's go back over to our inventory system project and i'm going to go back to scriptable objects and you can see that our test item now has an id a display name description uh a slot for an icon and a maximum stack size for now i'm just going to put 5 in the maximum stack size and we'll import some sprites later on when we need them we'll leave id alone and but for this i'll just say test item one and this is a test item okay that's great so um i'm gonna rename that to test item one and then we can make another inventory system inventory item and we could say test item two test item two description this is another test item and we can hold one of these this has got a max stack size of one great so we've defined what um an inventory item is now so now we can make a slot to actually hold the item so let's go back over to our scripts folder and i'm just going to right click create um folder and i'm going to call this one the inventory scripts open this up and then i'm going to right click create c-sharp script inventory slot again this isn't a modern behavior it's not going to sit anywhere on this scene um so it doesn't need the mono behavior it doesn't need start or update what it does need is um it needs to be marked as system dot serializable so we can actually see these in the inspector then i want to serialize a field again so we can see these um in the inspector i want a private inventory item data which is that scriptable object we've just made and i'm going to call this item data and then we want a serialized field private int called stack size so this this is going to keep track of how many of the item data we've got access to and then i'm just going to make some public properties so we can get this stuff elsewhere so i'm going to say inventory item data equals item data not equal sorry we use this um use this separator for the expression body definition and then we want to do the same for the public int um stack size so we can accidentally change these from outside the script but we can get a reference to them using the public properties and then we need when we're making an inventory slot uh what we do want to do is we want to make a constructor so we can say public inventory slot and this is going to take in some source data uh for example test item one test time two and sword of doom whatever the inventory slot is going to contain um we can pass in a source and we can also pass in an amount of the item if we want to construct it with more than one item so item data equals source and then we can say stack size is equal to the amount that we've passed in but i'm also going to want a public inventory slot with no parameters it sets the item data to null and stack size is stack size equal to negative one so that's essentially null as well and the reason we're doing this is because if we go back over here our bag or you know the hot bar the hot bar has the slots on it regardless of whether there's an item in them these are all slots that exist but there's no item in there so there's null so i'm on my mouse now instead of my tablet so the item is equal to null even though the slot's there there's no item in it and it doesn't have any amount in it either so we'll set the uh amount to minus one just so we know that that's also null as well and we can check for that if we need to anywhere down the line but these slots they they exist they're they're made they're ready to go they're ready to hold an item but in their default empty state they don't have anything in them then we can make an inventory slot with nothing in it or we can make one that's full of stuff and give it an amount of that stuff and actually what's probably better is we may want to um clear a slot as well at some point down the line so we can say public void clear slot and then we can actually do this here and then just call clear slot from our um constructor and then we're not having to write this code out again because we probably want to be able to empty out a slot for example if we if we delete an item we can just clear this slot instead of creating a new slot and replacing replacing them we can just set this slot back to empty and then um what we want to do is we can make two methods here so we can say public bull add to stack i'm going to say int amount and then we can just say stack size plus equals amount and public void remove from stack and we can pass in an amount as well and this will be stack size minus equals the amount that we're passing in and before we add to the stack it's probably good if we have um a method that checks whether there is actually room left in the in the stack and if there's not going to be enough room in the stack for the amount that we want to pass in and then let us know how much room is actually left in the stack we can just make two more methods here so we can do a public bull room left in stack which will take in the amount to add and it'll pass back out the int amount remaining i'm going to make another version of this method that just takes in the the amount to add because we may not always need to know how much is remaining and so we can just do public ball room left in stack and make sure these are named the same and this will just take in an amount to add so we'll do this one first so we can say if stack size plus the amount to add so if we've uh if we've picked up two of an item we're going to pass in two on the amount to add so stack size plus amount to add is less than or equal to the item data which again is that scriptable object we've made the item data.max stack size then we can just return true if we add this amount to the stack size then we're not gonna encroach on the maximum stack size so if our maximum stack size was five we had a stack size of three and we tried to add two to the stack then that would equal five so that'd be fine because the stack would be it would have room left in the stack to add this amount to it so it's just going to return true otherwise there's not enough um room in the stack so we'll just return false so for example if this was four and we tried to pass in two and the maximum stack size was five then four plus two six that's bigger than the maximum stack size there's not enough room left in the stack for this amount so for this version we can just return our other version of the method passing in the amount to add but we also need to pass back out the amount remaining and the way we can do that is we say amount remaining is equal to the item data dot max stack size minus stack size and we'll need this later on for splitting stacks and combining stacks for example using this um as an example again if the maximum stack size was five the current stack size was four and we were trying to add two to it which would take it to six we want to combine the items and leave one item kind of on the mouse so just to explain that from our test project if i pick up some items here so the maximum stack size for these hearts is five and i've got three here and i'm going to pick up three and i want to combine them together so i'm going to place two from the mouse and leave one on the mouse so i'm going to go up to the maximum stack size you can see that we've added two we've got one left we put that back down but i can put the four on the one which brings it to five and that's all done through checking the whether there's room left in the stack then we will work that out um later so it's good to have these here now but we won't use them till a little bit later on there's one more method that needs to go into the inventory slot class and that's about how to split stacks but we'll come back to that when we actually need that one so with that done let's go back over to unity and we're going to create another um script so now we've defined what exactly a slot is and that we can clear them and manipulate them in some way we can right click create c-sharp script and we can start now working on our inventory system as in the system that holds the slots so again this isn't going to be a mono behavior so we can get rid of that and we can get rid of the start and update and again we want this to be system dot serializable and then we can do serialize field and this is going to be a private list of inventory slots so this is going to hold the amount of inventory slots that we that we want so inventory we can just call this um inventory slots and then we want a private int which is going to be our inventory size which we're going to pass in from our holder which will send the inspector so which is it in inventory size and we just want to make a public getter for our public list of inventory slots and i'll just call this inventory slots and it will return our inventory slots actually we can get rid of the private inventory size and what we could do is we can do a public int inventory size and then we can pass back our inventory slot dot count which should be safe to do because when we make a new because in the constructor when we make a new inventory system we will be taking in a size and that'll set up the inventory list is the very first thing it does so that should be safe we also need to add to using statements so we're going to be using unity engine dot events we can alert whoever cares that our inventory has changed i'm going to be using system.link um to do some stuff as well so we'll include that in our using statement so then we can make a public inventory system constructor which is going to take in a size all this is going to do is it's going to create an inventory slot list passing in the size that's come through it now lists you know they can grow and shrink but we know we're going to need this many so we may as well just do it now and construct the list with the size and then we can just say for int i equals zero i is less than size i plus plus and then we can do inventory slots dot add new inventory slot and we're going to make an empty one which is going to be null and with a stack size of -1 so that's our inventory system done so when we create a new inventory system we're going to pass in the size of the system and then that's going to make um enough inventory slots to fill fill the system sorry i forgot we didn't put our action in after i said we would so we're doing a public unity action and we're going to pass in the slot that changed on inventory slot changed this is going to be an event that fires when we add an item to our inventory so let's go back over to unity again and now we can define um our inventory holder so again c sharp script inventory holder and i'm just going to mark this as system.serializable this can be a mono behavior okay so in our holder script we just need to make a serialized field private int inventory size so we're going to set that in the inspector and again here we need to be using unity engine dot events and this is going to come into play later and but we're just going to make a public static unity action passing in an inventory system and this is going to be on dynamic inventory display requested so just ignore this for now uh you know it kind of says what it's going to do on the name of it but we'll come back to that when we need it so as well as the event we want a public static we can serialize another field here and this will be a protected inventory system which is going to be called inventory system and again i'm going to make a public getter for this so we can get it when we need to from other scripts inventory system and this will pass our as we'll get our inventory system so in our awake method we can say the inventory system is equal to a new inventory system with our inventory size so other scripts are going to inherit from this this is why this is protected not private and by default all inventory holders will have one inventory system on them for example chests will have one inventory system on them but we're going to make a player inventory holder which will have two inventory systems it'll have its backpack and it'll have a um but for now this will this will do fine so we've got inventory holder um with one inventory system so let's just see this uh how this looks now in the inspector so if i make um just an empty object and we'll call this player well we'll add we'll build up on this um as we go along so i can add our inventory holder script to this and i can give it an inventory size of 10. so our inventory system in a minute is kind of it's zero size it's empty but with our inventory size of 10 when i hit play you can see that that's created 10 slots and each slot on our inventory takes in an item data with a stack size so the way this looks in our test project if i go over to my player top down player with inventory i've got my player inventory we have an inventory system here if i just hit play we walk over some items you can see that now there's five items which is represented here with our item data which is our test item and we can pick it up and move it between different slots so that's what we're building towards that's the visual representation of the system so that's how that looks obviously it's a bit boring and plain here so the last thing to do in this video let's just make it so our player can actually pick something up from the ground so i'm just going to make a capsule underneath our player um object that we've just made here i'll just call this uh graphics because it's the player kind of graphic i'm just gonna move it up so it's sat on the floor i'm going to remove its capsule collider i'm going to put the collider on our player where the actual player object and i'll just make i'll put box collider not and we'll move this up and make it bigger i'm just going to right click create an empty and i'll call this test item one reset the transform on this i'm going to create a 3d object and i'll create a sphere and i'll just move up move our item i use the term item lightly here just going to come over to our assets folder and create a um material and i'll just say red just pick a nice kind of pale red color so here's an item here's our player so what we need for our player to actually pick up this is we need a mono behavior to sit on our test item so go back over to your item scripts folder under scripts i'm going to create a new c-sharp script and i'm just going to call this um item pickup so in our item pickup script um it's going to need a sphere collider or some sort of collider for our player to kind of hit and collide with i'm just going to require component type of sphere collider and i'm gonna do a um public float pickup radius which i'll just set to one for now we can always change it in the inspector if it's too much or not enough and then it's gonna need some public um it's gonna need some item data to show which kind of item it's actually representing and then um we just need a reference to our sphere collider called uh let's say my collider in an awake function let's get a reference to our sphere collider so get component sphere collider and i'm going to say my collider dot is trigger is equal to true my collider dot radius is equal to our pickup radius now in our ontrigger enter function so on trigger enter i'm going to create a variable called um inventory so just var inventory i'm going to say this is equal to other dot transform dot get component and it's going to look for an inventory holder on the things that hit it if it finds an inventory holder so if inventory is not equal to null so if we could just say if uh what we could do is say if inventory uh then we could just return so if there's no inventory if this is null if it hasn't found an inventory holder script we'll just return out of this we don't care about what's next but if inventory isn't null then we can just say if inventory dot inventory system and we're going to want to add it to the inventory so add to inventory which doesn't exist we need to define that we need to make that so if i go back over to our inventory system.cs file we can just make a public void sorry public bool add to inventory and this is going to take in the item so the inventory item data and we'll just call this uh item to add and it's going to take in an int which will be the um amount to add and for now i'm just going to directly and i wouldn't this isn't how it's going to work obviously um but i'm just going to add this directly to our to the first item slot in the list just so we can see this all working we'll set this equal to a new inventory slot and i'm going to pass in the item to add and the amount to add and then i'm just going to return return true this is not how we're going to be doing it um properly we need to when we actually do this properly we're going to be checking whether the inventory already contains the item and if it does add it to the stack and if it's if the stack's full we'll get a free slot that isn't full etc um but just to see this kind of work in um i'm just botching it to kind of overwrite that first slot so we say add to inventory we can pass in our item data and we'll just say one for the amount to add because there's one item in the world with the physical representation you may want it so for example in minecraft if you drop 10 uh torches all 10 are represented in that one item pickup so this could be 10 and but for me i'm just gonna have one in the inventory there i'm just going to destroy this object if if i've successfully added myself to the inventory which is indicated by whatever we return so in this case i'm just returning true by default because we're overwriting the slot then i'm going to destroy this item if we don't add ourselves to the inventory we won't do anything so we'll just come back over to our project and then we just need to add to test this a rigid body to our player turn off use gravity check is kinematic now if we select our player uh we've got our inventory system here we've got our test item here we can drag our item pickup on it and then we can say the item data this is a test item one put the pickup radius here and also our sphere collider is dropped down because we moved our sphere up so instead i'm going to put our sphere center and then we'll just bring up our test item i'm going to remove the sphere collider from our [Music] 3d object and actually we need to say destroyed this dot game object as well not just to this so if we've got a player selected you can see the inventory holder script there if we hit play so we've got 10 slots the first slots empty if we go forward we pick up the item it deletes from the world and we add one of the item to our test um inventory and we add that item to the very first inventory slot and again i've just hard coded that to always go into that first slot and overwrite it and make a new slot this isn't how we're going to be doing it properly the next video we're going to search our inventory slots and see if we can find the item that we want to pass in and if we can we're going to add the amount to it if we can't find it we'll get a free slot and add it to the next freak slot that's available and by three slot i mean a slot with no data in it and if it can't find a free slot and it can't add it to an already existing item then obviously our inventory is full so it would return false and then the item pickup wouldn't destroy itself from the world but for now i think that's quite a good place to leave this first video if you're watching these as they go out the full series is currently up over on my patreon which is patreon.com you can also get the files for each lesson so i'm going to export this script as it is currently now out as a package and all so you can download the packages in stages as you go and again they're all over on patreon if you need help with anything in this video that we've covered uh feel free to leave a comment below or better yet go over to the discord which again is linked in the description over on the discord either me or someone else on the discord could jump in and try and help you that's probably the best way to get help other than youtube comments because you can actually share links to your code etc watching these on youtube as they go out the next video will be up in one week but in the meantime thanks for watching and i'll catch you in the next video bye you
Info
Channel: Dan Pos
Views: 35,425
Rating: undefined out of 5
Keywords: unity inventory system, unity inventory, unity inventory system tutorial, unity inventory tutorial, unity inventory ui, unity inventory list, unity inventory slot, unity inventory system 3d, unity inventory hotbar
Id: svoXugGLFwU
Channel Id: undefined
Length: 34min 41sec (2081 seconds)
Published: Sun Dec 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.