How to Make an Inventory System in Godot - Looting Items (#3)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey this is arkeev i'm back with another tutorial to show you how to pick up items and add it to your nice little inventory system that you got this is part three so if you haven't watched the first two parts i suggest you go and do that but if you have let's get started to start off let's add a world scene so do do let's rename this to world and let's add a child node and we'll create a stack body 2d and this can be our ground and now let's add another node a texture act for the image of the ground and have the image already loaded up here so click and drag into it to the texture property and let me zoom out and now we have a ground but we also have to add a collision shape to it so the player knows where to stop instead of continuously falling down collision shape 2d and click on collision shape 2d and then let's add a shape to it a new rectangle shape 2d will work let's drag it and let's just cover the surface of the ground okay that'll work fine let's save it as world scene now i'll just make a simple player scene click click kinematic body player and bam this is the player scene and it's pretty simple just walking left and right this is just so we have a player to pick up an item so let's go back to the world scene and let's add the player scene to it let's scroll out and it's fairly simple it doesn't even have gravity so i'm just going to snap it onto the ground like that and let's try playing the world scene so now we have a player that can walk left and right now we have to add picking up items to our inventory so let's close this alright so the first order of business is to be able to open up our inventory so let's first go to project settings and let's assign a button so go to input map and let's create an action called inventory click add let's scroll down and let's assign it any key let's say e so now when we press e we should toggle the inventory screen then let's add a canvas layer so canvas layer is something that is good for user interface because it doesn't move along with the camera so it's great for the inventory let's rename this to be user interface and then add the inventory scene as a child that we created before so now we have it but now we have to add some functionality so we're able to toggle the visibility of the inventory so let's add a script to the user interface now all we got to do is just check for input so let's create the input function and if event is action pressed inventory the button that we assigned let's toggle the visibility of the inventory node so we can access the inventory node by the dollar sign and the name and we want to change the visible property and to toggle we can just do the exclamation mark which is just the not symbol and this will make the value of inventory dot visible opposite now let's click play run the world c and let's press e and now we're able to toggle the visibility of the inventory with just the button press great but one thing is we don't want it to start off being visible we wanted to start it off being invisible so we'll just click the visibility now we want to create and lootable item and we can start off by just creating a scene and i'll create a kinematic body today because i envisioned this item drop to have some movement and let's rename this to be item drop we'll add a sprite to it and i'll just make it a slime potion and since it's a kinematic body we would have to add a collision shape to it as well and the shape doesn't have to be exact but i'll give it a general shape just so when the player detects it he'll pick it up so it doesn't have to cover exactly the body of the sprite so that'll work just fine now let's save the scene as item drop and let's also give it an animation player and i'm just going to use this animation player to animate a floating effect to the sprite so i'll just call blow and i'll make it one second long and make it looping and what i want to animate is the sprites position so to do that we'll go to transform and i'll key the position property and in the middle i'll duplicate that and then change the y value of this key to be negative one and you can see it slightly go up and we want it to slightly go down so if i play this now it looks like it's floating which is pretty neat and i'll make it auto play unload let's add a script to the item drop scene and this is the code it's fairly simple and all it does is just apply gravity to the item drop scene and also i set the item name so we can reference it later on now let's go back to the world scene and let's try to add some item drops so item drop scene that's whoops let me click that and then drag it down here actually i'll drag it down there so i can show you some gravity and let's click play so if you saw it dropped all the way down and now we have an item drop in the world but if you notice one thing is that the player collides into it and we don't want that we want the item drop to collide with the world items but not the player itself so to do that are to change that behavior we would have to go and mess with collision layers so let's go to project and project settings go to general scroll down to i correct i think it's physics and 2d nope that's not it it's layer names layer names and 2d physics and here we can define different layers so for instance we can have a layer called player and world and another layer could be item drop so let's click close and now we're able to configure certain layers to look at certain layers so that way not all the layers are looking at each other for collision purposes let's expand the collision section and for the layer property let's change this to item drop because layer refers to what layer is this current node on and item drop is clearly on the item drop layer and mask refers to what layer does this node look towards and we don't want it to look towards player but instead the world layer and let's go to the ground node and let's open up the collision section and then change the layer to be world and for mask we'll make the world look towards all of the layers and the last one is the player node and let's click on the player node open up the collision section and it's on the player layer and it looks towards world and not player now we want the player to be able to detect item drops to do that let's add a note to the player we'll make a area2d let's rename this to be pickup zone and we'll put this to be on no layer but it looks towards the item drop layer because it's trying to search for items and we'll add a collision shape to this node let me just tug a little visibility of the other collision shape and let's add a rectangle this essentially works as the detection range for items so if an item drop comes into this range we'll be able to pick it up okay that works for now let's add a script to the pickup zone and then let's add some signals whenever a body enters the pickup zone and since we set the layer it looks towards to only be item drops then the body that's entered can only be item drops so let's click on pick up zone and then node tab and then we'll double click on body entered and we'll connect it to the pickup zone scene and we'll do the same thing with body exited and if you go to the script you'll see that there's two new functions and this these two functions are called whenever an item drop enters the range of the pickup zone scene let's keep track of all the items that enter the pickup zone and we'll do that by creating a dictionary called items in range and within the body entered signal let's add to the items in range and the key can be the body type itself and whenever the body exits we should erase it from the dictionary so we first check to see if it has the body and if it does we'll erase it let's go to the player script and let's add an input function to pick up items so in this input function we check to see if an action pressed pickup occurs so we didn't add that yet so let's go to project and project settings and then to input map and we'll create an action called pickup and i'll just assign it to be zed so when z is pressed we first see if the pickup zone has any items in range and if it does then we try to pick this item up by calling the pick up item function and this function exists within the pickup item so we have to go to the item drop script and add another function the function will simply be this but we have to add two new variables one is the player which keeps track of the player obviously and another one is being picked up which is a flag to let the item scene item drop c know what state it's in so we'll initialize it to be false so if it's false if being picked up is false we just want to apply gravity to it else we want to start picking it up and to do that we add this code and what this code does is it first gets the direction to the player and applies velocity for the item drop to go towards the player and once the item drop is close enough to the player we destroy it so it disappears let's go over to the world scene and let's try to play it so i can walk past the item drop and if i'm in range and i click zed i'm able to pick it up you saw it swish over to my player but the next problem is it doesn't really add to our inventory we don't keep track of it so let's do that to keep track of the inventory let's create an autoload script so we have it always even if we change things to do that let's first create a script right click new script and we'll call it player inventory and now let's make this an autoload script to do that we go to project project settings and then to the auto load tab and then we'll find that script that we just created player inventory and we click add and now it's in autoload script click close let's add this code to the inventory script and the inventory variable here is of type dictionary and the key is the slot index and the value is an array of size 2 and it contains the item name and the quantity of the item so we'll store information about our items in the inventory in this format and that way we can load it up whenever we open up our game or if we just close our inventory and open it up let's go to the slot.gd script and remember in the ready function we had code to randomly add an item to the slot let's comment that out and let's add a new function called initialize item which takes an item name and an item quantity and sets the slot to be that item and in this way during runtime we can set a slot to be a specific item then let's go to the item.gd script and let's add another function called set item which takes in a name and a quantity and sets this item to be that item that we want it to be so we update the texture and also make sure that the label is visible or invisible depending on the stack size let's go back to the player inventory script and let's add a function called add item and all this does is it updates the inventory variable that we have in the script and we call this add item function from the item drop dot gd script and we call it right when we pick up the item and before it's destroyed so we go player inventory dot add item and we pass in the item name and for now we'll set the quantity to be one last but not least we want to update visually the inventory slots and what's in the slots and to do that let's go to the user interface script and every time we toggle the inventory button let's update the inventory so we go we'll access the inventory node and then we'll call a function that we have yet to create initialize inventory cool and then let's go to the inventory script and let's create that function so initialize inventory all it does is it iterates through all the slots and then it searches the inventory within the player inventory script and adds that item and we can call it in the ready function as well in case we do start up with any items and that's all there is to it and let's go to the world scene and then let's try this out let's walk past let's press zed press e and voila we picked up that one slime potion and if we close it we open it up again it's still there and the sword's there because we added that in in the code so if i go back to the script go to player inventory we have iron sword right here right we can also easily manipulate this by adding in another iron sword and say we want it to be in the next slot and we'll give it one item quantity and let's go to the world scene and let's add more item drops press press ctrl d to duplicate and i'll put it just dispersed and now let's click play this button actually let's press e and then we have the two swords that we specified let's pick up this item pick up that press e again and we picked up four slime potions this is not complete because whenever we move around an item it doesn't update the inventory variable that we have in the script so probably in the next video we'll go over updating the inventory variable every time we move an item within the inventory panel because right now it just messes up and that brings us to the end of this tutorial if you found it helpful consider subscribing because it makes me happy so please do and take care until next time
Info
Channel: Arkeve
Views: 12,822
Rating: undefined out of 5
Keywords: godot, indiedev, inventory, gamedev
Id: ssYqIQQPt7A
Channel Id: undefined
Length: 19min 14sec (1154 seconds)
Published: Fri Sep 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.