EQUIPMENT - Making an RPG in Unity (E07)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll be adding equipment to our game we'll be making it easy to add new equipment inside of the editor as well as adding an equipment manager that will allow us to equip and unequipped these items from the inventory and thanks to a hands-off turn for the support on patron if you want to support the videos as well and make sure this channel keeps going you can do so at patreon.com slash practice and that let's get started so let's begin by going to our scripts folder and under items that's right click go create c-sharp script and let's create a scriptable object called equipment it's been double click it to open it up in Visual Studio and in the last couple of videos we created an item and inventory system so now instead of deriving from scriptable object we actually have a base class called item and so now if we just leave this class completely empty and go to the top here and make sure that we can create the object in the editor by going create asset menu we'll choose a default file name of say new equipment and a menu name of inventory / equipment so now if we go into unity and go into our items folder here we can right click go create and we now under inventory create a new equipment I'm just going to call this one the helmet of fire and I'm going to delete the two previous items we made we also have a bunch of test items lying around in our scene I'm just going to delete the third one and let's rename the first one here to helmet of fire let's rename the second one to helmet of protection we then duplicate our helmet of fire here and rename this one to a helmet of protection we also put the name here into the name slot and the same thing for helmet of protection and we can choose an icon for each one I'm going to give this one a placeholder sphere and this one a placeholder cube none of these are going to be default items we then take our helmet of fire and drag in our helmet of fire object and do the same thing for the other item so now we have created two pieces of equipment but so far they only have the default item properties we want to be able to define stuff like an equipment slot armor and damage modifiers and of course a graphic for our equipment so let's go into visual studio and first off we want to have a public int this is going to be our armor modifiers we'll also create one for our damage modifier and we'll be using these when we create our players stats but we also want to define an equipment slot you could of course go up here and define this as an integer public int equipped slot and then simply remember what number corresponds to what slot but this is very prone to error and doesn't feel very intuitive instead let's use what's called an enum and we'll actually write this outside of our class we do that because we don't want it to be encapsulated by our equipment class now we want to be able to use this in multiple places so we'll create a public enum equipment slot and then we can go ahead and define the different slides I'll just write head chest legs weapon you can put a shield in there and then some feet so now we've defined these different slots and instead of writing int we can now write equipment slot if we save that and go into unity you can see that we now get a drop-down menu where we can choose between these different options and of course our armor and damage modifier so for our helmet of protection I'm going to give this a big armor modifier say 3a no damage modifier but for a hundred of fire I'm going to give it an armor modifier of say two and a damage modifier of 1 now when we play this and pick up our items we can see that it's definitely working and they will show up in our inventory but currently nothing happens when we click on them it says using and then the item name but we're not really equipping them to do that we need to go into our equipment and in here we need to override our use method so we'll go public override void use and first off we'll call the base class as you can see this only prints out the name of the object that we're using but we might add something more important later we then want to equip the item and finally remove it from the inventory now in order to equip the item we need to define some kind of equipment manager to do that let's go to unity select the game manager hit add component and let's create an equipment manager let's say create an ad and open it up and the first thing we want here is a simple singleton pattern the exact same way we did it for the inventory script again if you want to learn more about Singleton's I will have a link in the description let's create a public static equipment manager called instance and India weight method will simply said instance equal to this let's wrap this in a region called singleton so we can easily collapse it now the most essential thing in this equipment manager is an array of all the items that we currently have equipped so let's create an equipment array called current equipment then when we start the game so inside of our start method we need to initialize this array and we need to give it the same length so as many slots as we have in our equipment slot innum now here we can use a pretty handy little trick in order to get the amount of elements in an enum we can use system enum here we have a function called get names and we give it the type which is our equipment slot and now this is actually a string array of all of the elements inside of our equipment slot so we can simply use the length of this in order to get the amount of elements and then we can store this in an integer called say number of slots and we can use this number to initialize our array so we'll set current equipment equal to a new equipment array with the length of number of slots cool next up we want to define a few methods first one is for equipping items so we'll create a public void called equip this is going to take in a piece of equipment let's call this the new item and of course we want to insert this item into our array but the placement matters because we want the different elements of our array to correspond to different slots luckily when we create an enum every element is associated with an index we can actually see this if we hover over one of them when see equipment slot head is 0 chest is one lexis two and so on so what we can actually do is get the index of the slot our new item is supposed to be inserted into so we can create some kind of integer called slot index and we can set this equal to new item got equipped slide and then we simply cast this into an integer so now if the equip start of our new item is say a chest we can see here that the corresponding index is 1 and so our slot index will be 1 and this will be the case for any chest piece equipment and so we can simply set current equipment and we get the element corresponding to our slot index and set it equal to our new item so if we now go into our equipment object and where we want to equip the item we simply call equipment manager dot instance dot equip and we feed it this item well then we should see that if we go into unity and here we can switch into debug mode in the inspector so that we can actually see the current equipment array we can then hit play we can see that when the game start it initializes with the size of six elements because we have six different slots in our enum when we then pick up a couple of items and try and equip one of them we can see the item gets inserted into the proper slot and if I try and equip the helmet of protection it gets swapped out this is also the case if we go ahead and create say a third item here just going to move this over and we'll call this one the sword of Destiny let's just switch out of debug mode here so let's right-click or create inventory equipment sword of Destiny and we'll give it the name of here as well we can set the icon just to one of the placeholders change the equipment slot to a weapon and the damage modifier to save 3 then we can select our sword of Destiny object and drag in our scriptable object and if we now hit play and pick up this sword of Destiny select our game manager and go back into debug view we can then click on it and you now see that our sword maps to a different part of the array awesome there's of course one problem and that is that items stay in our inventory after equipping them and so we can actually crypt them infinite amounts of times changing this is very simple inside of our equipment object we say here that we want to remove it from the inventory well let's go ahead and create a method for this let's actually put it inside of item since we have multiple items that might want to do this that then create a public void remove from inventory and this is simply going to get the inventory instance and call remove where we insert the item then inside of our equipment we can simply call remove from inventory so now when we play and pick up a few items we can see that when equipping them they get removed we can also see however that if we try now and equip the helmet of protection when we already have the helmet of fire equipped that it is going to swap out but the helmet of fire doesn't reappear in our inventory so we need to put in some code for swapping items we'll do that in the equipment manager here we can put in an if statement where we check if that's already an item in this slide so if current equipment and we give it the slot index is not equal to null but something is already there well then we want to add our item back into the inventory to do that we need a reference to the inventory we could just access the instance here but it's probably better to cache it so we'll go to the top here create an inventory variable called inventory instead of this start method will set inventory equal to inventory instance and now down here we can simply write inventory dot add and feed it the old item of course we also have to get the old item here so let's just go to the top here actually create a variable of type equipment and it's called this old item so by default this will be null and then if we actually discover that there is an old item will set old item equal to the item that sits in the slide so current equipment at this slot index place and then we'll add that back into the inventory so now we should see that if we save this go to unity and hit play we'll pick up a bunch of items here hit I and equip them you can see that every time we try and equip a helmet it simply swaps out with the previous one awesome but of course there's currently no way to unequip items and we haven't really integrated this as part of the UI but I thought our equipment manager should at least be able to do it so let's add the functionality and then we can make it so that when we press a hotkey we unequip all of items to do that let's create a public void called unequip and you will check in an integer with the slot index we'll check if there is any item at this slide index so if current equipment or input slot index is not equal to null well then we'll get the old items so equipment old item equals current equipment to slot index we'll add back the old items so inventory add old items will then set current equipment to slot index equal to null because we are unequipping everything so there will be nothing there now and that should be it we're going to create another public void called say unequip all and here we'll simply loop through all of the elements in our current equipment array and for each one we'll call unequip then we can check if we press a certain hotkey in order to do that or you could make some UI to trigger it we'll just create an update method where each frame we check if we press and the key that we're looking for here is key code view for unequip and if we do well then we can go ahead and unequip all so now we should see that if we're going to unity and hit play and we try to equip some items we can then hit you and all the items disappear from our equipment manager and reappear in our inventory awesome so that's pretty much our entire equipment manager the last thing that I want to do is add in a callback method that will trigger whenever our current equipment changes this is really good job because you want to update different things when we equip or unequip items we want to display the changes on our character and we also want to update our stats accordingly let's go to the top of our equipment manager and let's create a public delegate void we're going to call it on equipment changed and we're going to take in a piece of equipment which is our new item and another piece of equipment which is our old item we then create a public on equipment changed variable called on equipment change with a non-capital oh and now other scripts can subscribe to this callback method and we'll make sure to invoke it when we equip an item so we'll go if on equipment changed is not equal to null so if we have any methods to notify well then we want to invoke those methods so on equipment change dot invoke and we'll input our new item and then our old item and we want to do the exact same thing when we unequip an item so there and here of course we don't have a new item our new item is simply no let's now save that and we are going to be using this functionality yet but it's going to make it so much easier later on that's pretty much it for this video if you enjoyed it make sure to subscribe to them it's a future one is going to be doing the next to do in this series well show how you can display armor and weapons on your character so make sure to subscribe to this channel if you aren't already button that thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in July and especially thanks to hands of tuned Derek in skirt face Tamara Phi yes for Mickelson Stone gamer summer's volley Sybok Marine corporal Jason Lotito Erin Robert Burns and kudamon if you want to become a picture in yourself you can do so a pigeon come / cracky you guys Rock
Info
Channel: Brackeys
Views: 190,414
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, model, texture, models, textures, material, materials, easy, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, equipment, equip, items, clothes, armor, inventory, system, bag, slot, slots, character, clothing, RPG, sebastian lague, role playing game, modifier, modifiers
Id: d9oLS5hy0zU
Channel Id: undefined
Length: 13min 50sec (830 seconds)
Published: Wed Aug 30 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.