Full Item and Item Inventory System in GMS2!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to my item inventory video this is a super flexible and really dynamic system that will work for literally any kind of game and um it even works for stuff that aren't traditional items this covers how to create new items that each contain a bunch of different data that you can use for different stuff like their name description any sprites that need to go with them whether or not they're a key item like you can drop them even contains their effects super super easily then i cover how to add those items to an inventory remove the items and drop them swap them out with other items making sure you can't pick up items whenever your inventory is maxed out and using super different types of items i cover like a healing item two different ways to do a type of key item a weapon item like a bomb and once you get through this whole video you'll be able to do pretty much anything any kind of item this can be tacked on super super easily to my rpg series and if you don't know much about for loops and arrays then uh you can definitely check out the menu system video that i've done that would go really really well with this one if you're not super confident with those concepts you can see the video is a little bit long it's just because i i didn't really feel like breaking this one up into multi parts but mostly it's just because i like to explain things and make sure that hopefully you're really understanding a new type of concept so anyways either way at the bottom of the video on the little timeline down there i'm going to kind of insert where the chapters would be and that way you'll know where we are in the video and if you want to come back to it you'll know exactly where you are and before i go any further i'd just like to say that i have a game coming out very very soon called starcross starrcade special you can wishlist it right now on steam if this item system helps you out then uh that would help me out a bunch and there's a good chance that by the time you even watch this video it will actually just be released so if you wanted to buy it that could really help me out that and the uh the internet stuff you know the the liking the subscribing the twitter following you know if you wanted to do that but anyways i'll shut up and i'll just get to it good luck before i get started i'm just going to show you a short little demo of what everything actually does right now and this will be like a good look into how easy it is to get this system to do like a lot of different stuff and this honestly is not even like the tip of the iceberg you can do a lot with this so first we have the items in the overworld that we can actually pick up and so i just picked up the red key this is obviously a super super temporary looking inventory the visual stuff requires a little bit more flair and tweaking and work and it's gonna be different for all of your different projects so i'm just doing it this way for the video and i'm using like mouse clicks and stuff like that for the video but i'll mention some ways that you can do it with like or controllers or buttons or cursors and um at the end of the video i'll give you some more ideas and stuff like that so anyways we picked up an item we picked up a red key if we hover over it it shows that it's selected and it shows a short little description that we wrote and it says it's the single use so uh whenever we left click items we're gonna use them uh it's to open doors so this type item if i were to try and use it right now it doesn't do anything until i actually get to the red doors and then i use it and it opens the doors get a blue key and a blue key is slightly different because it doesn't go away whenever we open our blue doors so just open these and we'll open these we still have our blue key so then we can pick up another red key to open that door over there and we have some food items and a bomb and we got a lot of stuff here so another thing that i've done is added a maximum to the inventory so i've added i've made that six be the maximum that we can have here and so if we have the maximum we won't pick up more items then we can scroll down and see what each one does so we have like a burger that will increase the player's hp which i have the top right of the screen so if i use it the burger goes away hp goes up from 10 to 20. we have another food item which is like a two-pack item so if we click it once it will heal us a little bit and then it will become half of the pack the description changes and everything basically this is just swapping to a completely different item so one thing you'll be able to do is use items and then swap them out with other ones we'll use that again now that we don't have six anymore we can pick these up we got a bomb which would be like an action item so it would like make a bomb it doesn't actually do anything i'm just it looks like it's gonna blow up it's not i was too lazy to do that for these demonstrations but you get the point and then the last thing is keys are pretty important items and in a lot of games in rpgs you have items that you don't want the player to be able to drop voluntarily maybe you do maybe your game's a little bit harsher than others but i have it set to where non-important items can get dropped so if i right-click a burger we drop it it's out of the inventory same with the bomb same with the other burger but if we try that with the red and blue keys we can't drop those so we've gotten all of our spoils and all that and now you can just leave the level you've earned yourself one entire blue key and you can do tons of stuff with the system and it can be super applicable to other things like having separate inventories so like an inventory that you carry and one that you maybe store stuff in this would be super super easy to apply a system like that to this okay so now we can actually get started i'm going to open up a new project here okay so i'm here in a new project the only thing that i've added in is just a player and a wall for them to run into and then i've added that blue door and red door back in they're literally just walls they have no code they're just children of this thing and once i bring them back up i'll show everything so yeah that's about it i assume if you're watching an inventory video then that means you probably already have some kind of game at least functioning and if not i have a whole little series about doing that kind of thing that i probably mentioned in the intro so now that we're all square the only other thing that i have is a room where i just drop some walls in to look like that little demo that i showed you and i put my player in that's it no inventory code yet at all so this whole folder is just my stuff you can just pretty much ignore it so first thing first let's make our object for the item inventory and we're going to call it object item manager because this will be managing our inventory and also where we're creating our items and stuff like that this is going to be an object that you're going to want to reference throughout the rest of your game it's going to be very important object so we're going to make it persistent and something i kind of mentioned in another video was the concept of maybe creating like some kind of initialization room for a game to put objects like this at the very beginning so that way you don't accidentally make duplicates which could cause a lot of problems so let's just make a new room create room and i'll say room init for initialization i'm gonna put this in my room folder first then i'm gonna go ahead and put it from the quick access to the room order up here i'm gonna go ahead and put this on top so it has the little home icon and that means that this is the room that the game will start with which is what we want and first things first you might want to just go ahead and set all of your camera settings in this room because defaulting game maker the first room that runs sort of dictates like your camera size and your viewports and all that stuff again you probably already know how to do that so i'm just gonna do that really quick for mine and once you have that information in we want to go into the creation code of our room down here on the bottom left in the properties panel open up your creation code and just call the function room go to next this will immediately just take us to whichever room is next in our room order so again it's just like we're adding our own little single frame initializer at the very beginning of the game so we can put any of those uh persistent instances like our item manager in this room just anywhere it doesn't matter doesn't matter how big the room is because now the game runs for a single frame it creates this object we move on to our actual first level or first room or whatever and now we have this in the game like i said the main reason for this is so we don't accidentally create more than one and now that we've done that a little bit of setup we can actually start doing some stuff in it so let's add a create event first thing i'm going to do personally is just set the depth to a really low number because i am going to be drawing with this object again this will ultimately vary on what your project looks like okay so now we're going to start creating our items that's the first thing we want to do so let's say create the items and we're going to be using something called structs now a good way to think about structs if you don't know what they are is structs are a lot like objects they're like objects in the way that structs contain their own variables and those variables can be manipulated and changed with outside sources and within itself just exactly the same way that objects can what's really useful about them is they're not nearly as complex or taxing as objects because objects have a lot of different things attached to them objects have different events they have different built-in data already that they're always keeping that they're always using in some way objects can use draw events to draw things but fundamentally the way they work is they work on setting and manipulating their own variables and that's the same way structs work so i'll show you exactly what i mean and as we go on you'll understand why they're a little bit more appropriate in this situation and how useful they can be so let's create our first item and i'm going to create that burger item so we're going to create and store a struct and we're going to call it burger and here's how you create a struct you would say burger equals and then you use curly brackets and now within here we can start setting our own variables for burger so i'm just going to indent and space all this stuff out just a little bit easier to use so probably the first two things that we want to look at for an item would be we want our item to have a name we probably want it to have like a description and then we probably want to get a sprite for it which we'll have to make a sprite so for now let's just do our name and description and here's how you set that stuff in a struct so let's call this name i'm gonna say name colon and then whatever value we want and we're going to want a string so we'll call it burger and it doesn't have to be burger of course it can be like i don't know mega burger i don't know could be anything and you can change it later if you want just keep calling it burger so now we've already set one variable for burger so name is not a variable for object item manager it is specifically for the struct of burger which we have stored in object item manager so yeah just remember everything in here but belongs to burger a word that is both starting to look and sound very strange to me at this point okay so let's let's move on now we want to set our description and to separate new variables in scripts you use a comma so in this instance i'm going to comma space create a new variable for burger called description and i'm going to do the same thing this is also going to be a string because it has to be text so i'm going to say it's a burger that's the description and then maybe i'll add recovers hp all right now i'm going to add my little sprite for my burger because i do want to go ahead and add the sprite in here okay so here i have it it's a very simple 8x8 sprite of a very low fidelity burger like i said i've kept it eight by eight and i'm keeping the origin at the top left because i'm going to use these to draw as like icons which you would probably be doing for a lot of inventory systems if you want to have visuals you might want to do like an earthbound or like an undertale type thing or i mean lots of rpgs um don't actually use icons and stuff like that which is totally fine again this system doesn't really hinge on any of that stuff i'm just gonna kind of show you a bunch of different things you can do with it so i have burger and i specifically have made burger in a way where i can draw it in like squares so i'm gonna make all the item sprites eight by eight you can do any size you want and i'm gonna put all of them top left you can do whatever you want just as long as you're making them all the same and so now i have a sprite for burger need to make a new variable so comma now you could either call this sprite or um like you know sprite burger like this or depending on how many parameters you have or if it might help you to think of it this way a little bit more um you could even just call this icon so that way you know it's specifically the sprite that you want to use for displaying an icon because we're going to be using some of this data in our item structs to actually display sprites for overworld objects for picking them up too so if your game was a little bit more complex higher fidelity than mine is right now then it could be useful for you to have an icon for menu purposes and then have a sprite for like overworld purposes or you could call it sprite and overworld sprite or in-game sprite whatever tons of stuff you can do you get the point by now so we now have one entirely functioning item at least theoretically so uh let's go to our draw event and i'm just gonna do a little experiment just to see if everything's going well and to show you how we're going to start referencing this stuff so i'm going to say draw sprite and i want to draw my burger sprite so i'm going to say burger dot sprite like i said it's just like referencing a variable in an object because this variable is within our burger so that's how you do it if you're testing this right now if you're testing it in a different object like not our item manager currently you would also need to be able to get our struct of burger which is located in here so you could do this object item manager dot burger so then we're getting that struct and then from that struct we're getting another variable within it you're not going to need to do this for long and especially if you're testing it in this you don't need to do it right now so just keep that in mind if you are testing it somewhere else should probably stick closer to the video if you're maybe testing it somewhere else but either way i trust you so just know that now so the way the icons are working i just have them as a single frame so i'm just going to say sub image 0 and let's just say at 16 16. so top left of the screen [Music] so i'm gonna go ahead and test that right now okay so it's working for me if it's not for you then go ahead and do a quick little run back through the video skipping through a little bit the places where i could probably be going wrong right now would be you didn't make it persistent because remember we put it in our room initialization so remember it has to be persistent you have to have actually put it in your initialization room and you need to make sure your initialization room is the first room that runs in the game then if that's not it just check this stuff but everything's working for me right now you can see we got it right up there so now let's test our name and description as well just really quick i'm just going to say draw text this is probably going to look really ugly by the way because i haven't set any fonts or anything like that so draw text we'll say uh same x position we'll go to 32 so it's a little bit lower so it shows up right under and then we'll say burger name oh right and uh with structs there are like certain variables and constants and stuff like that that are are kind of already built in or they're things that you can't use name is not one of them um i'm gonna be completely honest with you i don't know why it highlights red like a constant or something like that the first time i noticed it i thought maybe name was a variable that i shouldn't be using for structs but it has worked with the system for me and it hasn't caused any problems so uh maybe it's just something that's already built in that you can name yourself and it just doesn't really make much of a difference whether you try and set it or it's already in there uh if if the red makes you uncomfortable you could maybe just call it item name or something like that i don't know but anyways it works like everything else so i'm gonna test that okay so it works and it is quite ugly because it's the default font and i'm drawing it in the regular draw event instead of the uh the gui layer and i'm gonna level with you really quickly because i make pixel art games and i really like pixel accurate pixel perfect games i very rarely use the gui layer i normally will just use the draw event of different objects at different depths or different layers or something like that so just for the sake of looking nice i'm going to add in my own font which i covered in the menu system video and reference in the text box system video like i said in the beginning if you haven't watched especially if you haven't watched the text box system video and you're a little inexperienced then that will help you out immensely later on in this video very simple concepts and stuff but regardless of how simple a concept is you have to learn it before you can apply a simple concept and i don't want the video to confuse you or give you misconceptions about something because maybe you're getting some wires crossed or something like that so anyways i'm gonna make my own font you can download your own font and uh set it any way you like you can right click and add a font from your system i think it's any yeah i got 8-bit operator here so yeah so any font inside of your own system and then whether you're using the gui layer or the regular draw events just play around with the size whatever uh i do like making my own though and i advocate for people making their own especially with pixel art okay so i've created my own personal font and i've stored it in global.font if you have a font asset you could just type that in here instead of this whole function and i used this sprite here which is just a font that i made a very simple pixel art font again i covered it in the text box video and i'm just doing everything exactly the same as i did there so i've created and stored it i've added a clean up event which is the event that triggers when an object is destroyed or a room transitions and it's no longer in the room or at the end of the game and i'm just deleting it there because fonts take up a little bit of memory and you want to free that memory back so your computer doesn't slow down and ultimately end up crashing the game and other stuff so yeah exactly like i did it in there and now i've just used draw set font before i started drawing the name so now whenever i run the game it looks a little bit nicer so didn't mean to go off on a tangent it just ultimately makes my life a little bit easier and it makes it look a little bit nicer for you so now you get the point though of how the structs work and how creating items work so before we get any more complicated with our single item or we start making more let's do something that i personally think just makes it a little bit easier to reference these and i think a good way to do that is to create a bigger outer struct for these items and make that a global variable that we can reference like so first let's get some space i'm going to say global item list equals and like i said we're going to make this just a big struct that stores all of these other little structs so to make a struct you open a new thing of curly brackets around it i'm going to tab this out and now we're getting a little error that's because now the format of this struct is just a little bit off you see it says we got an equal sign but we expected to get a colon and that's because now the relationship between global.item list and burger is the same as the relationship between burger and the name so whenever we set name for burger we had to use a colon right so now we're setting burger for item list so the same way that we would be setting that would be with a colon and now we're all set so i'll show you something interesting now now if we were to run the game with the exact same draw code that we have this is going to give us an error so i'll run it really quick and what this message is telling us is that the variable burger item manager.burger has not been set before trying to read it so it's trying to figure out what this means and the reason it can't figure it out and the reason it has not been set in item manager is because now burger just does not belong to item manager burger belongs to no one but item list but our global.item list so now you kind of see how they're like they're separate things we're setting them in this object but structs exist as their own thing like that's what i mean by they're very similar to objects so now if we want to reference those we just need to reference them based on our item list so we'll say we have our global item lists burgers sprite and we do that down here too hit play and now we're fine now it's working so cool cool now this means that we can make a bunch of more items have them stored in our global item list it's very very easy for other places in our game to reference this item list so good and just like we had to do here whenever we were setting this stuff for this item we need to separate these with commas so at the end of my curly brackets here i'm going to add a comma so i can add a new item and i'm just going to add one more because i think it will help for demonstration purposes don't go adding a bunch of items yet trust me it's going to get it's going to get very cool and very efficient at some point but i want to walk you through it step by step so you really know what's going on and you can make your own educated choices in the future okay and i'm just going to space this stuff out a little bit more so now i made another item called bomb got its name got its description i've got its sprite which i just added from that other project really quick same exact size as the burger eight by eight origin at the top left and i've set it up exactly the same way using the colons and the commas to separate everything so now that we have two items i think it's about time we actually made our inventory so i'm going to go down here and create the inventory like everything in programming could do this a million ways you can use data structures arrays whatever i think i'm going to use an array for this because nowadays gamemaker has more functions that actually support arrays and make them a lot easier to use than they were before so i'm just going to call this array inv and we're going to use the function array create this way we can just create an array that is completely empty and so yeah with this you can choose the size of the array you want to initialize and whatever the default values are you can also optionally choose that it doesn't matter for this and now we have our inventory i mean that's all it is so let's temporarily just add a burger and a bomb in here so we can just use array push which is cool just adds a value onto the end of an array very easy very simple we're going to push this value onto our inventory and the value we want to add would be global dot item list and for our first one we'll just add our burger now that's cool and we can just do that again and this time add a bomb or add another burger or whatever array push is actually nice because you could just keep adding them for the same thing but um for the sake of visualization i'm gonna do them on separate lines because a little bit later we'll make a function to help out with this that is really really nifty so now our array has these values and since there's not much else we can do with them let's just try and draw them out so i'm gonna go back over here and we're gonna be doing a very similar thing but we're gonna be looping through our array i'm gonna put uh my font at the top here and then i'm gonna set everything else up again if you don't know anything about for loops uh menu system video just watch it so i'm gonna say 4 var i equals 0. so we're starting at 0. as long as i is less than the length of our inventory so array length of inv then we're going to keep adding to i until we've looped through everything so let's put all this here now we're obviously going to need to manipulate our x and y positions and get the correct values for our sprites and names and stuff like that so i'm going to create uh local variables for x's and y's so we'll just say they'll both start out at 16 so if our xx equals 16 var underscore yy equals 16 and dealing with our sprite first the sprite that we want to reference is stored in our inventory like this so remember we stored globalitemlist.burger.item whatever it is into our inventory so instead of having all that there we could just say inventory of our loop so of our eye the sprite for that so now we're getting the correct sprite sub inch zero sub image zero still and then i'm gonna do it like i did in my little demo so the x's are always gonna stay the same and then i'm gonna do our y value here plus 16 times i so that way every time it loops through it goes down a bit and i'm actually going to turn that into a regular variable in a second so for now we'll just call this underscore separation like sep set that also equal to 16 and i'm going to use that here instead because in a second i'm going to make an actual variable in our create event for this which will help me out with selecting the items later so if you're following along with me pretty closely then can continue to do so so uh we got the right sprite now for drawing the text i actually don't want the text to draw below it i want the text to draw to the right of it so we'll say we want it to draw at the same x plus 16 and that one i'm just going to keep is 16. like i said all temporary visuals so that there and then i'm going to do the same thing i did with the y so that's gonna be y y plus our underscore separation variable times the loop that we're going through and then same thing is up here we just need to use the value from our inventory and cool take a look over this really quick and if we run it you see it works i got my burger up there my bomb if it's not working for you just take another little check be very thorough with your checks i know it can be very difficult to find the differences but that is a skill you you have to uh get better at and believe me i know it's awful at first getting used to it but such is the way of programming so it looks like we're doing pretty good i'm gonna go ahead and really quickly just add my own special variables here at the very bottom and say this is just for drawing and mouse positions that's why it's actually important that i have these down here um is so i can track it in the step event as well with uh with the same type of loop so say screen border which i will use for these because this is just how far it is from the screen so i want to keep them the same different distance you could have one for x and y it doesn't really matter and then separation will just be sep or okay i don't know why i said speed it's cause i was typing when i was thinking um or said all right uh sep not speed okay and run it one more time just make sure everything's cool and yeah we're fine so so cool now we have a way to make items and a way to store them in our own inventory now is gonna be where we're gonna get a little bit more efficient with it and we're gonna start doing some cooler stuff so it is very easy to make our items right now like very very easy however it could be even easier now something that goes along with structs is something called constructors and you can think of a constructor as like a template for a struct or maybe even you can kind of think of it as like a parent object you know when you create an object it has a bunch of variables and it has its step and draw event and all that stuff and if you make a child of it naturally that child inherits everything from that parent that's kind of what constructors are good for but like i said really they're just a good template for making the same types of structs so something like making an item list you want them all to have the exact same type of information there's no reason you wouldn't want an item to have a name or description or a sprite now i mean creatively you might but in that case then you just wouldn't type a name in or you would type some gibberish or question marks or you know if there was a sprite you would just put in a blank sprite or you would just say no one and then have the appropriate checks in the appropriate places the point is the reason you wouldn't programming wise want to do that is because it would make it a nightmare if not impossible to have a system that could use your items every single item would have to have like unique programming which could just ruin your life so so our next step is to kind of reverse engineer these structs that we've made and let's make a constructor so we can have a template so here's how we would do something like that so i'm going to go above our item list and i'm going to create our item constructor and here's how you do that so you would use the function keyword i'm going to call this just create item close parentheses because this is going to look like a function and then we'll end it by saying constructor and open up our curly brackets really quick so now we're basically we've made a function that's purpose is to create a struct create and return a struct so uh we can make our arguments up here first and we can just go ahead and set these in the same way you would within with a normal creating your own function which what it kind of does is it automatically creates these kind of local variables for you and whenever i make local variables i normally start with an underscore that's just what i do it's what a lot of people do so um we're gonna want name description and sprite and like i said these are just like essentially they're just argument variables they're just so our function knows which values to read where so their names are not important just as long as you know what they mean so like this one can just be underscore name uh and then we can add another argument and i just underscore desk and then we can do like an underscore spr for underscore sprite you see they filled out here and they turn light yellow and the reason i didn't type these out all the ways because you can see at the bottom here whenever i highlight create item um it shows us the arguments that it wants and so depending on how complicated your items are how many uses they are how much information they all have uh you'll get a lot of arguments so i just don't like seeing super long names down there but make sure that whatever you put up here you know exactly what it's supposed to be so the format for our constructor here is more familiar so we can just say name equals that first argument so name and then semicolon and we could do the same thing for description say equals underscore disk and sprite equals underscore spr now these are going to be the actual variables down here so i type them out fully and i name them something that i know how to reference in situations like this where i need to reference a structs dot sprite or dot name or something like that so now we have this constructor to make our items and really quick i'll just show you outside of the item list how that would look normally so we would want to say like burger you say equals new then we use our function create item and then we fill in that information so we get burger description you know this and that and so on and so this is how we would create a struct using a constructor the format of this is also a little bit flexible so you could indent between all these like this this is totally fine too and you can see how it lines up down here so to do that inside of another struct like item list it'd be the same thing since we're storing some information in another struct already we would need to use this colon and we don't need to use our curly brackets because we're doing everything inside of this function here so let me just redo all of that down here so i'm going to say burger equals new create item i'm going to go ahead and start spacing them out which you might want to at this point because it will be really useful later on to have them separated out like they kind of are down here and anyways add burger need to make sure we add a comma to separate these arguments out add our description and then finally our sprite again and then last but not least we need to remember that inside of structs we have to separate each of their own values with a comma like down here we have name separated with a comma from description separated with a comma and you know we have bomb separated by big comma because it's inside this struct so remember don't don't forget that so looking at these two they they look pretty much identical and it may be kind of difficult to judge what the advantage is here because it looks like they're basically doing the same thing obviously the first thing is you don't have to keep re-typing these same variables but the much more important thing is if you have 50 60 items in your game every single one of these variables has to match every other item if you want them to work in a larger system if 59 of the 60 items have a dot sprite then it will work for all of them but if for some reason something goes wrong with that 60th one maybe you just mistyped it which is an easy fix but if you mistyped it it will cause an error or at worst there will be times where you decide to add new qualities to your items as a whole and having constructors set up all that stuff for you and then give you a list whenever you click on them of everything that you need to have in it is immensely useful so uh let's redo our bomb object really really quick so there we have it i'm gonna test it one more time just to make sure everything's drawn all right and yeah so before we move on to actually having our items have real effects i'll really quickly show you how to pick up items in the overworld and create items like that so first we're going to make our own little function to add an item to the inventory which will basically do the same thing is here but if we make it our own function we can iterate on it a little bit and that'll be really helpful for us so let's just get rid of these really quick and i'm gonna come out here and create um a little script and maybe i'll just call this item functions something like that just the script itself not this we can go ahead and get rid of this and we're gonna make our own so we'll say this one will be for picking up items so i'll call this function item pickup open up our curly brackets and the only argument that we need is just going to be the item itself so it works the same way like these little local variable type things and we can essentially use that same code that we used earlier so we can say array push because we want to be able to use this function anywhere let's go ahead and reference our inventory object so object item manager inventory is the array that we're trying to add to and then we just add the item that we've passed through our function same thing very similar you'll see why isolating this in its own thing will be helpful and we can go ahead and at least use it with a new object that i will call i'll just call it object item over world or you call it object item in game i know overworld is pretty specific to like feeling to top down stuff but you know you know what i mean so just a default i'll make it like the burger sprite or something like that like i said you could have your your own set of overworld sprites for stuff if you wanted they don't have to be the same as your icon sprite but for here we'll add a variable called item and i'll make the default here say global dot item list item list dot uh burger right our default item is our burger because that's what our sprite is so let's just start from there and we can now add our step event and we want if the player runs into this the item goes away and it's added to our inventory so we will say if place meeting x y object player so if we touch the player first we want to add the item to the inventory so we can just say item pick up and pass through our item that we have and then we can just destroy ourselves and i'm just now realizing that item pickup probably is not a very good name for this function um maybe item ad or inventory ad is better because there are other circumstances in which you could add items to an inventory not just picking it up so yeah you know i'm going to go ahead and change this i'm going to go ahead and change it to item ad i'm glad i thought of that right now instead of later on but uh yeah so anyways i'm going to snap so you're paying attention again you drifted off into slumber within the last eight seconds then know that item pickup is now item ad sorry so okay so uh i removed from my item manager the stuff down here so there are no items in the inventory anymore but i now have this function item ad and the uh overworld item here that should help me out with this so i'm gonna place some items around in this room here let's put some burgers down put like five burgers it's a lot of burgers so what should happen is we run into them and yeah look at that added to the top left here and it vanishes when it's added and we can just keep adding burgers excellent so now i can step out of here and now once again something that has a billion different ways to do it i think for efficiency's sake just having one object that does all of it a good way to change which item it is in the overworld would be to like set it in the creation event so basically just put this code in the creation event of whatever instance you wanted and if you wanted to be a bomb change it to bomb and then in our step event we can add this code we could say sprite index equals whatever our items sprite is and this will automatically change our sprite once the game runs to the correct one to match whatever we put in the create event so let's say if i want this one to be a bomb i can just say item equals global dot dot item list bomb and if i run the game that one's a bomb now and if i gotta touch it it adds a bomb pretty easy pretty cool so we can now collect items we now have an inventory and we can display that inventory albeit just the uh sprite and name no description or anything else yet so now the the path forks in like 11 different ways so let's start with the displaying the description so i'm back in my item manager and the next thing that i want to do for this specifically is i just want to be able to track whether or not my mouse is hovering over an item because that's how i'm going to be using my items and so let's set a default value for selected item and we're just going to always keep the default value at negative 1 so meaning nothing is selected and we can add a step event here and we can say get selected item we're just going to be getting where we are in the position of the array and going from there and easy way to do that is to loop through all of our positions again in the same way that we did in the draw event so first we want to set our default of selected item to negative one then we want to check for other stuff so we can go into this draw event let's just take this loop and copy it and paste it right here now obviously we're not going to be drawing anything so get rid of this stuff so now i'm just going to do uh x and y position checks for my mouse position to make sure i'm within certain parameters and those parameters are based on this stuff so um we want to make sure our mouse x is bigger than this x so if mouse x is greater than x x and then we want to make sure the x is less than the right side of our item so about here is where x x and x y are and we want to make sure that the mouse is somewhere you know around here so it needs to be greater than that value and it needs to be less than the size of our item icon which for me is just eight so we need to be greater than that number and we want our mouse x to be less than x x plus eight and same principle with the uh y values y has to be greater than y and y has to be less than yy plus eight and now that we're in those parameters we can set our selected item to i oh um and actually we need to we need to compensate for the the separation here as well so instead of using separation here i'm just going to add it into our y value naturally so then we'll say plus separation times i and so now we're checking at the right y positions for everything as well and i'm going to go back in our draw event and i'm going to add another parameter here and this will just give us visual feedback that we're actually selecting an item so we'll make a local variable for color we'll set the default to white separate out where we draw the text in the sprite and i'm going to say if our selected item is equal to i so only on the loop that the loop itself matches the selected item while we run this code and i'm gonna set the color to just uh yellow so that way we can actually tell that the item we've selected is selected then of course we have to actually draw set our color and then reset our color back to white so it doesn't mess up the rest of the game so yeah in this step we're making sure that we're hovering our mouse over the icon then we're setting our selected item that way and then in the draw we're checking through the loops if it matches up with whatever item we have selected and then we're just going to change the color of the text itself just to represent that we are actually hovering over it so grab a couple of these okay there it is it's turning yellow so that means we're on our burger and we're on our bomb good obviously the main important thing is just just getting the selected item itself it obviously doesn't have to be with mouse positions and stuff could very easily be just with some variable that you cycle through with button presses or something like that but now that we have this we can do pretty much everything else that we're going to need another thing i'm going to do really quick is i'm just going to draw our invent or our description text i'm going to comment these out it's getting gross okay hopefully this looks a little bit more intelligible if not only slightly so now like i said down here i'm gonna add our description i'm gonna use uh draw text extended so that way the text automatically wraps around it doesn't just draw in one really long line i want to draw it at the same x position i think as the name but the y position as you saw in the demo i decided i wanted to draw it at the um the very bottom of our list because that's about the easiest place to put it right now to where it won't overlap with any names we have so uh to do that i'm gonna do our separation times the length of our array of inventory so that way it is drawing under everything else directly under everything else obviously the string we want is our inventory i dot description separation is the vertical separation between the lines once it does break and then w is width and that's how long in pixels we want to let the lines go before they start indenting or um you know whatever you want to call it so i don't know i'll just say 80 that might be a little small but that's fine and then the last thing is we only want to draw the description of our item if it is selected so we'll do the same check as we did up there if selected item is equal to our i our loop and let's see how that works yep there it is it works and you see that it automatically breaks the line up there cool um and i actually don't think it looks better added out so i'll just make it the same x okay i think maybe that looks a little bit nicer but anyways okay i know now i know it's been a long time coming but we are finally at actually using items and i wanted to save it for last one because a lot of this stuff was kind of necessary or would have been a lot more complicated without all the visual feedback that i've added but the main thing is it's a really interesting little thing that you can do with structs and i wanted you to have time to get used to them a little bit so for now let's add a new parameter to our item constructor and we will call it our effect do the same thing down here effect equals effect now this is really really cool what we can do with this we can actually store our own custom function here which means that we have essentially completely limitless possibilities for whatever the effect is and this makes triggering it massively easy so i'm gonna add a comma here and i'm gonna add myself a little bit of space for this effect parameter here so let's make a function and and just to clarify there are multiple ways to make a function there is the way that i made this function which is to say function and then you type it out this way the other way to do it in like an object or anywhere else is you could say like my function equals function and then you would type in the parameters like this this would be the exact same thing as this you can do this either way these these both these both work the only reason it's giving me uh an error right now is because it's confused because i put them right next to each other but um so that being said we can store a function in this way over here so that's exactly what we're doing for for our effect and we're just going to say function and we're not going to have any parameters for it we don't really need to because this function is very specific to this item so whatever we want it to do we can just tell it to do that so for a burger let's say we want to take our object hp and we'll say add 10 to it and that's really really simple but again we could just keep doing stuff and the way i want to do the using items thing is uh i i like to get rid of them in this function so i would say like get rid of the item and the way i would do that is uh array delete delete it from our uh inventory the index that we're using is our selected item because that's the position where our item is um and then we're just gonna number as one that just means how many from that point on do you want to delete so you could delete your selected item and then delete the next three if you wanted to but this basically just means we're just removing that one item at that position which is this burger here and the reason i delete it in this function instead of say like the moment you click on it is because maybe some items you don't want them to delete maybe they're unlimited items maybe um maybe they swap out other items and stuff and this just makes a little bit more sense to me to be completely honest with you just know that this is how you you would delete it if you wanted to do it on the button click or the button press or whatever you could totally do it that way it would make certain items less redundant for sure you could also make this into its own function like um exactly like i did with item ad you could do item remove but because a lot of the way i have everything set up is by dealing with with a number not necessarily a type of item i i don't see the need to make it any more simple or technically any more complicated than just using array delete and then whatever position you want to delete it from so anyways now we have all that done and so now how do we trigger this effect well that's easy we can go under wherever we got our selected item again the only important thing about this code is that you got the correct position of it in your inventory so as long as selected item gets the same information as this up here then we can use this so first let's just check and make sure that something is selected which means we'll say if selected item is not equal to negative one then we can start running some code and now let's say use an item so i'm gonna say we're gonna use our item on our left mouse button click so if mouse check button pressed not not just check button this runs every single frame and will probably cause you some big issues this checks for one single frame so it only runs the moment you hit the button and that's what we want so if mouse check button pressed mb underscore left for mouse button left then we want to perform this function and we already stored it in our items effect variable parameter function whatever you want to call it i'm not good at getting certain technical words right sorry but basically uh we just need to say from our inventory array selected item and we're just going to use its effect in the same way that we would call like a function and it has no arguments so you just open up the parentheses and do that and this will use the effect now currently we don't have an effect set up for the bomb but um let me just show you the burger okay so i'm going to pick up pick up my burger and if i click on it there it goes and um invisibly the player's health got added back let me just add a little indicator up there okay so in the top right i've added some code that just shows how much hp my player has click on it got 20 now excellent now we now we don't currently have an effect set for our bomb yet so we can add one really quick and we don't actually have to do anything yet uh we can just put an empty function in here so not actually doing anything yet it will just run nothing it won't even delete it because we don't have the deletion code in here for example see so i've added a new object that i'm just calling object bomb weapon it has no code but this would be like a zelda type bomb item or something like that and then i added um a sprite that just flashes a little bit to make it look like it's about to explode just to show that that's what it would be doing so for our um bomb function we can say instance create depth i'm going to say we want to create this at our object players oh x and our object players y uh right now depth doesn't really matter it would you know you would just create it wherever you would actually want to show up and then and now i want to create our bomb weapon and we could take this code right here to get rid of it after it's done so let's see if that works okay picked up my bomb and yep left a bomb that's about to blow up now the bomb could be like a a permanent item that you had so like you might want to keep it so don't get rid of it but maybe your player object has like a bomb count or something like that like in a zelda game so you would just say like if object players bomb count is greater than zero then you could make a bomb and you would just want to subtract from that bomb count but i'm just going to set it back to how it was just because i don't have that variable and i don't feel like doing any of that just a single use for me but now you can see you can you can just do like so much stuff with it here i'll uh we'll do a couple more things i'll show you how to make uh the red and the blue keys which is super simple okay so first let's talk about our red key named it red key literally named a red key added a description opens red doors it's a single use item it created a red key sprite very simple one and so my function checks for an object called object red door which all that is it is just it is just a wall object it is just the child of a wall object i have a depth setting code just so it has a little bit of depth to it so it's it's honestly more like a big locked block than a door but um it's just a wall that's all it is there's no special code in here whatsoever so for my effect one it checks to see if there are any of the red doors in the room and then if there are we go into every single one of our red door objects and we say if i'm within 20 pixels of the player then i'll destroy myself this is a very simple way to make doors open you'd probably want to be slightly more specific about how you check it or even slightly weirder so you could do some weird fun stuff but um you know anyways the point is is if this object is within 20 pixels of the character it will destroy itself which basically opens the door it's just a very simple lock and key type thing literally and then it removes itself from the inventory so the first thing i'm going to do i'm going to add a couple of these red walls here and i'm going to make this item a key so i'm going to say item equals i'm going to make this a red key and then i'm going to copy and paste it again to add one more key because there's something else i want to show off and then maybe uh add some more red walls just to show that they don't all go away so testing time so again these are just walls i have very simple collision so i collide with them i pick up my red key i'm gonna stand by these walls and use it key goes away and the walls go away and we're good and these walls are still intact because they were farther than 20 pixels away from the player one thing is that even if this key doesn't open any doors at all if you use it it still goes away i think for most games you probably wouldn't want that to be the case could be an interesting mechanic for for uh for sure but i think generally most games only want to give you a limited amount of keys so we can add a cool check here i'm going to open up a local variable oops so i'm going to call var and i'm just going to say used and at default it's gonna be false then we can come down here to where we destroy this red door if we're destroying a red door it means that we have actually used the key right so i'm gonna give all that a little bit more space now i can set this local variable to true and so now that we've confirmed that we've actually used it now we can remove it so we'll say only if our used variable is true do we delete the item so let's run the game again and check so if i run over here and try and use the key it doesn't work but then once i actually use it it goes away for both of them excellent and so uh that might be a type of thing that you want to use for a lot of different items where you only want to use them situationally but you also don't want to waste them if they don't work especially if your game is very point and clicky where people are just going to constantly be trying to use items on everything but i mean that could also be pretty good incentive to get people to try and make more educated decisions on where they try and use items that's a thought isn't it okay well anyway i need to think about that more uh later i'm on my own but anyways uh yeah and if you just wanted this to be like a key card type item then obviously you would just never get rid of it in the first place once you got the red key it always stays in your inventory and you can always use it and that's just that's exactly how i did the blue key for the intro part all the same exact code except i just didn't even use this used variable or any of that and didn't get rid of the item at all same exact thing but when it comes to key cards and stuff like that something you do not want to happen again most likely is you don't want to be able to drop those items you don't want to be able to drop key important items but you probably do want to drop useless items especially if you have a limited inventory space so let's cover both of those really quick so first let's go to dropping our items so we can just add a new parameter to this constructor i like to keep the effect at the bottom at the very back so uh we'll just add a new little thing here new argument and i'm just gonna call it can drop and so um i'm just gonna call it the same thing here too just without the oh just without the underscore um might look a little bit weird so i might add that under there for me so can underscore drop and obviously what we would just be doing with this is just setting a variable to say can we drop this item are we allowed to so for something like a burger so yeah sure we can drop that so um we'll just say true because that's where it would be it would be in between our sprite and this now you can see it down here you know what i'm going to add that underscore here as well i don't like the way that looks so underscore can underscore drop underscore can underscore drop um yeah and so yeah we can drop a burger i want to make sure to add that comma back in um you could also drop quote unquote a bomb so get rid of suppose that's true and for our red key even if it's a limited item it's still a key item it's still important so kind of no matter how i would handle a key item i would probably just say uh not true i'll probably say fall so probably no no you cannot drop this so cool now we have that variable for each item and now we can add some really simple code we already know whether or not we're hovering over it so let's just make dropping the item the right mouse button so drop the item okay so checking for a right click of the mouse and then we're going to do the same thing that we did for these items that we wanted to get rid of so i'm just going to copy it go back to our step event and paste that code there it's the same thing but we need to add that one more check we need to check that can drop variable because otherwise this will just delete any item including keys so let's say and our selected item so inventory of selected item dot can drop is equal to true now we can drop items and we will only drop items that we tell the game is okay so once we're on the game let's pick up a bunch of stuff burger got rid of it didn't use it hp top right still 10. same with bomb we didn't drop a bomb we just straight up dropped the item burger burger and we can't get rid of the red keys perfect now let me think i think a couple more things i want to do is well i can just tell you you saw the beginning of the video i had an item that was uh it was like a sweet bun item and it was like an item in a little two pack of plastic and if you used it once it still left half the item so to do something like that let's say with our burger i'm gonna make a new item just by copy and pasting i'm gonna call it burger half and this will be a half eaten burger this burger to finish eating and really quick i'll just make a new sprite just take my burger and just there you go very perfectly half eaten burger set the sprite correctly this is an item that we can drop and we'll say it has the same effect or now that we're having it let's say they each only add 10 right now this is all good down here burger half is done but now that we've deleted this item we need to add a half burger item to our inventory so we could just say array insert it's going to be our inventory array we're going to insert it at the selected item position and the value we're going to add is global dot item list dot burger half so now once we have this item deleted then we're gonna add the half burger item back in and we're adding it in the same place that the burger was we're not just stacking it back on top of the list this creates the illusion that you've actually just eaten half of the burger and it's the same burger so let's try all that out someone pick up all these items weird order okay so burger to eat now it's a half-eaten burger and if you look at our hp it only added five added another five bomb still goes you can still drop a regular burger you can still drop half of a burger so again this this function functionality is very powerful you can do tons of stuff with it it's really cool and one more thing to cover would be having a limit on your item inventory so let's make a new variable i'm just going to call it m max and we'll just say it's five five items it's kind of pathetic item count but we're gonna do so we can use this in conjunction with a function that we already made our item ad we only want to add this item into our array if we have room for it so we're going to check the length of our array and see if it is lower than our max inventory size so we'll say if array length of our object item manager inventory is less than our object item manager's inventory max then we can add the item in so now we can't have more items in our inventory max as long as we're using this script which is the way we should be adding items from now on this is why we made this script instead of just use array push all the time so let's test that out so one two three four five and let's see if we can add the sixth one we can't we can't add the sixth one but it does get rid of the item as if we picked it up so here's something cool that you can do with functions that is very very similar to what we did with the red key checking let's add a local variable and i'm going to call it added and we're going to start it off as being false we're going to do the same principle where we only change this local variable to true once we've actually successfully added it to our inventory and then we can use the keyword return and return this variable whenever you use return it sends back whatever value you put after this keyword and it exits the function after that so now that we're getting more information from this function let's go back into our overworld item object in our step event and instead of just running the function by itself and then just calling the destroy function we can use this function as an if statement so we can say if item add is equal to true then we can destroy ourselves even though we put it in an if statement dislike item add still runs all of its code even though we're technically it looks like we're just trying to return value we're just trying to see if it's true or false it still has to run all of this code to return that value it has to run all of this code to get the value that you want to send back at it so that means anything that you do before you return it still happens even if you're in an if like this even if this is false it has still run all of this code so anyways i'll stop rambling about that hopefully it makes sense hopefully i didn't just talk for too long again and now let's see if this does what it's supposed to do so you pick up one two three four five and we can't pick up this last one because our inventory is full and it doesn't get rid of it and now i can eat this burger and now i can pick that one up so yeah so as you can hopefully see there is a lot of room for editing this and adding to it and it basically just means that like at any point you could add your own functions to it you could add more parameters um if you have a shop system you know you could uh you could add a uh a price uh here we go price and so now you just need to go here for each one and you could add like a uh a money value to each item to determine how much they cost to shops and stuff like that if your items are like in star crossed arcade which is more like an action game i use this system for the sub weapons of the game and so each one of those has like um a magic cost alongside of it like an energy cost if you start getting complex and you have like different characters and stuff like that you can have uh variables for like which which characters can use this there's a lot of stuff to do with it uh and obviously there's a lot more room for me to personally iterate on it in another video and stuff like that which i think i'm gonna do in fact uh i mentioned this in the um the 2000 subscribers video which uh if you didn't see that or if you're new which you probably are um yeah that's pretty crazy to me that was a big deal for me and so um i just did a little video on that and i mentioned some of the things that i wanted to try out going forward um you know first of all yeah uh star crossed arcade is my main focus for the next few weeks the next little while here you can wish list it on steam right now uh if you really wanted to help me out that would be awesome um but chances are by the time you hear this video uh it probably might even just be out so if you if it is then uh definitely go check it out if you wanna support me in some way um once i'm done with that i'm gonna be making videos a lot more regularly once i get back to main big focus on rosa starcross which is just a slower project in general so i don't feel quite as horribly pressed for time as i do with star kick because i want to get starcade done but anyways uh right after that one of the things that i'm thinking about doing is maybe little series that are like you know how to make how to recreate x game and do like older games like nintendo games and stuff um like how to recreate zelda one or a link to the past or something like that um earthbound is one that i'm really thinking about doing castlevania super mario brothers all that kind of stuff and since that stuff would be really really contextualized i'd be able to cover some really specific topics that i know for a fact might be more what someone was looking for because the problem with making videos right now is sometimes i have to be very vague in the concepts which is good in terms of creativity and good in terms of wanting people to make their own unique things which is what i really want but uh you have to start from somewhere and it's easier to see stuff contextualized in the picture of a larger game and how it works sure a lot of you guys are going to be able to actually use this item system in your game and for a bunch of different types of games too especially because it's so it's become such an easy thing to make like a good item system now like the one in the demo of rosa starcross is just horrible i hate it i hate the way it works it was so stressful um and now i cannot wait to go back and uproot all of that and replace it with this stuff but um yeah there's there's no doubt that you know if you want an earthbound like item system then it's important then something that's important about that is the way it works in like battles and stuff like that and how it interlocks with a couple different systems and stuff item systems can be a little bit complicated if you think about like metroid games you have different types of items you know like you have your weapon items missiles bombs whatever and then you have upgrade items a lot of metroidvanias have like overlapping item inventories and stuff like that uh like i said this kind of system would be really really easy to create a second inventory um you know we just created an array called inv and in your item manager you could just create another array called like in stock or inventory stock or you know just whatever something that represents like an item pool that you're not carrying um and then just make some really simple functions using like the array delete and array insert functions that we talked about um you know just use a just use a combination as those delete a certain value from one of the arrays and then just add it to the other one and then just make sure you're checking proper inventory maximums and stuff like that but i mean that's that's all very adaptable from this system right here so yeah but i don't want to leave you too high and dry or anything this is definitely going to be something that i come back to at some point but just contextualized a little bit differently but uh yeah hopefully hopefully this helped you out hopefully this is a good addition to your game and you're well equipped to actually use it make your own modifications and stuff to it if you want like i said the best thing you could ever do in the world to help me out if you wanted to um would be to wish list star-crossed arcade on steam uh or if it's out which there's a very good chance it probably will be when you're hearing this um you know please just go buy that if you want uh definitely subscribing liking video hitting the ring and ringing that dangle bell is a you know that does something i'm sure i'm not gonna lie i actually don't know what it does but it i hear other youtubers say it all the time so so yeah if you want to do those things um follow me on twitter all that stuff um i'm very very very very busy lately the main reason i made this one i made this one a little bit quicker than i did the other ones just because it's been a while since i put a video out and i really wanted to have one out but um i'm chest deep in star cross arcade work and other things just a lot of other stuff had a little minor back injury type scenario from sitting like a gremlin at my desk all the time so yeah very busy and just dealing with a lot of stuff so um answering questions and stuff like that can be very very difficult for me because it can be super super time consuming hopefully that'll change soon though what's really cool is there's more people here now so still ask questions in the comments one i might be able to get to them and two if i can't there's other people watching these videos that can hopefully help you out or maybe someone's even had a fix in the comments for your you know yeah if you're having problems too also just always remember to go back and really really check make sure your code lines up properly i know it can be a drag and a pain but like i said earlier that's just kind of a skill that you got to get into so yeah thank you very much and hopefully i'll be making a new video very very soon bye bye [Music] you
Info
Channel: Peyton Burnham
Views: 2,910
Rating: undefined out of 5
Keywords: gamemaker, gms2, yoyogames, inventory, item, items, inventory system, rpg inventory, zelda items, undertale, gamemaker studio 2, tutorial, how to, make an inventory, make items, gms, drop items, use items, keys, item inventory, item system, structs, constructors, top down, metroidvania, platformer, item inventory system, indie, indie game, point and click, puzzle, limited, free, program, programming, deltarune, jrpg, pixel art, pixel, dynamic, how to make, game design
Id: fa26B54JDDk
Channel Id: undefined
Length: 70min 37sec (4237 seconds)
Published: Sat Nov 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.