GameMaker Studio 2.3 - Beautiful Inventory Part 2: Adding An Item To A DS Grid From an Array

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
greetings and welcome my name is aaron craig with let's learn this together and this is part four of designing a beautiful inventory the finished product looks like this with a lot of features but specifically today we're going to be taking a look at this whole function right here of adding an item from an array to a ds list you can download the finished product in the comments below or pause the screen right here and just copy this code i'm going to go ahead and go on and explain exactly how i do this and why i chose what i did so you can implement it into your own game stay tuned to the end for details about the giveaway of my beginner course on all of my game maker tutorials so i'm going to create a script right click create a script and i'm using 2.3 so a script here is actually a container for functions but if you're not on 2.3 that's okay just name this script the function that you want to use i'm going to name this inventory because i'm going to put all of my functions inside of here and for right now i'm going to rename this specific function add item to master list and i'm going to put a description inside of here add an item to master list now that's kind of redundant but at the same time it's always a good idea to get into the habit of using descriptions and parameters which is what i'm going to add next so the parameters here are actually going to be attributes this is the array of attributes to add and in 2.3 the function works like every other function in pretty much every other language so we can actually just type in attributes and that is what we can access if you're not on 2.3 you just say var attributes equals argument zero and then we're off doing the exact same thing after that so i want to show you some safe coding practices but before i do that i'll just go ahead and show you exactly how we're going to add this to the ds grid so if that's what you're here for you can do it the first thing we do though is look at the size of our grid right now i've created it at a size of zero because there are no items inside of it the way that i'm going to be doing this is always adding one two whichever one we're at so we have to resize our grid so that it goes up correctly every time we add an item and the function we use for that is ds grid resize so we're going to resize this and i am using my ds grid like this the x or the width of it is the items so i have this is what it looks like and the height are the actual properties that we're using so when we resize a grid we have to pass in the grid which is going to be global.all items we pass in the width and this is the new width so i'm going to say dsgrid width of global.all items plus one and then the width or the height of it is going to be ds grid height of what we currently have because we're not changing the actual height at all add two parenthesis because it's all wrapped up inside of a function and now we've resized our grid now we're going to actually add to it so four of our i is equal to zero i is less than array length and if you're using not to if you're not using 2.3 then you'll use array length 1d we'll say attributes plus plus i and now all we have to do is just map each of the attributes of the array so each index of the array to the index of our ds grid which they should match up exactly because that's the way we have it designed so i'm going to say global.all items and this is the ds grid and we can actually access a ds grid directly as an array with the hashtag or the pound symbol and now we're going to always add the attributes to the item that we just resized for so ds grid width global.all items minus one so when we made it we created it with zero items so it wasn't it was it was not able to hold anything now when we resize we get how many items it can hold oh it can hold zero so let's add one to that now it can hold one item and that one item starts in index zero but when we call ds grid width now after we've resized it it's going to give us a one or two or three or however many items you have so we have to go back one because we just added that one to make sure we're in the right spot but you can't resize after because you have to have that slot available so it must be done in this order just go back to the one you actually want it at ds grid width returns starting counting at 1 but arrays and ds grids start counting at zero kind of annoying but if you understand that that's how it works then you can get around it and the attribute that we're changing for this item is just i because that's what's moving up all the time so close that bracket and just set it equal to attributes i are you ready to start making the game of your dreams then head on over to less learners together dot com to check out my trilogy of courses to take you from beginner to expert game development is hard and frustrating when you're going at it alone and you don't have anyone to turn to join me on the journey and i'll be with you every step of the way to alleviate all of that frustration and by the end you'll be ready to make any game you can set your mind to go ahead and get started now at less learners together.com and that will actually work so i'm going to put a breakpoint in here i'm going to open up our room and i'm going to put the item that we're working on right now which is obj items inside here so we can actually watch this work as it goes so i'll actually put it right here so we can see it get resized as well so i'm going to hit f6 now in here i'm going to pull this up here's our global variable i'm going to right click on it and view it as a ds grid that way we can actually see inside of it so right now if i try to expand it there's nothing there it has a width of zero and that means that it has no items like it can't hold anything but it does have a height of six if i press next now we have one item in here with six empty slots so let's go through and add those in so the first time you can see over here here's our attributes our local variable 0 is small knife so attributes 0 because that's what i is is small knife and we're going to place that here so i click next and it transmits it over you can keep going through and it keeps doing that for all of them and then when it's done well it's done so now we're all done and that's it that's how you add an array to a ds grid it's with a for loop and make sure you resize it beforehand unless you created your grid with the proper size now this is actually adding the item but what i want to also show you is being able to make sure that this functions exactly the way that you would expect it to so if we comment this out we forget to make it or anything like that and then we run this well it's obviously going to crash because we don't have that variable so i want this function to always work without crashing our game now when i say work i just mean it gets called and if it works then it proceeds properly if not then it's going to return gracefully and not crash our game so the first thing we're going to check is does the global variable exist so if variable global exists hey there's a function for that look at that we have to type it in as a string and we only type in the actual name of it not the global portion so we're going to type that if this is equal to false what i'm actually going to do is create it so then global dot all items equals ds grid create zero and then item that height and that way now if it doesn't exist we make it so let's check again is the global variable a ds grid now we know that if it doesn't exist we make it and so that it is but what happens if somewhere else in your code for some reason you destroy it or you reassign it to something else if that's the case and that happens somewhere along the line after you try to add an item to that then you're gonna get an error and it will crash your game so now we're going to check to see if ds exists global.all items and it's a type of grid and if it's not so it doesn't equal that then we're going to set it to be that so global. all items equals dsgrid create 0 item.height so now if it gets through here and it is a variable but maybe it's something else entirely we're going to grab that and we're going to recreate it to exactly what we need it to be now this might actually cause more problems in your game than it's worth so you could also comment these out and instead just show a message that's like no variable found called all items and then return so you can try to fix it or you can just say hey something went terribly wrong here the code that you wrote before you you were supposed to write something went wrong and then you just return return will take you out of the function and not do anything after it's called so that's actually what i'm going to do here just to be on the safe side no ds grid found and the last one we want to check is going to be are the attributes proper so if what we pass in of the attributes isn't an array then we've got a problem and if we try to add it then we're going to fail because we'll try to access variable attributes which isn't an array and we don't want to do that or array length and again if you're not on 2.3 use array length 1d if the attribute's length doesn't equal item.height then we're going to do the same thing so we're going to show a message and then return so input for adding items isn't right so we want to make sure that it is exactly the correct size there otherwise there's a problem so let's test that out let's add a 10 to the end here and now that's the wrong number of attributes so it doesn't work and it gets returned but our game doesn't crash and that's exactly what we wanted so now we are able to add items to the master list in a safe way making sure that it's always going to work properly and if you wanted to you could set up a system here where if you call this and none of these properties are correct you could fix that and then you add it so different ways of approaching the problem i'm going to go with the approach that if it doesn't work then you've typed something wrong in your code and you probably want to go back and fix that and be told right away that something went wrong so that's what i'm going to do right here and that's all i wanted to do in this video in the next one we're going to take what the items that we've added and we're going to add more in the next video as well but we're going to take all of that and start displaying it in a ui in the inventory that we're actually going to have in our game on every game maker tutorial and video i put out from here into the future i'm going to be giving away one copy of my beginner game developer course a great way to go from no programming experience to be able to make your own games to be entered to win just like the video and leave a comment showing me your keyboard works you can leave a comment about anything a week after the video is posted i will send you a message with the coupon if you want you can use it for yourself give it to a friend or apply it towards a more expensive course by just sending me an email and letting me know that's what you'd like to do if you want to see more content from me then subscribe and ring the bell to be notified every time i put out a new video but that's all i've got for you so thank you so much for joining me and as i always say keep making keep learning and i'll talk to you later a huge thank you to all of the awesome people who support me over on patreon their names are on the screen now and every dollar pledged helps me create more awesome content you can support me for as little as one dollar a month and get access to exclusive perks like my discord server your name in the credits early access to my youtube videos and courses and more check it out at patreon.com letslearnthistogether.com or find the link in the description below and become a patron today [Music] [Music] you
Info
Channel: Let's Learn This Together
Views: 2,236
Rating: undefined out of 5
Keywords: GameMaker Studio 2.3, GameMaker Studio 2, GameMaker Studio Tutorial, GameMaker Studio 2 Tutorial, GameMaker Studio 2.3 Tutorial, GMS 2, GMS Tutorial, GMS 2 Tutorial, Let's Learn This Together, GameMaker 2.3, GameMaker Projects, Add item to DS grid from array, beautiful inventory, GameMaker Studio 2 Inventory, Game Maker Studio Playlist, game maker arrays, sorting data, built in functionality, Game Maker Studio DS, DS Grid, Item Data, Custom Function, GMS 2.3, yoyogames, GMS
Id: kpTihLMHFeE
Channel Id: undefined
Length: 14min 3sec (843 seconds)
Published: Fri Aug 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.