Game Maker Studio: Inventory Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is perfect! I've been trying to understand arrays and lists and maps for a while now.

👍︎︎ 1 👤︎︎ u/[deleted] 📅︎︎ Dec 19 2014 🗫︎ replies
Captions
alright guys welcome back so today we're gonna be taking a look at how to build an inventory system in game maker now I fought about this topic for a long time and I thought about how I might do it from various different genre contexts like how you might do an inventory for an RPG or platformer or for a point-and-click adventure game and various different things but what I've decided to do for this video is try and do a Natori system with essentially no context to show you the bare basics of just setting up a system and setting up an object that will track your inventory and the objects in your inventory as well as how to use code to add items to your inventory and remove items from your inventory and to check if an item is in your inventory and then hopefully with those tools at your disposal you'll be able to kind of adjust this very simple but pretty versatile system to fit into pretty much whatever type of game you're working on now because I'm doing this tutorial independently of any specific game context I'm not going to be showing the absolute specifics of how like you would start on the inventory to be displayed and interacted with and how you could click on a sword on the ground and pick that up and put it in your inventory because all that stuff is gonna be specific to the game you're making like all the stuff like how how you click on one item in your inventory and like move it to another place and stuff or all that kind of stuff it really depends on what it is you're making but what I will show you is the basics of how to interact with this inventory in general so that you'll be able to hopefully adjust and expand the system to fit into whatever specific type of game you're making should be very clear how this works lay it wrong so let's quickly walk through all the resources that we've added to the project so far there isn't really very much I've got two sprites up here one called SPR in-school border it's just a little little white box with transparency in it I'm just using as a very simple thing it's going to show we're just going to draw to represent the slots of our inventory on the screen I'm just doing that really really at a basic level just so that you can see the inventory working more than anything we're not really gonna be covering too much of how to present the inventory level be more up to you guys but uh that's there for that and SPRs got items now this sprite is important and it contains the contains essentially the icons that every item we want to have in the game now I've stored essentially every item in the game in one sprite called SP underscore items as individual frames of that sprite now normally you would use frames of a sprite to store like an animation right but here we're using that same system differently and we're just going to use every single frame of this one sprite to store a different item icon and the only other thing we need in here is um RM underscore one or just have a room nothing in it yet so that's all I have right now and the very first thing I'm going to do is create our inventory object the object that we're going to use to track our inventory and to essentially manage everything to do with our inventory so I had an object on a create object call this obj underscore inventory doesn't need a sprite so it doesn't be visible either but I am going to mark it as persistent so that uh it carries over between rooms so if we change runes change levels like this object is going to stick around and I'm just going to reduce the depth to a generic - value it's not going to max we're not going to have any other objects in the game but um the reason I'm doing that would be so that if in theory we had a player object or something like that or wow it's in theory when we have the rest of the game in the game we would want the inventories probably draw over the top of everything else because it's like a an element of the UI so by sending it or really really load that to that ensures that it's drawing when it draws the inventory on the screen it's going to draw in front of everything else so the first thing we want to do in here it's what we're probably all used to by now is going to advance add event and add the create event our favorite then go to control and drag in our favorite action execute code and here we are back at our lovely code editor the first thing I'm going to do is just make a comment block to remind myself um which item is which number as you remember we have our sprite each different frame represents a different item so we need to know which one of those images represents wine I'm just going to create a comment block by doing slash and then asterisk and then down here asterisk slash and then you'll notice anything I type in the middle of here turns green and I've just created a block comment sewing everything outside of this is regular code and everything in here is just a comment on one get interpret by the game's code so here I'm just going to type in the query items you don't need to do this by the way this is just as a thing to help out 0 was our sword 1 was our red potion 2 is a blue potion and 3 was the yellow key so those are our items now the next thing I'm going to do is just establish a couple of global variables for inventory object and now you may have seen the established global variables before by typing in global and then whatever the name of that variable is um which works what I'm going to do this time for these specific global variables is actually going to use global var instead now this does essentially exactly the same thing only um I don't have to specify a value to initialize it to right away um my create a global variable called show in which is going to be a variable we just used to control whether or not we're currently showing the inventory on the screen I can even call them at night so we know what that does and I'm just going to set it to true now the reason I've used that this time mostly was just to demonstrate the difference in this tutorial is that now as you'll see when I signed it underneath this is a global variable and works just like previous global variables and the it's very easy for objects that other objects and other code to interact with this variable um as opposed to local variables where only the specific object knows that that variable exists but usually when I do something like if I did global show an equals sin church whenever I wanted to refer to that variable again I would have to type global dot Rho and every time whereas having established this as a global values ingush all I have to do is write show and and it knows and it will work in any object or in any script after this has been initialized the game knows that this is a global variable at that point and will be able to reach and access it from anywhere which really the other day just saves you right in global dot over and over anyway all that variable is there for us to just track whether or not we're showing the inventory at the screen at any point in time the next one global var max items is just going to control the number of slots the total is ones that we have for our inventory um depending how your game works I guess I figured my change over time but it's just important for us to track though the maximum number of items that we want to store and I'm just going to keep it really low just to be simple I'm just going to set it to on Eve now that we know how many items we want to store we're going to do is create a essentially a number of variables to store which item is in which not any one point in time now we're not going to do what you might imagine and say like item slop one equals two I mean slop 2 equals item slot 3 equals whatever and so on so forth that would be like really unmanageable like it's really hard to then know to refer to specific items laws you've got to recall the specific name of I am slot and so on what we're going to do is use an array to store what item is in which slot of our inventory an array you can think of it as multiple variables inside one variable or a sequence of variables stored inside essentially one word and how that works is we write the name of our variable and then put a number in square brackets after that variable so I could write inventory square brackets 0 to represent the very first entry of the array equals 1 and then I could also write inventory 1 equals 1 and so on so forth inventory 2 equals 1 and so on deport and now these are all essentially variables in their own right like the like 3 3 different variables the only one because it I can change that or whatever but the main difference here between doing this and as I said doing say item slot 1 equals 2 I'm still on what 2 equals 4 etc the main difference between doing this and doing this what I've got set up here is that when I'm referring through this array later I can control much easily much more easily control which slot I'm referring to so if I have a variable is that slot equals 4 for example I could do an if statement after that I said if inventory and then in square brackets wrote slot equals equals of ten or whatever and then then write some code after that and that would it would take slot which equals four and it would find the fourth entry in array which we we did instead to say that one was for for example I'm check to see if it equals ten which is much much easier like if we're choosing a specific slot than having to say oh if slot equals four and our and the name for our fourth inventory slot was item swap for it wouldn't help us because like we can say it'd be much much harder to find which variable we want to check so this is why we're using an array to store our items in specific slots now in order to set up that array what I could do is I could just say inventory as I did um inventory zero the minus one we're going to use minus one to store the idea of having no item in a specific slot so when there's no want and then a slot the variable is going to be minus one because otherwise we want it to be either zero one two or three one of this it's going to be either sword red Persian blue potion or yellow key if there is an item if there's no I am minus one will be a figure now we could establish our inventory of five items like this one two three four so the length is from zero to force those five items there and we set them all to be blank that would work but that's kind of again makes things much harder to manage burr is like what if we want to change this later to fifty say they're not awesomely then we've got to add like fifty more of this line saying needs to be all minus one that's a lot of hard work so what we're going to do is I'm going to use a for loop to set up our inventory for us and so I'm going to write for I equals zero let me go along I less than manse items semi go on I plus equal one close bracket open a curly brace and close the curly raise now what for loop does is it's going to loop any code that's in here a certain number of times now given that it's setting a arbitrary variable here I to be zero it's going to continue based on I being zero to loop any code inside these square brackets until this condition is no longer true so at the moment the condition is I this arbitrary variable that we set to 0 is less than max items max items being funny right now um so while I is less than 5 which it is it's zero so while I is less than 5 loop this code but every single time it loops this code is going to perform this thing on the end here which is I plus equals 1 so it's going to add 1 to I every single time it loops this code until I is bigger than max items at which point the loop will stop so if you followed all that you should understand that this for loop is going to loop exactly 5 times it's going to loop once with I equals 0 then with I equals 1 then with I equals 2 then with I equals 3 then with I equals 4 and then after it's finished with I equals 4 it'll do I plus I plus equals 1 I will become 5 and I will now be equal to max items therefore I less than max items will no longer be true and the for loop will stop if you want to learn more about for loops like want a more detailed explanation just middle click on 4 or just go to for the full statement and the the index and there's a good explanation of it there so the next piece of code I'm going to put in here is I'm just going to say as we're doing before our inventory open square bracket I equals minus 1 so now instead of having to write inventory 0 equals minus 1 inventory 1 equals 9 1 and so on so forth and just write inventory I inside of this for loop and because we know I is going to increment all the way from zero to four it's going to do inventory zero equals minus one that's going to do inventory one equals minus one inventory two because minus one and so on so forth the only other change I'm going to make to this is I'm going to put global dot in front of this so that we establishes is a global global array therefore it makes it easier for us because we can access it from anywhere in the game and that's it so we've essentially with this code here like really really short amount of code it's like eight lines um we've set up our object and we've got created five blank item slots and we can usually change like if I just set that eight or whatever we would have eight blank line slots but we're just going to stick with fine for now um now that we have those iton slots available uh the next thing I'm going to do is just set it up so our inventory can draw the contents of our inventory to the screen even though we haven't actually added anything to the inventory yet so I'm going to add the drawer event dragon code and first of all and here I'm just going to say if show um this is what allows that variable to work you remember the global variable show in that we set up in the create event that's set to true um it means we can set that to false any time and the game will skip over our draw even entirely and not rendering it at the screen and then we get to turn it back on when we do want to draw the inventory and so on really simple now in here I'm going to do I'm just going to do some very simple kind of drawing stuff to just draw a black rectangle at the top of the screen which is where we're going to draw all are in the Dorian stuff so the first thing I'm going to do is I'm going to type in VAR x1 x2 y1 and y2 now what I'm doing here is I'm going to set up for a coordinates that will eventually combine to give us a rectangle of space so we want the left-hand side of that rectangle on the right-hand side of that rectangle the top and the bottom and we're just going to store them in local variables just to make things easy X 1 to the left side is going to be a wherever our camera is right now Oh view X view right is 0 I could have just set that to 0 to be like the top-left corner of the room but the reason I'm using this view and the score X view 0 instead is so it ranks where the camera happens to be if you're using a camera moving around the room otherwise by default if using if you have in settled views um vo XP will be still the top left corner of your screen it's on X 2 is going to be the right-hand side of the screen so it's going to be the same as X 1 so wherever our camera is now plus however wide our viewport is so view W view for width 0 because it's camera number 0 Y 1 so the top of our rectangle is just going to beware of the camera is a view we're going to score one view 0 y 2 is going to be same as Y 1 to the top of the screen Plus like however tall we want this rectangle to be I'm just going to say 64 community arbitrarily this stuff here isn't that really important the inventory system itself is just because it'll change depending on how you want to draw it this is just a simple way of getting it on the screen it's going to say draw set color to be black just saying our drawing color to be black and then set the transparency of our drawing tool to be a not 0.82 like a little bit spam I'm just going to draw rectangle x1 y1 yes y1 x2 and y2 for the next corners you can see at the bottom those are the ones that's asking for and then outline it wants to know whether or not filled rectangle or just an outline of a rectangle we want a full rectangle so that was not an outline so put zero in there for outline and then set my transparency back to lap so I mean I can show that rectangle just working now if I just pop into the room that I made and just stick inventory in the middle it's going to establish all those blank slots for our inventory and I just going to draw a blank rectangle top of the screen or it's not because the inventory object is marked as not visible I did tell you guys tomorrow's invisible to start I apologize go ahead and set that back to visible it does need to be I close on out run that again and there you go black rectangle top of the screen not much to look at yet but we're getting there so now that we're drawing this black rectangle where we want our inventory to be the next thing I'm going to do is use a for-loop to draw the actual slots of our inventory and draw any items that happen to be in our inventory so I'm going to say for again I equals 0 I less than next items again so we're just going to cycle from 0 up until the maximum number of items can carry I plus equal 1 the exact same cell for this folding as we had before now in here the first thing I'm going to do is for each item I'm going to draw a border the border sprite so draw a sprite sbr underscore border and now for the x-coordinate one I'm going to well actually the sub image is going to be 0 um yeah there's only one frame of that sprite if the X&Y coordinate uh what I'm going to do is I'm going to say uh use first of all x1 I'm just going to use these coordinates up here which determine where the box is Plus 24 arbitrarily just to space it out a little bit and then what I'm going to do is add I x Watty now what I've done there is I take on whichever slot we're drawing so far I'm using that x 40 to add to the x-coordinate of where we're drawing each one of these borders because this this line of code here is going to be repeated five times we're going to draw this border in five times and we're going to draw it in five different places and the only thing that's different every time this loop happens is I increases by one so we can use that multiplied by another value in this case 42 space out our borders along by however much we put in here so these are all going to be about 40 pixels apart from one another the first one is going to be placed at x1 plus 24 plus 0 times 40 so just x1 plus 24 then the next one's going to put x1 plus 24 plus 1 times 40 then plus 2 times 4 8 then plus 3 and so on so forth and you can see how it's going to space them out horizontally over this blank box and then vertically they can all be lined up we're just drawing one row here so I'm going to y2 minus 24 again arbitrary value there's specific to this rectangle but I've got up here it'll be different depending on what you're doing then the next thing I'm going to do is check to see whether or not the I value we're at is a slot in the inventory where we have an item that Lee's drawing because I'm gonna say if global dot inventory and square brackets I so if this specific slot of the inventory does not equal minus 1 because as you know before we set all of those slots to be minus 1 when we made the object which means that those slots are empty so if they don't equal minus 1 it means they have an item in them and we need to draw it and then they draw sprite STR underscore items and this is where the sprite comes really n handy where each frame of the sprite corresponds to a different item of the game because now all we have to do for the sub image or frame that we want to draw the sprite is just refer to ever type of item is in this slot so in sub image I'm just going right global dot inventory and square bracket all right so if it's not minus 1 that means it's the value between like noir and 3 opal II so it's just going to take that value and use that value to decide which frame of SBR underscore items to draw in the specific spot and then as for the spot itself I can go ahead and copy the the X&Y coordinate from where we drew the border because it's going to be exactly the same and then just close the bracket on the end there and that's that and that will draw that will draw any items in our inventory it's really that simple there aren't any items in our inventory yet but I can very quickly add one just by going into our create event here and after we sell all our inventory slots to be blank in fact let me just show you that like what it looks like when they're all blank so you see now is going to that for loop and is drawing 5 empty boxes here now if I go in and say global dot in the pre um let's modify slot 2 which will be the third one technically because it starts at 0 let's take slot number 2 and set that to equal what item should we use we'll use the yellow key which is image number 3 so I know as it says at the top here so global dot inventory 2 equals 3 and that's so that means the third slot of our inventory should have a ello key in now if I run the game you can see there it is little yellow key is appeared in our inventory this is a simple time you could use that to sort of directly manipulate the inventory at any time if you want to manipulate this very specific slot or the inventory to in be a specific thing all you need to do at any point in the game is do global dot inventory whatever slot number you want to modify equals whatever item you want it to be and that fundamentally already gives you the power to manipulate and control is in the drawing but we're going to make things a little bit easier to manage than that long run so what we're going to do now is create the tools necessary to add remove and check for items in this inventory object because this object by itself is essentially done like that's all you need to be to manage and control a very basic inventory it's not honestly that complex so the task we've just created a list of variables that will contain a number that represents what type of item it is and that's that's it you really and we're just using that array of variables to drawer items to the screen or and so on so anyway the next thing we want to do is going to create a series of scripts but can add an item remove an item and check for a night and as I said the first time I'm going to create is going to be SCR underscore item pickup now what this script is going to do is we're going to make it so that we can write anywhere in the game or in any object any code anywhere SCR underscore item pickup and as you can see it's turned yellow because it's recognize the name of the script and then in brackets whatever type of item we want to pick up so if it was the yellow key for example I'm three we can just write that anywhere in code and it will add the yellow key to our inventory that's what we're going to do by creating the script that's the OnStar and pickup so how the scripts is going to work we're just going to create another for loop using exactly the same setup we used before we'll use this one a lot throughout this this tutorial for I equals 0 I less than max items I plus equal 1 so in this for loop I'm going to see if global dot inventory square brackets I so if the specific slop is minus one meaning this inventory slot is empty now there's a comment it's what I is empty right close bracket then global dot inventory I going to take that slot and say to be equal to argument zero now argument zero it represents the number that we put in brackets whenever we call this script when you create a script essentially you're creating a custom command in game maker that you can call from anywhere else so if I go back to our inventory object and if I was to type somewhere in here at the top SCR on the sky and pick up you see it turns yellow and then I can open brackets and send variables across to that script like at send over for and I can put commas in and sound like multiple variables across and then after doing that when the script is running it refers to those variables is arguments 0 through like about 9 so if I wrote argument 1 it would represent the second number a fight wrote one bracket comma 5 if I were argument 1 there it would represent the number the second number I wrote when calling the script but we just want the first one there's only one argument to the script just argument 0 so we're going to set that specific slot of the inventory to equal whatever we sent the script then after doing that script has succeeded at what it needs to do is found the first available slot because it's cycling from 0 all the way to 5 or all the way to 4 sorry so the very first free slot that it comes across is going to set that um that slot to be that item then we don't want it to carry on the loop otherwise it will fill up the whole inventory of the signs and they say rip turn one I'll buy return towels when you might return in a script it ends to script at at that point so if this happens to be true that the slot is three we add the item to the slot and then we finish with the script now by putting one in here uh it means the script sends back a one to wherever it came from so if I were to do um it means we can set a variable to equal the script and then the script will send that one back to that variable so we could say a test equal SCR underscore item pick up a one for example o2 or you know any any of the the item type so we have in SP underscore ends and then as assuming that works and it finds an open slot puts the item in it the test is going to be set to equal one because it's going to the script is going to return one and put that into who this variable then if we cycle through all of the slots in our inventory and we didn't find any empty slots so our inventory is full it means we're going to cycle all the way through the for loop and come after the for loop and say return zero which will end the script after having not put any items in any slots and we're going to send a zero back now that just the benefit of doing this returning one returning zero means that when we add an item we can say like add potion equals SCR and Scott line pick up a two and then say our if add potion equals zero show a message saying the inventory is full because if a potion is zero it means that tried to run the script I didn't find any available inventory slots and therefore return to zero if it equals one it means that it did find an inventory slot and it did fill that slot with inventory so then you can use that to do other things in your game and say like all the inventories for we couldn't hide the items and so on so forth and allow you to do things like that and that's that for that script so that's ser underscoring pickup now we can test that out real quickly so at the moment all our inventory items are blank I got rid of the stuff we did earlier with the key yep they're all blank but if in the create event I'll object inventory now at the bomb I stay SC arms for iron pickup let's add the sword so I am 0 and if I copy and paste that like 2 times so like do that three times we should end up with three swords in our inventory yeah there they are three swords all lined up in our inventory and now you can use that script anywhere in your game any game object or anything like that um so that like or you could have a sword on the floor for example and like in the click event for that sword you could just write ser and it's got item pick up 0 and then instance destroy or whatever and then that will add the sword to your inventory simple as that and and so on that's why we made the script so that you can start actually do things with this inventory the next script I'm going to do is very very similar STR underscore item drop uh this script is going to say it's going to start off with the exact same code as before in fact I'm just going to write copy and paste it from this so this is the exact same code as the item pick up script but instead of checking to see if each slot is empty and then adding the adding the argument to that a slot I'm going to check to see if the slot of the inventory contains the item that we're looking for which is argument 0 so we're going to look for a specific type of item in the very first one we come across we're going to remove it from the inventory by setting that slot to be minus 1 again and then if it successfully does that return 1 if it wasn't able to find the I in the inventory return 0 really is as simple as that it's not a slice empty anymore if slot I contains 1 0 so that's that one arm so now we can see that one working if I go back into object inventory if I add that script again till I see our item pick up um we'll do the blue potion I'm to will do that like three times again so I'll just run that so you can see three blue potions in the inventory and then I'm going to say ser I can drop to second I'm is going to find the very first slot with a blue potion app which will be slot number 1 or 0 rather and it will remove it from the image reset it back to minus 1 so yeah we have like an empty space here and then to blue potions simple as that removing items from inventory and then you can use that at any point just as I explained with iron pickup now the last one we're going to do is again almost the same SCR underscore item Shack now the script is going to check to see if there is an item of the specific type in the inventory so again same thanks again I'm just going to copy it again because it's almost identical I'm going to go back tonight and pick up and copy this paste it into this now we're going to check every slot the inventory just as before and say if that specific slot equals the item we're we're looking for just as we did with the item drop so I can just say this again contains argument 0 we're really this coat is the identical to item drop both only instead of removing the item from the inventory we're just going to do nothing because we don't want actually change the image or in any way we're just looking to see if this line in this there and if we do find one of these items then we can return one we found it returned one to say that we found that and it will exit the script at that point otherwise it'll loop through each one the if statement will never be true and it'll get all the way out and we'll return 0 because it couldn't find one and then all you would have to do like if you were looking for that item it's a potion rather sword check our equals Ser underscore item check 0 because sword a sword equals 0 and then if sword check equals 1 that means we have sword so you can check your inventory to see if you have a sword by just doing sword check equals Ser underscore item check 0 and if that comes back 1 you do have a sword if it comes back 0 it means you don't have a sword in the inventory simple as that so yeah with those three basic scripts and the super simple object and now I can um so yeah sorry 3 basic script super simple object you now basically have everything you need to create a simple versatile inventory system you can hopefully modify and expand upon to fit more or less any kind of game with inventory the Uni afore I'll cover some more stuff for the inventory um a little bit later involving like how to stack items and how to make items contain certain properties um like how to store an items name description sprite and so on rather than just just storing like a number to represent the item but there's a lot you can do that that really I'll cover that a little bit later um but for now there should be more than enough to get you going with basic inventory systems one last thing just to show off the I have the system kind of working in real time as opposed to just doing it on the create event is I'm just going to add the keyboard like the P the P key so one of the P key is pressed in fact I won't keep press with this letter P so whenever I press the P key I'm going to add a potion to the inventory just to demonstrate how this would work now what I could do is just do I see our item pick up two and that will work so now if I run the game now press P I can fill up the inventory oceans super simple but I want to add something just to check to see whether or not um the inventory is full when I try to add the potion so that I might you know know not to destroy the potion I clicked on on the floor or etc so I'm gonna say var and potions temporary variable add potion equals SCR underscore iron pick up - so I'm setting this variable to equal the result of this script which is going to be 1 or 0 depending on if it works or not if add ocean equals zero then we know that the inventory is full and so whatever you would want to do in here maybe you'd want to show a message on the screen or you just avoid destroy maybe you want to check to see if it did work and if it did work Oh destroy potion item that you just clicked on on the floor so on so forth but yeah that is that at the end of the day that to two simple events and one object and three scripts to help you manage it that is the basics of how you do an inventory system and also just how you use arrays at a basic level hope you guys enjoyed that more coming on this little stuff soon and I'll catch you guys next time thanks for watching see you next time guys you
Info
Channel: Shaun Spalding
Views: 181,868
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Game Development, Indie Games, Indie, Games, Tutorial, Tutorial Series, Game Maker Studio, Steam, iOS, Windows, Making Games, How to make games, Basics, Inventory, Inventory System, Code, Game Maker Language (Programming Language), GML
Id: LwBC6kyTa0M
Channel Id: undefined
Length: 41min 41sec (2501 seconds)
Published: Wed Dec 03 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.