INVENTORY & ITEM SYSTEM in Godot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys Tom here and in today's video I'm gonna be teaching you how to create an inventory an item pickup system in the goldeye engine before I start if you're new to this channel and you're looking to begin learning game development I'd love it if you consider hitting that subscribe button I release new content every week to help you improve your game development skills and I'd love to have you become part of this fantastic community with that being said let's get started okay so you can see I've got go door open here and I just have a 3d scene open with a character it's a third person character now it doesn't really matter what base you're starting from here the core concepts will still apply and you will still be able to use this system in any game you want to create so this tutorial is not really focused on creating the third person controller this is more about creating a generic inventory and item system that you can apply to any game that you want to create I'm just going to be using this as a base but hopefully you have something to work on if not you can follow my third-person controller tutorial I'll leave that in a link below and you can also follow my first-person tutorial as well that will work perfectly fine too I'll also leave a link to that in the description as well but for now let's get started with creating this inventory and item system okay so the first thing we're going to do is define our item resource now to do that we're going to come down to our file system let's right click on res and click on add new script and we're just going to call this item GD now where it says inherits from node we're actually not going to inherit from node at all we're going to click on this little hierarchy button here and we're going to search for resource now you'll get a bunch of different things here which are various different resource types that built into Gotha we're actually going to create our own so we're going to inherit from the base resource class let's create that script and let's fire open our script editor let's double click on the item Gd and now we're ready to define what variables we want to store on our item so you can have as many as you want I'm just gonna add a few for default and then we will add a few extras just to show you what sort of things you could do with this so let's delete all of this so we're gonna say twice underscore name item resource okay and that just means that we can reference this in goda as a item resource resource okay so we're going to export a few variables here we're going to export a bar called name and it's gonna be a string we're gonna export a another bar called stackable and this is gonna be a boolean with a default value of false I'm also gonna export a variable called max max stack size and this is gonna be an int and we're gonna default this to one so name is obviously going to be the name of our item stackable is going to determine whether or not this item can be stacked multiple times at a single inventory slot and max stack size is just going to determine how many items can live in a single slot before that item goes into a second slot and you start accumulating a new stack what I will do is I'll export a few more variables here just to show you the kinds of things that you might expect to see on a item resource so let's put a little comment here and let's just say examples of more properties we could store and let's do an enum stars and we're gonna call this we're gonna have a name Dino so let's call it you know item type now again all of these things I'm adding here we're not going to use in today's tutorial this is really just to show you what you could add to this and how it would be reflected in the inspector so let's add a few things to this let's add a generic item type perhaps a consumable quest item and maybe a piece of equipment oops and let's explore a variable for that so let's do export and if we open brackets here we can provide a inspector hint and this will allow the inspector to determine what kind of interface is gonna give the user when they're choosing the variables value so let's say export has an item type and we're just gonna save our type and let's try and add a couple more things here so perhaps you might want to export a variable to Elijah's texture to this item so we can explore a texture and perhaps this is a 3d item maybe you want to explore a mesh as well okay so these are just examples of things you can do of course you could add multiple variables here like consumable health restoration you could add attack-defense any sort of stats that you want this items have you could add as part of these export variables so let's save this and what we're going to do now is we're going to create a set of items so let's come into our res folder again let's create a new folder and we're gonna call this items ok I'm going to right-click on this and we're gonna press new resource now you can see here that it's Auto populated item resourced probably just because I've searched for that before if you have it again just search for item and resource and you'll see it comes up there item resource and that's our item GD script so let's create one of those and let's call this one oak log okay you can call it whatever you want I'm just gonna go with oak log let's save that and then if we open our items folder you'll see if we double click on this oak log and come into the inspector on the right-hand side you'll see here now that inside script variables we have all of the exported variables just like you would in any standard Gd script file except this is now a saved resource on our hard disk it's saved as an oak log TR ES file and that means we can basically load that file in and save it as well at runtime which is really really handy and I'll show you what that means for us a little bit later on so for now I'm just going to come through here so we're gonna say o clock okay so I've given this a name and oak log and it's stackable and we've set its max stack size to 5 again these are the properties that we defined over here on the Left we're not going to use these in today's tutorial but it's just to show you how the inspector would react if you had these sort of properties available on your item so you can see here that when we use a typed named enum that we get the options here so generic consumable quests and equipment and if we add a texture and a mesh you can see that we get a very soon interface to what you would do on a standard node in Godot so for example you can assign a image texture and for the mesh you could assign for example a cube mesh I'm just going to clear those out for now because we're not going to be using those in today's tutorial ok let's save that and now let's also create a second item that isn't stackable so I'm going to right click on this old blob I'm just gonna press duplicate and I'm gonna call this something like magic all ok so let's double click on that one and you can see it's got all of the variables duplicated from the old one so let's give it a name magic orb I don't know how to spell on it like that but there we go stackable let's turn stackable off and because stackables off max stack size doesn't really matter but let's reset it to 1 and just to be clear okay cool so we now have we now have an oak log t-ara es file and a magic orb TR ES file brilliant so now what we're going to do is we're going to create an item database that is going to loop through all of the files within this items folder and add it to a database that we can access within our game at runtime to do this I'm going to create a folder I'm gonna call it manager and I'm gonna right click on that folder and create a new script and I'm going to call that item database let's open up the item database script and let's delete all of this out here I'm just going to save that and I'm going to come up to my project settings I'm gonna come over to the autoload tab here and I'm gonna press this little browse icon and I'm gonna choose my item database and click on add now what that's gonna do is it's gonna auto load this script whenever the game runs and you'll see here it's got this single term enabled checkbox what that's gonna do is it's going to instantiate one single instance of this class and that is going to be accessible from anywhere within our game simply by typing item database that's really really handy when you just want one instance of something to be running at a single time and you want access to it from anywhere inside your game a singleton is really really useful for that and the auto wood section of our project settings allows to do just that so let's close this and now we're going to start creating this item database script so the first thing we're going to do is we're going to create a variable to store our items in so I save our items equals array we're going to have an array of items and now we're going to define our ready function and inside here what we're going to do is we're going to read from this folder here this items folder so we're going to loop through all of the files within that and basically instantiate them and add them to this items array so let's save our directory equals directory new then we're going to say directory open and we're gonna give it the path to this items folder now we can do that by saying res call on forward slash forward slash items okay so that is elicited quite easy to see we've got res colon slash slash items then what we're gonna do is we're gonna say directory list dir underscore begin and that's a function so we're going to call that and that's going to basically allow us to start enumerate in through the files within that directory next we're going to save our file name equals directory dot get next that's going to get the first file within this directory and then we're going to loop through all of these so to do that we're going to say while filename so while filename has a value we're gonna say if not directly dot current is dir and basically what that's saying is if the current thing that we're numerating over is not a directory then we know it's definitely a file in that case we can say items dot append and this is where we want to load that resource so you'll have done in the past you'll have done something like violet equals load and then you'll have passed it the path to the bullet seam for example or a player seam whatever it might be we can simply use that load function directly here so we're same items dot append which adds a new item to the items array and what do we want to add well we want to add the resource at that half so load and then we're gonna say rest call uncle on it /r sorry colon slash slash items splash and then we're going to use a string formatter so we're going to say percent s and we're just going to interpolate the items file name into this string so a percent file name so that's going to go through it's gonna get the file name so it's gonna get for example magical TR es and it's gonna replace that with replace to the percent s with the file name and that will load it from the directory okay next what we're going to do is we are going to get the next file we're gonna say file name equals directory yep next again so we're basically just doing this again to make sure that we are enumerated into the next file and that's just gonna continue looping until file name becomes null until there's normal files in the directory okay next thing we are going to do is we're going to define a function within this item database that allows us to get an item from the database based on its name to do that we're gonna save func get item and we'll give it a parameter of item name and then we're going to loop through all of the items we have in our array and basically loop until we find one that has the name that these are asked for if we found it then we're going to return it if not we're going to return null so let's say for I in items so for every item within the items array we're gonna say if I got name equals item name then I'm simply going to return the item and if all of that fails so we've gone through all the items we've never hit this condition so we haven't found the item then just a bigger return not let's save that and that is it for the item database class so we are looping through this directory here and loading every single one of the resources into an array called items and then we simply have a function that will iterate through this item array and it will return any item that it finds with a matching name okay so the next thing we're going to do is we're going to create an inventory class that we can add our player as a resource and this inventory class is going to be responsible for setting items on the player getting items that the player has being able to add items to that players inventory and it's also going to emit a few signals as well actually it's just going to admit one signal that will let everything know all the other scripts in that game let them know when the players inventory has changed and that's going to be useful for things like updating an inventory screen for example to do that we are going to create a new script so let's add a script underneath our player folder here you might not have a player folder if you don't must absolutely fine add it wherever makes sense for you I'm just going to expand this player folder right click and create a new script I'm going to call it inventory GD and I'm gonna open it up let's delete all of this I'm gonna give it a class name we're gonna say class name inventory and I'm going to define that signal that I mentioned before for when the inventory gets changed so I say signal thank you change and I'm also going to have a private variable export var underscore items equals array and it's also going to have a set and a get function so let's say set get now you might not have seen this before this basically allows us to define in line a setter and getter for this particular variable so let's say set get set on the store items that's the name of our set function comma get items that's the name of our get function now you might be wondering if I'm trying to make this as a private variable with this underscore naming syntax in front just like we see on func underscore ready and why am i exporting it here now the reason for that is because we are going to create this as a resource now actually I should have extended this for resource or less quickly changed out there and we're not extending this from node we're extending this from a resource and we want to be able to save this inventory as a resource to the file system now in order to do that in order for the items to actually get saved we need this export keyword on this items array itself if we don't have that the items that won't get persisted to disk and when we try and load it again we'll have no items in our inventory so that is the reason we're using the export keyword there okay so let's define our set item function that we entered to above so func set item and new underscore items actually I've just realized here that this should say set items and get items okay so set items and then what we're going to do is we're going to say under store items equals new items and also going to omit that inventory change signal someone has set a brand new set of items on this inventory so we want everyone to know of our inventory has changed so let's say emits underscore signal let's give it the name inventory changed and we're just going to pass a reference to this inventory resource itself so that whatever is receiving this signal can directly access the inventory and see exactly what has changed there okay so let's create the function for get items this is not going to take any parameters and we're simply going to return that array directly so return underscore items so now let's define a function that allows us to get a particular item from this inventory so let's say func get item and we'll give it a index property and again all will do here is return our items but this time we will return at the index that's passed and that returned a single item from this items array and finally we are going to add a function to allow someone to add an item to this inventory so we're going to call this function an item and all we're going to take here is an item named item underscore name and a quantity that we want to add so we're going to need to do a few things here in this add item first of all we want to make sure that we're not passing a negative quantity we don't want to be able to use this function to remove items that's probably not the best thing to do we would probably add a separate function to be able to do that so this function is purely going to focus on adding new item so we're going to make sure that the quantity that the user passes in is a pop zero so it's a full number whole number and we're gonna say if quantity is less than or equal to zero then we're just gonna print something that says hey we can tie a negative number of items and then we're simply going to return out of this function that was an error you can add less than zero items so there's nothing else for us to do here so let's return out of the function the next thing we're going to do is we're going to try and find the item that the user is asked for so based on this item name we're going to try and find that in our item database so to do that we're gonna save bar item equals item database and this is where our singleton auto load comes in we can simply access the item database we created here by calling it item database dot let's say get item I'm gonna give it the item name I'm gonna say here let me just scroll down a little bit here get rid of this output for a second and we're gonna do is we're gonna say if not item so if we didn't find an item in our database with that name then we're simply gonna say print could not it's in the wrong place let's say could not find item and again we're just gonna return from this function okay so if all of that has passed we know that the user has passed in a positive quantity so either one or above we know that they have passed him an item that we have actually found in our database so now what we're gonna do is we're gonna start trying to work out whether or not we have a current stack available for this that we could add it to if it's even stackable at all and perhaps we have to create a brand new slot for this item we're going to do all of that logic next to add this item to the players inventory so first of all what I'm going to say is var remaining quantity equals quantity so the users passed in five for example we want to keep a count of how many items we still have left to add to the inventory now the reason we're doing that is because for example the item that the players trying to add it might be stackable in stacks of five you might be trying to add three and the user might already have four so we need to keep track of how many we need to add to each stack and how many new stacks we need to create so I'm just creating this variable here is a temporary variable that will start decrement in as we start adding these items to the inventory now if that doesn't make sense right now don't worry it'll all become clear as we start writing more code the next thing I'm going to do because I'm going to save almanacs stack size equals item dot max stack size so we're going to say the max stack size is the items max stack size if the item is stackable so if item got stackable otherwise pick one so how many how many can be stacked in one single inventory slot well if the item is stackable then it's the items max stack size if the item isn't stackable then it's simply one one that can go in a single slot okay so now we're gonna check whether or not the item is stackable and if it is we're gonna loop through every single item in this inventory and we're gonna try and find stacks that we can add some quantity to in order to meet our demand so we're going to say if item dot stackable so if the item is stackable then we are going to loop through all of the items in this array let's just scroll down a little bit so we're gonna say for I in range item start sighs and that's function and that is going to basically get the current size of our inventory items and it's going to loop through every single one okay so we're gonna say if remaining quantity whoops if remaining quantity equals zero then we can break from this leap okay so if we don't have any more items to add then we don't need to carry on looping through all of these items we know that we can just break straight out of this now I understand that this might start getting a little bit confusing feel free to pause the video or rewind the video and watch this several times so that it starts to make a bit more sense because this is I understand it is quite complicated it took me quite a few times to understand the code that I drove the first time the next thing we're going to do is going to save our adventure item equals items with the index so we're going to get a reference to the current item that this loop is looping through okay so let's say for example we've lived through this three times we're gonna say inventory item eat or equals the third item in our array I'm all going to do is we're going to check if that inventory item is even the same item that we're concerned with adding if it's not again we can just continue straight past this loop it's not the right item we can't add to the stack of it and because it's not the same item so we can just bypass that straightaway to do that we're going to say if inventory dot okay for venturi item item reference dot name and you'll see why I'm calling this item item reference a little bit later on it does not equal item dot name so it's not the same item then we can just call continue continue keyword will basically say I'm not interested in doing anything after this line for this particular iteration of this loop so it just goes straight on to the next one and then what gonna say is if adventury item dot quantity is less than the max stack size so at this point we know that this inventory item is the same item we're trying to add now what we want to see is if the max stack size that this item has if the current quantity on this particular item is less than that then we can add to this stack right so for example if it's match stack size is 10 and this inventory item has 3 then we know that we've got seven slots available on this particular item that we could fill in the stack so we're going to save our original quantity that's the original quantity of this item to the venturi item you've got quantity and then we're gonna say inventory scroll down inventory item quantity equals and we're gonna call the min function but basically gonna say we want the least value out of these two out of a and B and our a is going to be our original quantity the original quantity of this item hat plus our remaining quantity so for example if we had seven and the original quantity is five but the stack size is 10 we don't want it to be 13 okay we want it to be the max stack size and we want some remainder left over to add a new stack so we're gonna say original quantity plus the remaining quantity or the max stack size we want the least value of that okay and then what we're gonna do is we're gonna work out how many remaining items we have left to ask we're gonna say remaining quantity - equals our inventory item quantity minus our original quantity and that is basically going to work out how many items we have left to add to other stacks or to create brand-new stacks with okay and that is it for stackable items okay now that is quite confusing please feel free to pause the video and read through this a few times just to make sure that you understand what's going on there but basically if the item is stackable we need to loop through every single item in the current players inventory and work out if there's any free space in those facts to add some of the quantity that we've asked dad up in this parameter here if we we've got any space to add some of those items to okay so if this item isn't stackable then we need to loop through the remaining quantity and create brand-new item stacks for that and those stacks will obviously be 1 if it is stackable and we have some remaining quantity left over again we want to create new stacks up to the max stack size so how we're going to do that we're going to say while remaining quantity is greater than 0 so while we still have remaining quantity left we're going to create a brand new item to add to the country we're going to say new item equals and we're going to create a dictionary to do that you use these curly braces and now we can provide some properties on this so you'll see here before we use this item underscore reference I'm going to create that here I'm gonna say item reference equals I'm gonna provide a reference to the actual item that we loaded from the item database and what that's gonna do is it's going to allow us to always access this item reference which will have all of the stats and all of the textures measures whatever other properties you saved on the actual resource item itself and we're also going to store the quantity yeah so we're going to say quantity equals and this is going to determine whether or not we can just assign the whole remaining quantity or if we have to create a full stack and keep rolling over until we've added the remaining amount of items to our inventory so we're going to say min remain in quantity and max stack size okay I'm gonna do that as you're gonna say items dot let's just had a few I have lines here to the lot and just so I can scroll down and off for you guys to see so we're gonna say items append our new item and let's reduce our remaining quantity all right remaining quantity by the new items quantity so however many we were able to add let's reduce our remaining quantity by that okay and once that is all done we are going to omit our signal and Ventre changed and we're gonna pass it a reference to ourself and got spelling mistake here it's new underscore item not new items and let's remove these new lines and let's save that inventory script okay so this is the most complicated part of this entire tutorial so again like I've said a few times now please please please pause the video read through the script here or if you are following along in real-time please make sure that you read through everything that you have on your screen it can be quite confusing I totally understand that so hopefully I've explained that enough for you guys to get a grasp of what's going on here if not please feel free to leave a comment down below and I'll try and answer any questions that you might have now that we have our inventory resource we want to add this inventory resource to our player in order to do that I'm going to come up to my player script you might have a player script of your own or you might want to add this somewhere else so find an appropriate place for you to add this in your game I'm going to add it to my player script and underneath where I've assigned all of these variables here again you can ignore all of these these are just part of my third-person tutorial and let's just make a big space here just to clarify that this is completely separate what I'm going to do is I'm going to save our inventory resource equals I'm going to load my inventory resource so that is kind of a rest pull on slash slash and I saved it underneath the player folder so player slash inventory GP okay so that gives me a reference to this inventory resource and then I'm going to create a brand new instance of that so I'm going to save our inventory inventory equals inventory resource inventory resource dot new and that is basically going to say for this inventory variable on our player it is going to be a new instantiated instance of our inventory resource script okay and that's going to give us access to be able to do things like player dot inventory add item but get item all the functions that we created in our inventory script here let's save this and now what we're gonna do is we're gonna create a very easy way for our scripts anything within our game to access our player and also our players inventory to do that I'm going to create something called a game manager now you might have used something like this in the past it's basically a global script again like our item database that we can access from wherever we want in our game so any script and it's just going to allow us to do certain things in this instance it's going to allow us to access the player and its inventory but you might have things that maybe you can add a score to the game or various things like that they're really handy to have something like this like a game manager available in our games I'm going to right-click I'm going to press a new script and we're gonna call it game manager let's open it up let's delete all of this let's give it a class name let's delete all of this and let's go into our project project settings and we're gonna auto load this so let's open it up choose game manager let's add it to the list and we want to make sure that it loads after our item database we want our item database to be the first thing that loads let's close this and now we're gonna have a few things on this first of all I'm gonna have a signal capsules signal player initialized what this is going to do is we're going to fire this from our game manager whenever this game manager gets access to the player within our game and that's going to mean that things like you I can also subscribe to this signal and as soon as there's a reference to the player they can fire things up and go and get the players inventory and do whatever they need to do with that player object in order to get a reference to the player let's do a funk process Delta now there's obviously a myriad of ways you could do this I'm just gonna do it this way basically going to say if not player create a variable for player as well so this is our player above there so if not player so we don't currently have a reference to the player then I'm gonna call a function called initialize layer and then I'm simply going to return from this process function so as to find that function yeah so initialize initialize player and what we're gonna do here is we're basically going to try and get access to the player within the scene tree so let's say player equals get tree dark gallery and then we're gonna say get node and we're gonna pass it in the path to where we would expect our player to be so / route / world / player okay so from here we're going to say if not player so again if we weren't able to find the player at this position then there's nothing else for us to really do here we're going to just return out of this function and wait until the player becomes available if we have got past that part then we've got a reference to our player within the scene tree and we're going to emit a signal basically saying player initialized okay so our game manager now has access to a player variable everything that needs access to that can come over and grab that now so let's also attach the player object itself to that signal just for handy purposes and now this is where we're going to start doing a few things okay so we're going to load the players of current inventory from disk and we're also going to save changes to the players inventory to disk whenever it changes in order to do that we're going to have to connect to a player inventory signal that was fired from within our inventory script up here this signal inventory changed let's go back to our game manager so we're going to say player dot inventory to get access to its inventory resource we're gonna connect to that signal the inventory change signal what's going to be responsible for dealing with that well ourself we're gonna have a function on our self called on player and venturi change and let's just carry on and defined that so let's say funk on fire and venture change and we're going to pass in a inventory parameter just remember with in our inventory itself when we emit that signal we passed a copy of ourself in and again down here you'll see we passed a copy of ourself into this so we're going to take a copy of that inventory and our function and for now what I'm going to do is I'm just going to pass on this function we don't really need to do anything right now and once we start implementing the save and load functionality then we'll implement that so here what we're gonna do is we're going to save our existing inventory equals I'm gonna try and load an existing venture inventory from the users file system to do that we can use the user colon slash slash special path and this will be a path on windows it might be in your updater or your Documents folder so we're gonna say user slash colon slash slash and then we're just going to look for a file in there so I'm going to say inventory dot t res is the typical name for a resource file and we're gonna say if existing inventory so if we found that file then we're gonna say player dot inventory got set items and that's the function that we defined earlier on and we're gonna call existing inventory dot get items now we don't want to overwrite entirely the players current inventory object with the one that we've just loaded the reason we don't want to do that is that would break any connections to the signal itself because this inventory variable would be a completely different object now right so this signal would completely break so instead of just overwrite in it we're going to get all of the items out of the inventory that we loaded and we're going to set them explicitly on the new inventory reference that we have within our player the bag got set up whenever the func ready got called on our player so we're just going to set all of the items on that new reference if we didn't find an existing inventory then you don't really need to do anything but what I'm gonna do is I'm just gonna make a comment here I'm just gonna say let's give the player three pieces of wood okay so let's just give the player an item so let's a player dot inventory dot add item this is the function that we defined earlier and we need to give it an the name of a item so for example overlock you'll see if we expand our items and double-click on o´clock you'll see we give it a name of oak space lock and we're gonna give him I said we're given three pieces of wood okay and let's save that and on our inventory player sorry I'm a player inventory changed what we're actually going to do is we're going to save the current players inventory to this file system location so let's take a copy of that location and to do that we're going to use goats built-in resource saver so resource saver dot save and we're gonna give it the path user inventory zeroes and we're basically just going to pass in the entire inventory resource well that's going to do is every time that signal gets fired so a new item has been added or perhaps an item got removed then this is going to get called and it's basically going to save that entire resource to a file and when you load up the game again it's going to find that file and it's going to set those items on the player so that's going to do full persistent inventory in your game so this is great but now we kind of need to visualize this okay so we need to see if this is even working we've wrote a lot of code so far let's go and have a look to see if this is actually working let's go into our 3d see I'm going to create a new control node here it's not adding a bit of UI so I'm going to create a control node let's just rename it to you I I'm going to create another node this is going to be panel and let's move it down to the bottom that size it up a little bit here and I'm gonna right-click and add another child lower then this is going to be a grid container and let's go to layout let's go to full rect and let's just give it a bit of a margin yeah so let's go 10 10 minus 10 10 oops - 10 that's just gonna give it a bit of padding inside this panel and I'm going to attach a script to this so let's go at app script and you can call it whatever you want I'm just gonna call it an adventure grid let's delete all of this here and what I'm gonna do now is I'm going to save func ready and inside here I'm going to get a reference to our game manager that we just wrote and I'm going to connect to that player initialized Sigma total player initialized who's going to deal with that well we're gonna deal with that ourselves and what's the function name we're gonna say on player initialize okay so that's all we need to do in our ready function let's define our fund on ready on player initialize we're going to reference to the player constant and here we're going to do is we're going to get the players inventory so play about inventory and now we're going to connect to the inventory change signal again who's going to deal with this ourselves what's the function name on player and eventually change just like we saw in our game manager and now let's define that function so on player and bench we change entry think that gives us a reference to the players inventory every time it changes and what are we going to do here well all I'm gonna do is I'm going to loop through any item that's currently in the grid and remove it and then I'm going to add a bunch of text labels into the grid that are basically just gonna have the items name and the quantity of the item currently in the players inventory so let's say for node in get children put for ending our children we're simply going to say end up Q free guys gonna delete it from the hierarchy and now for item in invent we get items for every single item within the inventory we're gonna say bar item label equals labels up new and we're gonna say the items labeled got text equals let's do some string format in here so I say % s for a string and let's say x % d for the number of items we have let's do our % here and give it some square brackets to provide the two variables for this first of all we're gonna say item two item reference remember that gives us a actual reference to the full item resource item deine reference table name and then simply item dot quantity so how many are actually in this inventoryslot finally let's add the text label to this grips oh sorry sorry item label and let's save that okay so if we play the game now all being well I know that was a lot of code anything could have gotten wrong here if we play the game we should see an inventory panel down on the left hand side and remember in our game manager script up here we said that if we can't find a current inventory for the player we're gonna give him three oak logs so we're hoping to see three oak logs in that panel let's press play and we have an error here so we say an F I don't name equals item name okay so the issue there was simply something to do with myself he probably won't experience this at all and there was some import files that I had leftover hanging around from when I previously did this tutorial when I wasn't renewing it and they got hung around and it was basically loading no items from the file system here so I went and cleared of cleared those and it's now all working fine so you probably won't experience that if you do basically open your file browser here and you can delete the import folder and let go to re-employ everything again and you should be good to go okay so let's go back to what we were trying to do if we run the game now we want to see that the player has three oak logs in entry so let's press play fingers crossed it all works fine and great we can see down in the left corner here now that we have three oak logs in our inventory so let's actually where we set the items here so at this point here we know that the inventory is saved to the file system let's try and add a few new items to utilize a player inventory to add item and we're gonna say oplog again this time let's say one so we're expecting now that it's going to come in it's gonna hit this if statement it's going to load the existing items which is three and then it's going to add another one to that which should be four and it's not going to run this again so it's not going to give them another three so we're hoping that they end up with four items let's press play fingers crossed this all works great we have four oak logs in our inventory so we know this is working let's remove this from our code here let's save this and now what we're gonna do is we are going to create just a simple item that the player can run into and pick up just to demonstrate how we would use this in practice within a game in an environment where we have items on the floor collectibles coins for example that the player can run into and we would trigger this player dot inventory add item code you can use this now for anything from basically any way you have a reference to the player you can call inventory add item and give it any name of an item that you create in here so you can duplicate these to your heart's content let's say we had I don't know let's say a pickaxe for example I was duplicated that there it's going to here let's rename it to pickaxe pickaxe it's not stackable we've got one perhaps it's a piece of equipment it might have a mesh attached to it we can basically now just say player dot inventory add item pickaxe oops pickaxe let's give him one we know that this isn't going to fire actually because there isn't eventually saved to the file system so let's run it in here and let's press play just to make sure that that works and you can see now we have a pickaxe if we close this and remove that line of code we're no longer adding the pickaxe anywhere in the code here so let's run this again make sure it's still there we still have that pickaxe so yeah let's go and create a little item that we can collect within the world itself I'm going to come up to the top here I'm going to create a new scene it's gonna be a 3d scene I'm gonna change the type of this 3d scene so let's go to change type I'm gonna change it to an area let's rename it to magical and we're going to create something that allows the player to pick up this magic orb item so again we get this little warning triangle it basically says there's no collision shape here first of all though what I'm going to do is I'm going to add a child which is going to be a mesh instance and let's come over to the mesh here and choose a sphere and I'm going to come to the transform and I'm just going to scale it down to not point one and all the axis just so that it's small let's give it a bit of a material as well so it's come in and I apply a spatial material let's give it a bit of a color I don't know let's go for something like this and now let's come over to our magic orb and let's add a collision shape and it's going to be a sphere now we can keep air that size we don't want it to just be the tiny little sphere here that the player has to run into I think leaving it quite a big size is good and now what we're going to do is we're going to attach a script to this so I say attach script let's just leave it like that magical that's fine let's remove all of this and let's save it as we don't want to save it in the items folder let's just save it just save it to our roof scene for now so a magic orb and now what I'm gonna do is I'm gonna come back to where it says magic orb here I'm gonna come over to node I'm gonna attach to this body entered signal so let's double click on that and let's connect it to our root element so connect and now we're gonna have this on magical body entered so what are we going to do here well we're basically going to have something really really simple that allows the player to pick up this item whenever they run into it we're gonna say if body equals and now we can basically say well is this body the player and we have access to the player on our game manager so we can say if the body equals game manager that player then we know the thing that ran into this was the current player so what we're gonna say though is gonna say queue manager player inventory add item and let's give him he or her the magic or let's give them one and let's just delete this from the scene let's just say fee free save that let's come back into our 3d scene let's go back to the main world has collapsed all of this let's find our magic obscene and let's drag it into the scene there let's not have it as part of the grid container let's just drop it in the world and let's move it up a little bit here let's just double check that that is actually sitting on the floor nicely okay let's put it up a little bit and let's ctrl D to duplicate let's add a couple more of these into our scene just so that we can test this out a little bit and less its playin let's let's see what happens okay so we have four oak logs in our inventory we have one pickaxe and I'm gonna run into this you'll see now we have a magical let's run into again we have another magic orb remember we made this non stackable and let's run and pick all of these up okay so we have four magic orbs non stackable let's close this and reopen the game and now we still have those four magic orbs let's try changing our magic orbs to be stackable so let's go to our items magical tears come over to the inspector let's choose stackable yes and let's create a stack of let's just say three we can stack three magic orbs together let's press play we're gonna have the four they're not stacked because they were saved to the inventory before they became stackable so you could go further and have something to allow you to drag items onto each other and stack them together but in this instance what we want to see is when we collect all four of these we want to see them get added to the existing stack so it's running to this you can see we have magical times - I was running to this one magic orb times 3 now we're hoping that this one rolls into the next stack and it does we picked both of those up and it's filled a new step and that is everything for today's video guys I know this was a long one and I know it was really quite complicated in part and I apologize if I didn't explain it well there were some parts that that you just had to pause the video and read through several times to really get a grasp of it like I say please please please leave comments down below if you are struggling I'm more than happy to answer any questions that you might have thank you so much for sticking with us for this one if you enjoyed this tutorial please make sure to click on that subscribe button if you haven't already and hit that little notification bell to get notified whenever I release new content I'm releasing new content every single week and just a reminder that they have actually changed my release schedule to be a Friday afternoon rather than a Monday morning just due to life circumstances it gives me a little bit more time during the week to work on these tutorials for you guys as always thank you so much for watching and I'll see you guys in the next video [Music] [Music]
Info
Channel: Code with Tom
Views: 53,262
Rating: undefined out of 5
Keywords: inventory godot, inventory system godot, item system godot, item pickup system godot, inventory and items in godot, saving inventory in godot, items in godot, inventory save system, save player inventory godot, inventory ui godot
Id: hYRN0eYttLc
Channel Id: undefined
Length: 50min 30sec (3030 seconds)
Published: Fri Jun 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.