PROPER Way to Inventory in Unity with Scriptable Objects! #2 - Adding Items!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so let's set up the interface i'm going to create a um yeah i'm going to create a new canvas here ui panel let's say and this is going to be our inventory panel i guess um an inventory panel should obviously have a title to let the user know that they're on the inventory so i'm going to unlock my inspector there panel let's make it let's give this a nice brown i guess color give me some more alpha there we go um yeah i'll do and then i want this to be in the center of my screen with let's give it a width of 500 yeah i'll do and then hot dot 500 as well yeah that's fine um again you can design this user interface however you want to make it look uh i kind of want to give it a blue not gonna lie yeah that would be fine okay um title this should say inventory now the title will be displayed across the top so i'm gonna hold alt and shift so that the anchor point and the everything else is selected as well um that's fine there center center aligned by geometry best fits color should be white and once you can set to whatever you want to set bold text um that's fine heights let's increase it a little bit like so beautiful now we have nice inventory text there now there's one thing that this inventory is obviously missing and that's some slots so we want to i'm going to create a slot prefab um and some of you will see this happen uh let me do this in the uh what's called the 2d sandbox series so i'm going to create a panel again as we slot i'll call the slot panel i guess cool uh we'll make this darker set this up to the sensor width would be let's say 60 i guess and heights 60 as well and this is just going to be set up just like any other grid sort of inventory you know like minecraft terraria or whatever you know basically exact same thing so ui image goes underneath the slots make sure you hold shift and alt and set this image to scale i'm going to give it a padding of 5 from every side yeah that'd be fine and you can test this just by adding any item into it that's how it's going to look beautiful uh let's do that cool and that's our first slot done i'm going to create a prefab of it by dragging and dropping it into the assets folder and now we have a slot panel prefab beautiful now one thing obviously is that one slot is not enough for our inventory so i'm going to create a empty object underneath our inventory panel and this is just going to be called slots and this is just going to hold all the slots in your inventory so i'm going to drag and drop our slots onto it there cool so let's create a few more slots just by doing ctrl d duplicating it position should be 60 and that will put it right next to it but i want some padding in between them so i'm just going to plus 5 next to it as well now i can duplicate another one and say minus 65 and i'll put it right in the center here with the other side and if i say my 65 again it will put it next to it so i'm going to move these all up as well to let's say 65 yeah that'll do i might put another slot on either side so again say minus 65 on this one and then duplicate this one and say plus 65 on this one and i'm just setting this up for you know the purposes of this tutorial you can make this look actually nice um ignore these white squares we're going to set that up in just a sec duplicate all of these by selecting them all and then ctrl d cause why we're gonna now set zero now select all of these again ctrl d and i'm gonna say minus 65 and there we go we have some slots of our inventory set up now you can add as many as you want doing the same method set this up however you want you know this is again like i said purposes for the tutorial now inventory manager should keep track of where all our slots are stored in the scene view so i'm going to have a public game object uh slots in fact for the purposes of making this you know actually good code i'm going to make this serialized field all of these public functions serialize field and private uralize field and serialize field and then private and i'm going to do the same for this one the realize field private the reason that's important is because you don't actually want these things to be public they're not being accessible via other script but you want them to be visible in the inspector so we add serialized fields and they're a private variable and that means show them in the unity inspector but i'm not going to use them anywhere else with that set up let's call this slot holder there we go and then i want a private uh private game object list or game object array um called let's call this slots and this is just going to keep the game object of every slot it allows us to add and remove things a lot easier so um let's set that what we've done so far in the inspector so if i click on the inventory you'll see that we have a slot holder object now i'm going to drag and drop the slots object with that done now we can access all these slots so um i can say slots equals new game object array and the size is going to be slot holder dot get child other transform got child accounts there you go so there we go that's fine and then we want to obviously set all the slots um and that way the way i'm going to do it is for each actually this could be a lot easier if i just do a list i think uh that said it is an array yeah we'll do an array yeah let's do an array because one thing actually is that i set this up incorrectly over here because i want the first slot in the slots panel if i open this up the first slot is actually in the center here which is not how i'd want it to be i want the first slot to be this one so what i'm going to do really quick is rearrange my slots uh here so that it goes one two three four five six seven eight that way when i add an item you can easily go to the first one then the second one and all that so i'm going to do that real quick and i'll be right back so i'm going to chop this bit up okay so i've rearranged this so now it goes from one two three four five um and then i can duplicate these and do the same thing i did before just by saying zero duplicate these again and saying minus 65 that way when i go down all of these it should just go down oh these ones are a bit messed up here did i do something wrong one two three four one two three this should be there not there um and now one two three yes okay cool that's fine that's set up so that that's one the most important thing you want to think about when setting up your inventory you want to make sure your first slot is they go in order in which the slots are going to be filled up if that makes sense so with that set up now i can again go back into our code and say for each uh well i'll say four into i equals zero i less than slots dot child count uh slot start transform dot lots dots transform oh wait slot holder that's the one so holder.transform.child accounts there we go and then i plus plus and what it's going to do is say slots i equals lot holder dot transform.getchild i got game object cool and i don't actually need these extra brackets here that's fine for us cool and that's just going to mean that now we have all the slots in there stored here so now i'm going to have a refresh function so this is a public void which is going to refresh user interface it doesn't need any parameters that's fine and this function is going to be called every single time we add an item so i'm going to say refresh use interface and every time we remove an item refresh ui cool and i'm also going to call it just before we add well i don't have to call it after this but yeah so after we initialize all our slots i want to say refresh user interface just because now we've started so what this is going to do is look through every single item in our inventory and determine yeah determine if it is uh what's called has an item there or not has an item if it has an item they're going to set its image so it's a bit confusing to explain but i'm going to show you guys what i mean right now so it's going to look through each slot game object and one thing i do need to add here is using unity engine unt engine dot ui there we go that means now i can access the image components and stuff on the slots so now i want to say for each game object uh slots in yeah for each gamer in slots that makes sense there we go now i want to say slot dot transform dot get child and remember that's how we set up our um slot so each slot um if unity loads each slot here its first child is an image and we're going to set the image here so um in fact i should probably make this a counter loop yeah okay so i'll say 4 ins i just like this one again i equals 0 i less than slots dot length i plus plus um now i can say slots i dot transform dot get child 0 dot get components image equals and it's going to equal items i dot item icon beautiful ah dot sprite that's the one okay so it's going to set the sprite equal to the item icon now what happens if we don't have enough items here so that's going to give us an error obviously now the way i'm going to do that is just by saying instead of looping through each slots well actually i should loop through each slot right and then i should try i should try this okay now if successful it's just going to set the item icon if it fails it's going to catch and i catch an error exception and instead of you know stopping everything it's just going to say okay there was an error okay now the error could be one of two things either there's no item in that slot or it's out of the index arrange so we're gonna be able to fix both with just one line and that's just by saying um so either channel this sprite equals null there's no item there and we also just want to say got image dot enabled equals false that's just going to get rid of that white square that likes to appear and i'm going to say here equals true so just in case it was disabled now we want to enable okay um and basically i think if i've done that right that should mean that um we should see all our items here refresh so let's press play and see which errors we get or don't get there we go as you can see now our inventory it's got a whole bunch of empty slots and it's got coal and apple in the first two slots okay so now if i want to add apple again i don't actually have a way of refreshing user interface so i should do that as well let's add a another sword and then we'll add two swords let's press play and then we should see a few swords as well as you know there we go perfect so it's added up an apple remove this initial sword that's good so now we have a way of storing items so perfect we're one step closer and now what happens if we want to store two apples in the same inventory uh not that one apple there we go so what will happen at the moment is for the uh ad function we'll just add it to the end and then you know that's fine now that's one way of setting up your inventory um however in most cases you want these items to stack in your user interface i'm gonna set that up now so each slot should obviously have if i open this slot here there we go you'll have an image as the first child and then the second child will be a ui text and this text is going to be quantity and then i can set this to be just the bottom here there we go we'll do that that's fine width and height but it should be 30 here we go and then this is just gonna be quantity here set it to the bottom corner color can be white best fit go for it um aligned by geometry yes and it can be bold fonts cool um now question is why is this why is that moving stop moving ah right maybe that's why no huh okay well it doesn't want to work for me that is fine um since i've applied that to the uh slot though they should all now have quantity um in uh text if i press play they'll all say quantity zero beautiful um and again you can set this user interface however you want to set up i'm literally just doing for the purposes of a tutorial now um so slots i equals this and then we also want to set the image so closely there got dot child dot text um dot text will equal zero let's save for now and then in another case it will just say there's no item there so set the text equal to nothing and that will be fine there so now if i press play every slot that has an item will display zero every slot that doesn't have an item will display um zero oh sorry nothing my brain it's turning off so let's press play beautiful that is working nice so the only issue now is that we don't have a way of tracking um you know which item has how which slot has how many items in it etc so i'm going to create a new c sharp script and call this slots plus now slot class is going to be just its own class it's not going to inherit anything like the other classes did so let's open up slot class um this is not going to be a monobehaviour class it's not going to be this it's just going to be literally some data so it's gonna have a private uh private private private let's say private item class item and then it will have a private int quantity now we're going to need to create a constructor for this class so i'm going to have a public slot class and this constructor is going to take in an item class underscore item and a int underscore quantity just like so and that way i can say item equals underscore item and then quantity equals underscore quantity so now we need to create some getters and setters for this uh there is a shortcut to do so but i'm going to show you guys the manual way because it's fun not really but we're just there with the manual way for now to avoid some confusion so i can say a public item class get item and this is pretty much the exact same as the other item class scripts here it's just going to return item instead here we go and then i'm gonna have a public int um get quantity quantity there we go and it's going to return you guessed it quantity beautiful all right cool um that is literally set up that's our slot class so now instead of having a um instead of our inventory having a list of items right our inventory is going to have a list of block classes so change it from item class to slot class there we go um that would mean the current items we have set up won't obviously work so what i actually do is uh maybe i should make this serializable for the purposes of the tutorial i'm going to make this c system.serializable you should not add this because you should be you know you you i mean technically i guess you can um yeah you know what just add it if you want to add it realistically this should be managed via your code entirely and i'm going to show you how to do so in literally now so now i'll add now obviously this is going to give us some errors i'm going to just comment these out for now and you'll see that this line also gives us an error so now slots dot sprite blah blah blah is now going to equal items dots it gets item dot uh where is it item icon now when it comes to unity technically you don't need getters and sellers like this technically you can just make this public however when it comes to general programming practice you should be using getters and setters like so um you shouldn't have all these public variables and stuff in fact you should technically do the same for you know this as well for example public tool type you should have this as a getter and a setup etc so perhaps that's how we don't need it we have a constructor that will do but yeah that's just some information for you guys who care which is probably not a lot of you but that's fine um and now a text is going to equal items i dot get quantity there we go uh uh this should also be dot two string as well uh in fact rather than doing two string i'm just gonna add an empty string to the end of it like so um because i can't so either or would work but yeah so that's fine now add function won't work anymore that's also fine our start function will also be a bit broken but that's fine we're going to fix all that now so our add function first it should check the entire inventory to make sure that no other slots no either a slot contains that item so check if inventory contains item now i could program this into here hard coded however a better efficient method and a more flexible method would be to just create a public bull in fact it would probably be a better method even more would be to use this uh do this so we're going to have a contains function which returns the slot class which contains that item so that way if we wanted to add it to it we can just increase the quantity of that slot class um if there is no slot class if it returns null then that means the item is not contained and therefore the inventory doesn't contain the item so um this is going to say contains and this will take in an item class item and this is where our code is going to go now i'm going to say return up at the end like that and then here we're going to do a loop we're going to loop through our entire inventory again so i can say we're going to do a simple pattern here uh so slots in fact i might do this for a for each leap for each loop for each slot class in um items i think is that right for each slot class and items yes which slot class slot in items if slots dot get item is equal to item then return slots and that's it that's our contains function so now i can say check if inventory contains item if contains if contains item the inventory contains the item i can say uh well what i should say is actually slot class um slots equals contains item and then if slots is not equal to null that means the inventory contains the item i can say slot dot get quantity and for this i will need to do a setup so i'm going to have a public void set quantity um or rather i'll have a um add quantity function and this will take in an inch quantity uh we'll do underscore quantity again and what this is going to do is literally say quantity plus equals quantity plus equals underscore quantity like that that's our setup or you know function there so that way in the inventory manager i can say slot dot add quantity and i can just add one there we go now in the case where our inventory does not contain that item what i need to do is a little bit more complicated i need to say items dot ads so ads and here i'm going to create a new slot class a new slot class and this is going to now utilize our constructor which we wrote here so now we're going to create a new slot class using this function or constructor rather and it's going to take in an item and then a quantity a quantity since we're only adding one is just only one like that that's our function there beautiful so now our remove function again is a little bit more complicated it's this time it's going to go through each slot and compare items instead of just going using the contains function or the inbuilt remove function um there we go sorry for each slot in items if slot dot slot dot gets item get item yes if slot get item is equal to item then slots items items items dot remove slots um and then i should also break because i don't want to keep doing the loop now this possibly this is not a good practice to do in reality because you don't want to remove something from the list while you're looping through that list not a good idea so what we should do is similar to this create a temporary slot class slot it could be stateless for the moment in fact i'll just say slot to remove now if the i if slots don't get items equal to item then i want to say slots dot slot 2 remove equals slots and now we're setting that slot to that slots and then i can say items dot remove and i can remove the slots to remove that's giving me an error use of undersigned okay that's fine um what i shall do is have a empty constructor here like so so what that means is basically that we can create a slot class from nothing and it's just going to say item equal to null and quantity equal to zero and that means now that we have a constructor for that i can say equals new slot class without giving any parameters because you know we don't need to we're using the other constructor this time and then that way it has a state we can remove it and that will be fine there beautiful okay so i think with all that set up that should actually work perfectly in theory um one thing i do need to do is go back into our inventory manager and you'll see that the ui here is a little bit different we now have element zero element blah blah and so on so i'm gonna delete all of these for now but this is there oh add now one problem is that our slot class since these are private we can't see this uh in the in uh in the inspector so i'm going to set these to serialize field as well like so and that way you'll see that element 0 now has some data we can edit on it which is perfect there we go now has a drop down and we can set the item and the quantity so for example i want to add an sword of quantity one and then an apple let's say apple of quantity five and one thing i just realized i forgot to do and i'll explain in just a sec is that the remove function um one thing that it's not taking into account is if the inventory contains that item right so a remove function should actually do this bit as well just like so okay so it should say slot class slot um i'll just call this tenth for now there you go and it's code looks a bit messy at the moment but let me explain what it does in just a sec i'll add another bracket here just to avoid confusion in the layout okay it's cool so if it contains then temp dot add quantity um i also spoke quantity wrong here let's fix that up real quick there we go i also want another function called remove or i'll say subquantity which of course says quantity minus equals quantity this time and that basically means that if we already contain that item in our inventory instead of deleting the item from pure existence just subtract one from the item otherwise we don't contain an item in our inventory um in which case well here's the thing we want to remove one from its quantity if its quantity is greater than one so if temp dot it gets quantity is greater than one or greater than or equal to one is that right uh if it's greater than one so it's two then otherwise it's one okay yeah so if it's greater than one subtract one from its quantity otherwise that's when we want to do this like that cool so there we go otherwise we don't have that item in our inventory anyway so you know we don't need to do anything one thing i do actually want to do is change this from a void to a bull so that we can keep track of if we successfully removed the item from our inventory or not that way i can also say refresh ui return true and here i can say return false which basically return false will exit this function no need to refresh the ui because nothing changed their inventory anyway we didn't contain the item we're not gonna that saves us from getting a null reference as well so basically just by doing that little change this is basically everything um so now let's look at what our inventory contains at the start it contains one sword five apples and ten coal we're going to add another apple so once we press play we should have six apples and then we're gonna we're gonna remove one sword okay and one thing i will also we'll test this and then i'll also test it by removing um setting the quantity of sword to two and then we'll test it and see if that changes so let's just press play okay so i have six apples ten coal and no swords beautiful that worked cool so now let's say set the quantity of sword to three we're meant to have three swords and now we subtracted a sword so we have two swords left but that works like a charm perfect so here's another thing we want to think about let's say we have we want to add two swords to our inventory right we don't want the swords to stack so each item class should actually have a function or a public bool to say is stackable and this is going to say like if it can be stacked and i'll be obviously added to it otherwise if you can't stack items then you know we don't want to stack them we don't we don't want the number to increase we just want that um we just want that number to stay the same and add it to the end of the list so one thing i'm going to really quickly do is tick this is stackable on the sword beautiful in our inventory manager when we add a uh when we add what is it here we go here we go yeah so when we say a public void add um i'm going to make this a boolean as well and it will return true here to say yes we've successfully added the item um two things we have to really quickly do i have to get the max capacity of the inventory because we don't want to add anything to the inventory if you know we're below max capacity so if the um let's see here so if our inventory contains the item already go to that slot and add one to the quantity of that slot otherwise we don't contain it already check do we have enough space in our inventory so if items.count is less than slots.length right that means we have enough space in our inventory so edit otherwise we don't have enough space in our inventory so returned false we went unsuccessfully um we were unsuccessful to add that item to our inventory okay um another thing we want to do is check if slot is not equal to null and slot dot get item dot is stackable only if it's stackable do we want to add one to the quantity otherwise we just want to add it just like any other item refresh the y return true blah blah blah that's all fine so now if i go to our inventory script um i want to say instead of adding an apple i'm going to add a sword so now we should see two swords appear in our user interface separately they did not appear separately they have stacked which is interesting to see um actually nevermind here we go wait let me make that quantity of one okay so we have one sword um so interestingly we also didn't add it to the end of the list ah i'm being very stupid aren't i our item class say it should say is stackable equals true by default and i realized that i i've made it so that every item is unstackable except the sword so uh is stackable is true apple is stackable is true sword is stackable is false okay let's press play and see if that works okay so now we have sword at the end i'm thinking women's and one at the start as well are we apple sword at the end okay hold up ah because it's also removing a sword let's stop it from removing a sword and just adding a sword we're not going to add anything actually that might give me an error that probably will give me another one it didn't okay cool so it says as you can see our swords are displayed right here now one thing i got feedback from the sandbox series was that we don't want to see the text if the item's not stackable anyway what's the point it doesn't make sense right so what i can just say is if um if slots if if if if items right yeah if items dots if items i dot get item here we go if items i don't get item dot is stackable then display this otherwise we're just going to display the same as if it was not even existing this just like that again and for those wondering what the shortcuts i'm using by the way is if you hold alt and up and down arrow you can switch lines um ctrl d to obviously duplicate lines um if you click on the line and do control d it duplicates it entirely anyway just a few shortcuts there for those wondering because i realized i'm using them a lot for those thinking how am i coding so fast feels bro so let's press play i don't i've got what i just did okay here we go yeah so on the non-stuckbot items the number is now not appearing perfect that is our user interface setup guys now one thing that we still have to add is the ability to drag and drop items that's when it starts to get a little bit more confusing and complicated so i just want to end this video here um if you guys did enjoy this video this tutorial let me know drop a like subscribe all this and that let me know on the discord as well what you guys would want to see in the next tutorials uh that's it i will be doing a part two to this where we do add the ability to drag and drop items around in your inventory that's when the fun stuff starts happening so thanks for watching this one guys i will catch you in the next video take care and peace [Music] you
Info
Channel: ErenCode
Views: 12,784
Rating: undefined out of 5
Keywords: how to create an inventory system in unity, inventory system in unity, unity inventory system, unity inventory system tutorial, unity tutorial, make an inventory system in unity, how to create an inventory system, how to code an inventory system, how to use abstract classes, how to use scriptable objects in unity, creating an RPG inventory system, RPG inventory system in unity, full inventory system tutorial, scriptable objects unity, easy unity tutorial, unity scriptable object
Id: clV9m9QG2ig
Channel Id: undefined
Length: 39min 21sec (2361 seconds)
Published: Fri Nov 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.