GameMaker Studio 2 - Inventory Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

A revisit of a much older piece I did for 1.x that covers the most basic elements I think a game with an inventory of some kind needs:

  • Looking for items
  • Adding items
  • Removing items

with some basic grid drawing and nine slice for good measure.

Personally proud of getting this one down to 8 minutes while also still managing to explain what an array is.

👍︎︎ 17 👤︎︎ u/ShaunJS 📅︎︎ Aug 06 2021 🗫︎ replies

A nice update!

👍︎︎ 3 👤︎︎ u/MoldyMopHead 📅︎︎ Aug 06 2021 🗫︎ replies

Oh wow, this is a hit of nostalgia... I remember watching your original platformer tutorial series as a kid about 8 years ago. You got me into game development and programming in general. It's been a hell of a ride! Thanks man :)

👍︎︎ 4 👤︎︎ u/CommenturTheGreat 📅︎︎ Aug 06 2021 🗫︎ replies

As someone who was going to implement this but had no idea how, thank you shaun <3 youre a big inspiration for a lot of us!

👍︎︎ 3 👤︎︎ u/ROBECHAMP 📅︎︎ Aug 06 2021 🗫︎ replies
Captions
game inventory systems are not generic every game with some kind of inventory will have its own requirements therefore every one of you watching this video will be here for something slightly different rather than try to spend hours showing you one all-encompassing solution i'm going to use this video to show you the most basic foundation i think a game with an inventory will need and then follow it up with expansion videos based on interest so to be clear today we're going to learn how to create an inventory as a simple list of items display the inventory as a grid of any dimensions add to that list of items remove things from that list of items and check for the existence of an item this part here is kind of a bonus on top of the whole basics thing skip it if you don't need it make a new object called o inventory at the create event and we're going to add the following now normally i'd put all my macros in a separate script for organization but for simplicity i've put it here this number decides how many slots are in the inventory this number decides how many slots fit in one row combined together this will give the size of the grid think about it like a world of warcraft inventory a small silk pack has 10 slots and a max of four per row our inventory will work in the same kind of style this line actually creates our inventory and it's as simple as that we make an array 15 slots long and default each entry to minus 1. if you don't know what an array is it's like a single column of a spreadsheet each cell can have something different in it and can be identified by its row number so from now on if we type inventory with square brackets and a number we are getting that entry of the array i do a randomize here which just randomizes our game's random number generation making sure we get different numbers every time we get random numbers this is only important because we're going to add some random items to the inventory later on as a test here i'm just adding three items to the inventory also as a test two of item type zero and one of item type one remember that an empty slot is negative one not zero now we're going to draw the inventory these are the images that we're going to use s inventory is the main box everything else lives in i'm using the new nine slice feature which lets me cut this image into nine slices and then make any rectangle i want by just tiling and stretching them this means we can make a box of any size without distorting the artwork this is s slot just a 32 by 32 box that shows an item slot then we draw over that with s items each frame of this sprite contains our images so you can see item zero and frame zero as this potion and item one is a smaller differently colored potion i told you we're keeping it simple now let's add the draw step to our inventory now there's a hundred ways to draw an inventory this section is kind of optional do it how you like really the important part is just drawing the items from the array which we'll see in a second but first of all we're going to draw our nine slice box like this this could just be one line but if you've got long arguments for a function and it would be a bit hard to read on one line don't be afraid to just separate it out like this okay just put the brackets on the following lines and just you know put each argument on its own line i use magic numbers here if you want you can define all these numbers as variables i'm just trying to keep it simple the idea here is to use draw sprite stretched with our nine slice image to define a rectangle to draw the inventory in we use the x y of the object subtracting a small margin for the border then we add twice that margin to the width and the height the width is simply our row length multiplied by 32 plus four so 36 pixels to add some space between each slot the height is trickier we do div to get the number of times row length goes into our slots total minus 1 plus 1 multiplied by that same amount this means that for every time we cross over our row length we get an extra 36 pixels of height like i said this is all optional this is just for drawing in a grid next up draw the items themselves and this is the important part the for loop here will let us repeat this code a number of times equal to the size of our inventory while giving us a handy variable that goes up by one each time that we can use to determine which slot we want to draw next these two lines give us the x and y position on the grid mod returns the remainder of a division so it will count up towards row length until it hits it then it will return to zero perfect for deciding where to draw each item horizontally for y we get the number of times row length can fit into the current item so if we were on item 12 and row length was 5 this would come back as 2. then we draw the slot at this location and if this section of our inventory is not empty that is it doesn't equal -1 then draw the appropriate frame of s items here as well to draw the actual item run this now and if you've done everything right you'll have this our three potions and a bunch of empty slots change inventory slots and row length a bit and notice the effect it has on the grid now we need to learn how to manipulate this inventory for this we'll create a script called inventory functions and get rid of any default code whenever we do anything with the inventory we're almost always going to look for something first whether it's the first empty slot to add something to or the slot that contains an item we want to remove change or check for we need to loop over the items in order so first we'll create a function called inventory search that looks like this we will require two arguments for this function the root object which is the id of the o inventory object itself and the item type that we're looking for the reason i'm getting root object is to give you a bit of flexibility and allow you to add multiple inventories easily say if you have multiple players or characters or bags or inventories whatever right then just like when we draw the inventory we use a simple for loop to repeatedly check each entry of the inventory array of that inventory object for a specific item or we can even look for -1 to find the first empty slot when we reach a slot that matches we use return to provide wherever we called this search function with the first slot number that contained what we were looking for this ends the for loop right here if the loop completes without returning then we know there is no slot that matches and we can return -1 so we know if this function returns -1 no slot matches what we're looking for with this function done we can now add two more functions to let us add or remove items from the inventory as you can see these two functions are very similar we get the root object and the item type we want to add or remove just the same as before then we call inventory search and pass the result into a variable called underscore slot for remove we look for a specific item then if a slot with that item exists we set that slot to be -1 for add we instead look for the first empty slot in the inventory then if an empty slot exists we put the new item type into that slot in both cases we return true or false for good measure so that we know if we failed to add an item because the inventory was full or failed to remove one because that item didn't exist now i've set up some test button press events to show you how to use these functions this for example will test to see if an item of type 1 the orange potion in this case exists and will output 1 or 0 true or false to the console this button will add either zero or one so a blue or orange potion entirely at random to the next available slot and this button will remove the first orange potion that it finds notice each time i have to provide id this is because i'm calling these from the inventory object itself if you only have one inventory object you can call these from anywhere just using o inventory in place of this or by providing the instance id of whichever inventory object you want to check or manipulate in the case of as i said multiple players etc so now i'll quickly demonstrate as you can see we have our two blue potions on our orange version that's set up by default if i press c right now you can see it's outputting a 1 to the console window because we have an orange potion if i press d it's going to remove the first orange potion it finds so now we just have two blue potions and if i press c again you'll see we start getting zeros because there aren't any orange potions there if i press p we're going to add some potions at random just like fill the inventory up like that um if i press c now you'll see we get one again because there are orange potions if i press d it's going to delete them starting from the top left and going all the way through um then when we get rid of them or press c we have zero again right so hopefully that shows you a bit of how you can manipulate your inventory now there are endless possibilities from here sorting clicking and dragging stacking tool tips etc if you want to have a say in the directions we expand this tutorial consider becoming one of these awesome people who fund my videos via patreon if i've helped you with this video please do remember to like it if i haven't remember to dislike it you know how to use youtube by now hope this was helpful thank you for watching and i'll catch you all next time
Info
Channel: Shaun Spalding
Views: 12,570
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Tutorial, GameMaker Tutorial, GameMaker, Game Development, Indie Games, Tutorial Series, Game Maker Studio, Making Games, How to make games, GameMaker Studio 2, GMS, GMS2
Id: nFHoxYwiqT4
Channel Id: undefined
Length: 8min 5sec (485 seconds)
Published: Fri Aug 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.