INVENTORY CODE - Making an RPG in Unity (E06)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll be making our inventory system and we'll combine it along the way with the UI from the last video so at the end you should hopefully have a fully working item and inventory system that looks something like this let's get started so the next step is updating our inventory as we go to do that lets go to our canvas let's create a new component called the inventory UI for this component we won't be using anything under your collections but we'll keep the sudden update and the first thing that we want here is a reference to our inventory because we set up a singleton in the last video we can get that by simply going inventory dot instance but to make sure our code runs at optimal speed let's go ahead and cache this let's create an inventory component called inventory and then in the start method that said inventory equal to inventory instance then what we can do is create a method we call this update UI India will have all the code for updating the items in our inventory but now let's just throw a debug deadlock statement saying updating UI but of course we want to call this method somewhere luckily in the last video we set up a nice way to do this as well we set up a callback method so if we go inventory dot on item changed callback we can then subscribe to this event to do that we use plus equals and we write the name of the function which is update UI remember inside of our inventory script we're triggering this event whenever a new item has been added or removed and we've now said that we want the update UI method to be called whenever that happens so just by saving this code going into unity and hitting play we should be able to right-click an item it then says picking up helmet of protection and then updating UI so a function is correctly being called what we then need to do is look through all of our inventory slots until each one what items are stored if any to make this easier let's go ahead and create a script on our inventory slot which we'll just call inventory slot and this will keep track of everything happening on a particular slot this will update the UI on the slot and the functions that define what happens when we press it or press remove so let's open this up again we can delete everything under collections and both the methods instead we want to create a variable of type item this variable is going to keep track of the current item in this slot we then create a method for adding an item to the slot we go public void add ID this is going to take in an item which we'll call the new item and we'll simply set item equal to the new item but we also want to update our icon to do that we need a reference to the image component on our icon object and whenever we deal with UI in scripting we want to be using unity engine dot UI now we can create a public image which is going to be a reference to the icon then you know add item method we go icon sprite equals the item icon and icon dot enabled it's going to be true we also have a method for claiming out a slot so public void clear slot and here we'll simply set item equal to null I can dot sprite equal to null and icon dot enabled equal to false let's then save that go into unity now we have an icon flood that's dragging the icon for this slot that's it apply to apply this to all the other inventory slots and now we simply need to loop through all of the inventory slot components and for each one called add item if there's an item to add if not we want to call clear slot let's jump into our inventory UI script and you when updating the UI we want to find all of the inventory slots and as we can see in our hierarchy all of our inventory slots are children of the items parent object so if we get a reference to this we can simply find all the components in the children so let's reference this in the script let's create a public transform called items parent let's then also create an array of inventory slots so let's create inventory slot array which we're going to call slots and inside of our start method we can set slots equal to Adams parent get components in children and you want to make sure you find the plural version so there's an S here so we want to find all the components and the components we're looking for are the inventory slot components now you could do this every time inside of the update UI method that's of course less performant but if your slots are changing you might be forced to do that but since our image our slots are completely static we'll simply do this once then in the update UI method we'll loop through all the slots that will create a for loop which will want to continue as long as sighs let's then slot that link and so we're looping through all of our slots and for each iteration we want to check if there are more items to add so if I is less than inventory items count within there are more items to add so we'll take our eyes slot called add item on that slide and pass in the corresponding item in our inventory items array then if we don't have any more items so we are the violence to add well then we simply want to call clear slot on that slot now if we save this go into unity and find our canvas we'll need to drag in our items parent let's drag that in and if we now hit play our script should be working so if we right click on one of these objects you can see it says interacting with test item we're picking it up and it's showing in our inventory but we haven't given it an icon yet so it's not looking very good also on our third item it says not enough room it's because we need to go into a game manager and set the amount of spaces to 20 if you haven't done that already then we can find our items here and the three items in the scenes here are all the same item which is the helmet of protection and for this we'll just drag in placeholder one let's actually rename this to helmet let's duplicate it let's rename this one to shield and we'll just set the name here to placeholder shield and here we'll drag in the second place holder and then we can take say the third item here and drag in our shield to use that instead so now when we hit play we should be able to pick up first helmet of protection then another helmet of protection and then third our shield now we can click on these but not much will happen and the remove buttons aren't showing either to get our remove buttons to show inside of our inventory slot will add another reference this one is going to be to the button we'll call it remove button and when we add an item we want to set remove button dot interactable to true and when we then clear it we want to set remove button dot interactable back to false but of course we also want something to happen when we press the remove button to do that let's create a public method that we will call whenever the button is pressed let's create a public void and we'll call this on remove button and here we simply want to go ahead and call inventory dot instance dot remove then we want to pass in our item if we say that go into unity find an inventory slot here we now need to drag in our remove button so let's drag that in we'll also need to select our remove button down here we'll add an on click event and we'll point this to our inventory slot go under the inventory spot script and make sure to call on remove button whenever it's pressed we can then apply this prefab hit play and now when we pick up an object we can see the remove button appearing when we press it we remove it from our inventory now currently the object just disappears into nothing this is actually something a lot of RPGs do including games such as World of Warcraft you might want to put some kind of prompt on the screen or you can have the character actually drop it on the ground that's all up to you what we want to add it's a way to use the item so inside of our inventory slot let's create another public void let's call it use item here we want to check if we actually have an item so if item is not equal to null and if we do we want to go item dot and then we want to have some kind of use function on that item to create this let's go inside of our item script remember we created this in the last video here we want to create a public and we'll actually make this a virtual void called use here we'll use the item and something might happen but no matter what we're going to throw out a tea bar deadlock statement saying something like using and then the name of the item now the reason why we aren't just implementing some functionality here is that for different items we want different things to happen when we try and use them some items might sit in your inventory only to be used as currency or for drafting others might have a direct effect like a potion and some might be equipped onto the character so by marking this as virtual we can certainly derive different objects from the item and then define for each one what we want to happen when it's used but we'll make sure to call this method for all of them inside of our inventory slot so now we'll select our item button and add an on-click event here as well which is going to link to our inventory slot we'll go to the inventory side script and here called use item again let's make sure to reapply and when we now click on an item in the game it's going to say using and then the name of the item we also want to be able to bring up and hide our inventory when needed we're going to our inventory UI script he will create a reference to our entire UI will right public game object inventory UI then in our update method we want to check if input does get button-down and here we want to define some kind of button for the inventory and if you press this button we want to toggle our UI so we'll go inventory UI dot said active and we want to set it to its reverse state so we'll check whether or not it's currently active by going inventory UI dot active cells and we'll then take the inverse of that let's close that off let's save it and now in unity we want to go edit project settings input and if we want to add another element to our list of inputs let's expand this to 19 and this is not going to be canceled instead we want this to be inventory as a positive button will do I and as our alternative button will do B so I for inventory and B for back win then select our canvas and drag our entire inventory object into the inventory UI shot now when playing we can use PI and B to bring up our inventory but we can still move when clicking on our inventory to fix this we'll go under scripts open up the player controller at the top here we want to be using unity engine dot events systems then inside of our update we want to check if event system dot current dot is pointer over game object and this is a function so make sure to put in the parentheses and if it is we want to return so here recently accessing our current event system to check whether or not we're currently hovering over UI if we are well then we want to exit out before we control our player let's save that going to unity let's disable our inventory by default here and hit play now we should be able to pick up some items bring up our inventory pick up some more click around on these to use them and delete them and our character will remain stationary finally let's just organize this by creating a folder under our scripts we call this items here we'll place our item and item pickup we'll also create another one this one we'll call inventory here we'll place all of our taury script finally let's create a folder called prefabs let's just drag our inventory slot into their period that's probably the most complex system we've ever covered in these tutorials great job for hacking your way through that lets really much it for this video and the next one we'll have a look at equipment that's something I'm really looking forward to until then thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in July and a special thanks to hands of stone Derek James Kirk faithful verify yes for Mickelson Stone gamer Thomas volley Sybok Mamiko Cabral Jason Lotito Aaron Robert bond and Judah man if you want to become a picture in yourself you can do so a pigeon come / brekkie you guys Rock
Info
Channel: Brackeys
Views: 360,358
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, easy, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, inventory, system, inventory system, RPG, collab, role playing game, item, items, slot, slots, equipment, equip
Id: YLhj7SfaxSE
Channel Id: undefined
Length: 11min 41sec (701 seconds)
Published: Wed Aug 23 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.