Make A Game Like Pokemon in Unity | #52 - Creating Inventory System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to part 52 of my pokemon game series in unity so in this video we'll create the inventory system we'll architect the items like this using scriptable objects and inheritance and then we'll show the items in the inventory in our ui like this all right so this list of items is loaded from the player's inventory and as i change the selection you can see that the icon and description changes so let's look at how to do that special thanks to all my patreons for making the series possible by becoming a patreon you can support me and get access to the complete project files of the series the project files also contain some advanced features that are not covered on youtube let's start the video so let's create the items so we are going to architect the items using scriptable objects just like we did for our pokemons and moose but in the case of items we also have different types of items right we can have recovery items like potions and ether and then we can have items like pokeballs and we can have key items so we have different types of items and each type will have different logic right so if we implement all these types in a single class then the class will become really big with lots of false conditions and that's not a clean way of doing it so to solve this problem we will be using inheritance so what we'll do is we'll create a base class called item base and then we'll create separate classes for each type of items okay so each types of items will have separate classes like this and all these classes will inherit from the item base class so in this way we can cleanly separate the logic of different item types into their own classes so let's start implementing this so inside scripts i'll create a new folder called inventory and in here first i'll create a class called item base all right let me open that with visual studio okay so since we want our items to be scriptable objects the item base is going to inherit from scriptable object all right and inside the item base class we'll put all the common properties of an item so what are the common properties that all the item types will have so all the item types needs a name description and an icon right so let's create those properties here so first i'll create a variable for the name and then i'll create one for the description and finally i'll create a sprite variable for the icon okay so let's also create properties to expose this so now we have the item base class so next we can create classes for the different types of items so in this video we are only going to implement the recovery items so let's go to unity and create a script for that okay i'll create a script called recovery item and this is going to inherit from the item base class okay so this is how you inherit from a class and when you do this the recovery item class will have all the members of the item base class all right so until now we have been using inheritance all the time so when you write this what you're actually doing is you are inheriting from the scriptable object class right and the same thing for mono behavior so when you write a class like this here you are inheriting from the monobehaviour and that's the reason why this class has all the properties of a monobehaviour and it can be attached as a component to game objects right so we have been using inheritance all the time but this is the first time we are designing a system using inheritance so one thing to note here is for the recovery item i'm only inheriting from the item base class i'm not inheriting from the scriptable object again so this is because item base is already a scriptable object and when you inherit from item base the recovery item will also be a scriptable object so next we want to be able to create instances of the recovery item class right so since it's a scriptable object we can simply use the create asset menu attribute for that so i'll use this attribute and for the menu name i'll put it inside items and i'll call this create new recovery item so now if you go to unt you should be able to create instances of the recovery item scriptable object so i'll go to my game folder and inside resources i'll create another folder called items and inside here we'll create all our item scriptable objects so if you right click and go to create you can see a new menu here for the items and if you click on create new recovery item it'll create a new instance for us so i'll name this item something like portion so here you can see this item has name prescription and icon so that means recovery item is inheriting those three fields from the item base class okay and now we can add new fields inside the recovery item class and it'll appear below these three fields in our inspector so let's go ahead and add the fields for the recovery item so the recovery items should be able to recover things like hp bp and then should be able to cure statuses and then we can also have items like revive that can heal a fainted pokemon so first i'll create a variable car hp amount so this indicates the amount of hp that should be restored so if it's a portion it'll be 20 if it's super portion it'll be 50 and so on so below this i'll create a boolean call restore max hp so this can be used to create items like max portion that will restore the hp fully so next i'll create similar fields for bp so i'll call it bp amount and restore max vp so this will allow us to create items like ether and maxetto and all that so next we need items that can cure status conditions so i'll create a variable of type condition id and i'll call this status okay so here we can specify the status we want to cure and then i also create a boolean call to cover all status so if you want item to recover all the status conditions then you can use this so next to create items like revive i am going to create a boolean variable called revive and then i'll duplicate this and create another one for max survive so we should be able to create most of the recovery items with these fields so now if you go to unity you can see that all the fields that we created inside recovery items appears below the base details all right so now the one problem is the inspector doesn't look clean right now right we have lots of fields and let's say if we are creating a portion then we only need to look at the fields for modifying php so to clean this up we can use the header attribute so here above the hp fields i'll put a header attribute all right and for the header i'll call it hp so we can do the same for all the other fields so i'll call this bp and i'll call the status conditions and finally i'll add one for the revive also all right so now if you go to unity you can see that the inspector is much more cleaner so now let's fill in all the data for the portion so first i'll fill in the name and description and then i'll choose an icon for it okay i'll just choose this icon and portion should restore the hp by 20 points right so i'll set the hp amount as 20 all right so next let's duplicate this and create the super portion all right i'll change the name and description and i'll just use a different icon for it and it should restore the hp by 50. so next let's duplicate this and create the max portion so i'll change all the base details first and in the case of the max portion all we have to do is set the restore max hp boolean to true okay and we can set the hp amount back to zero its value doesn't matter but just to avoid confusion i'll set it to zero in the future if we want we can create custom editor for this and if we do that we'll be able to hide fields based on the value of other fields so for example if i turn on restore max hp then we'll be able to hide the hp mount but it's not really important for a small inspector like this so i'll just skip that for now so now i'll pause the video and create few more icons so that i don't waste time so i'll pause the video and create a few more items so we have ether that will restore the pp and then we have items like attitude and awakening that'll cure a status so this way you can create all the recovery items in the game and you can also add more fields if it's required so next let's create an inventory script that can hold all these items so inside my scripts inside inventory i'll create a new script called inventory all right so inside the script we can have a list of items so here which class should be used should we use the recovery item class or the item based class so we should use the item based class all right the reason is because we want to store all types of items inside our inventory right so if we use the item based class then inside this list you can store all the classes that inherit from the item base class all right so even though this is a list of item base it can also hold recovery items all right i'll call this items so next inside the inventory we also need to store the count of each items right so here instead of storing the list of items i want to store the list of item slots all right and each slot will contain an item and its count so below the inventory class i'll create another class called item slot alright so inside this first we need a reference to the item so i'll create a variable of item base and then i'll create an integer variable for the count all right and i'll just add a serialize field here so that it can be assigned from the inspector so it's not really required to assign the item slots from the inspector but the thing is when creating the inventory it will be really easy to test if we can assign from the inspector so next let's create properties to expose these items all right so now instead of creating a list of items here i'll create a list of item slots and i'll call this as slots and also make this a serialized field so that it can be assigned from the inspector so one more thing to notice that since we have a list of item slot class here the item slot class should be serializable in order for it to be shown in the inspector so i'll just add a serializable attribute on top of it okay so to use serializable attribute we have to import the system namespace so now we have our inventory with a list of item slots so now let's go to unity and assign the inventory to our player all right so i'll put the inventory script on the player and we can assign different item slots here okay so i'll set the size to something like 6 and i'll assign the items that we created earlier okay i'll also give a count for each of the item all right so now our player has an inventory with list of items so next we need to show these items in our inventory ui when we open this right so in the previous video we have created an inventory ui script and attached it to the inventory ui so this script is inside the ui folder right now so i'll just move it into the inventory folder all right and inside the inventory folder i'll create another folder called ui and i'll put all the ui scripts inside that all right so i hope that doesn't break anything okay so we still have the reference here so next we show the list of items in the inventory over here right so currently inside the item list all these items are just a placeholder so when we open the inventory ui we should remove all these placeholder items and show the actual items in inventory so we have to dynamically add these item ui objects to the item list from the code so for that i'll turn this item ui object into a prefab so that we can create it dynamically so inside the game folder inside ui i'll drag and drop the item ui here and turn it into a prefab and i'll actually name it item slot ui okay since it's the ui of the item slot so now let me go inside this prefab and we need to attach a script to it so that we can easily reference it and we can get a reference to the name and context so inside scripts inside the inventory and in the ui folder i'll create a new script called item slot ui okay so in the script we need reference to the name and count text so i'll create variables for that and i also create a public function called set data so this function will take an item slot as the parameter and it will use its value to set the name and count in these texts so let's do that so i'll say name text dot text equal to item slot dot item dot name all right so next i'll set the count text so for the context i'll put it like this so that in the ui we have this in too simple before showing the count okay so let's attach the script to the item slot ui prefab all right and then we need to assign the name and count text okay so next let's create the item slot ui dynamically from the inventory ui script so in inventory ui first hand interruptions to the item list game object we need this since the item slot ui will be spawned inside it all right and then we need interruptions to the item slot ui prefab so next we need to get the list of items from the inventory right the list of slots is actually inside the inventory and we need it here in order to show that list in the ui so i'll get the reference to the inventory from the inventory ui script so in the awake i'll cache the options to the inventory script so how can we get the inventory we can't simply use get component here since the inventory ui script is attached to the inventory ui game object and the inventory script is actually attached to the player right so we can't simply use get component instead we'll have to get a reference to the player first so we can get that by using find objective type player controller and now we can use the get component to get the inventory from the player object all right so to keep this clean i'm going to put this inside a function inside the inventory class okay so in here i'll create a static function that'll return an inventory and i'll call this function get inventory and inside it i just have to [Music] paste this line and just return it like this all right so now from the inventory ui i can simply say inventory dot get inventory all right that's much more cleaner so now we have the inventory and the list of items in it so let's show the list of items in the ui so in the start function i'll call a function called update item list and let me create this function over here so inside this function first we have to remove all the existing items inside the item list right and then we have to create an item slot ui for each item inside our inventory so first let's go ahead and clear all the existing items so for that we just have to remove all the children of the item list game object right all the item slots are the children of the item list so we just have to remove all the children so we can get all the children's from the transform of a game object so here i'll write a for each loop to loop through all the children's so we can get the children from item list dot transform and i'm going to call destroy on the game object of all the children okay so this is how you destroy all the children of a game object in unity so next we have to attach all the items in the inventory so for that i'll use a for each loop to loop through all the items slot so we can get all the item slots from inventory dot slots all right we haven't exposed the slots yet so let's go to the inventory script and create a property to expose the slots all right so now we should be able to access it from here okay so for each slot we have to instantiate a slot ui prefab right and then we have to attach it to the item list game object so first i'll instantiate the prefab and in the second parameter we can pass the transform of the parent so what this will do is this will instantiate this prefab and it will make it a child of the parent object that we passed so in our case we want it to be a child of item list so here i'll just pass item list dot transform all right so this will instantiate the prefab and add it as a child of the item list object so next let's store the instantiated object in a variable and then we can call the set data function on it and we can pass the slot from the inventory okay so this is the function that we created earlier to set the name and count in slot so let's go to unity and test this so first we need to add references for the item list and the item start ui so let me assign the item list first and then i'll assign the item slot ui prefab all right so let's try testing this now we have to actually make the inventory ui inactive by default and let's test now okay so if we open the back you can see that all the items from our inventory is correctly shown in the inventory ui all right so that works fine so next we need to be able to select different items from this list using up and down arrow keys so let's do that so we have to write the code for selecting items in the inventory ui script so we have already written the code for selecting an item from a list multiple times we wrote it for menu party screen move selection and all that so i'm not going to write it from scratch once again instead i'll just copy the selection code from the menu all right so this is the code for the selection so let me copy that into the handle update of the inventory ui all right so now we have so many errors so first let's create a variable for the selected item create it here all right so next instead of using menuitems.com we have to use inventory dot slots dot count okay and next we have to create the update item selection function so we can also copy that from the menu controller okay all right so here we can't use the menu items list instead we need to use the list of item slots that we instantiated from here so first let me create a list to hold all the instantiated items so here i'll create a list of item start ui all right and i'll call this slot ui list and when we instantiate the slot ui we should also add it to the list okay so first let me just initialize the list from here and now we can add the instantiated prefab into our slot ui list all right so now we have all the slot ui inside this list so in the selection code we can just loop through the slot ui and here also we have to use the slot ui okay and we can't use the color directly since we have two text inside this class right we have name and we have count text so we want to set the color of the name text so i'll say slot ui list dot name text okay we haven't exposed the name text so let's create a property for that and while we are here we can also expose the context so now from here i'll change the color of the name text okay so let's also do that from here so that should take care of the item selection for us again the reason why i'm not explaining the selection code is because we have done this so many times if you find this confusing then watch part 49 where we implement the menu we have done the same thing there so now let's go to unity and check if the selection is working okay so the selection is working but when we open this for the first time we didn't have a selection right so that's because we we have to call the update item selection when we open the inventory why okay so when we open the inventory ui the update item list will be called and from here we can also call the update item selection function all right so let's test now so yeah now we have a selection by default and we can select different items using the up and down arrow keys so next when we select different items its icon and description should be shown over here right so let's do that so first we need a reference to the icon image and description text so let's create that okay i'll create an image for the item icon so we have to import unity engine.ui to use image and then i'll create a text for the item description okay so now when the update item selection is called we can also set the icon and description of the selected item right so first we need to get a reference to the selected item right now we only have its index so let me get the selected slot so i'll say inventory dot slots of selected item okay so this will return the slot at the selected index so now we can set the icon and the description so i'll say item icon dot sprite equal to slot dot item dot icon all right why does the item icon have capital i over here okay i have mistakenly named it like this so let me rename using ctrl r all right so we have set the sprite so next let's set the description so i'll say item description dot text equal to slot dot item dot description actually we can get the item from here itself so that we don't have to write it twice over here okay that's much better so let's go ahead and test this okay we forgot to assign the item icon and description in the inventory ui so let's go ahead and do that okay i'll assign the item icon and description and let me just deactivate it and test once again okay so yeah now if you change the selection you can see that it's correctly showing the icon and description of the image over here all right so i'll stop the video here in the next video we'll look at how to handle scrolling when different items are selected using up and down arrow keys so before you leave make sure to leave a like on the video and consider subscribing to my channel so i'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 1,734
Rating: undefined out of 5
Keywords: make a game like pokemon in unity, make pokemon in unity, unity inventory system, unity inventory tutorial, unity 2d inventory tutorial, how to make inventory in unity, how to make inventory in unity 2d, unity item system, unity items tutorial
Id: 3v7Sm_NhMzA
Channel Id: undefined
Length: 37min 48sec (2268 seconds)
Published: Mon Jun 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.