Grid Based Navigation in GameMaker Studio - Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Laughter] greetings and welcome my name is Aaron Craig with beyond us games and in this video we are going to be going over grids and how they work and how to create one and why you would want to use one over the past functions that we've already talked about now I've got this running and I'm going to show you that here I'm going to draw my grid on the screen by pressing D and I've set it up so that I can use letters to start doing these things and you can see that I have rocks and trees set up here and if I press T it's going to add to the grid the trees you can see then that they now become blocked that you cannot go around them or through them so if I create a path here for my guy he's using the grid based functions and now he is finding the best and quickest path and he's able to move in a smooth and precise manner that makes him look really good and I can also add in the rocks that I've got right here and he will again find the best and quickest path now this is similar to what we've been doing but it is a lot smarter and it looks a lot better overall so we're going to set this up in the video and I'm going to talk about the best practices for setting up a grid as we go along if you'd like to follow along you can download the advanced paths functions we're actually gonna be picking up exactly where we left off and this will be available to download in the description below before we start any code though we're going to duplicate the tile set that's in here a couple times it's that way we can grab a couple of objects to add to our room so what we're gonna do is we're gonna open this up and I'm gonna come in here holding down ctrl to zoom in and I am going to select this tree I'm gonna take it cut it out add a new frame throw it in here press X on that and I'm going to come up to image and auto trim all frames that this becomes just that size which is what I would want up at the workspace right here we're going to call this SPR tree and inside of here we're going to come in do something very similar with this rock I want this rock cut it at it eliminate that and auto trim perfect so now we're gonna name this SPR rock and we're going to make two objects we're going to call obj tree and assign that the tree and we're gonna say obj rock and assign at the rock now these don't need to be marked as solid because the grid is actually going to take care of what the AI is allowed to go through and what it's not allowed to go through so that will take care of with code actually so now we're gonna make the grid object because I'm gonna take all of the code or at least most of it and keep it inside of one object that way it's easy to find and debug and change if necessary so we're gonna call this obj town grid and that's because this is the room town and inside of the room we're just going to add that in before we forget and I'm going to add in these trees and rocks so we've already got some trees so I'm gonna place a few of them over here that way it doesn't look too weird and we have a couple of rocks but I'm not gonna take too much time doing all of those so we'll add in a few more trees right here and a few rocks to block his path and his way so you can get a good idea for how that should work so now inside of the town grid we're actually going to create the grid itself and to do this we're going to be using functions called MP grid functions so if I type in MP grid you can see here these are all the functions that you have available to you and the one we want to start with is create now I'm gonna middle click on this and open up the menu and that's because this is really important to think about before you actually jump into it now creating a grid is easy you just choose these arguments and fill them in but if you plan ahead of time it will save you a lot of work and will allow you to create the best grid for you or games specifically so the cells that you are going to create in your game like I showed you in the beginning those that green overlay that cell size is probably the most important decision that you have to make and you can see right here that there is cell width and cell height now if you make these too small then it is going to take a lot more computational power to find and choose the shortest path out of all of the possibilities if you make them too large the the AI is not going to have as many options and it's not going to look near as smart choosing a path it's going to look kind of wobbly and very janky as it moves around so finding the exact right cell size for your game is important it requires a little bit of forethought now when you're doing this you normally want to create enough cells that are going to be divisible by your room size what that means is that when you have a room in your game you want to make sure that you're the number of cells you have are divisible by divisible evenly by your width and your height now I've changed my room size but most room sizes are going to be divisible by 32 and if you do that your your height and your width of your cells if there are 32 by 32 ish that's a that's a fairly good cell size it lets your sprite find a good number of paths and not take too long to do that so what we're gonna do is because I've changed my room we're actually gonna make mine divisible by 40 so you can I'll let you see exactly what that looks like as we draw on the screen but first let's create it so to create it we need to set where it's gonna start and where it's going to start is zero zero which is the very top left of our room because we wanted to cover the entire room sometimes you may not want it to if there's an area you don't want it to cover then you may want to start in a different position and then you can choose that position right there so the number of cells we want is going to be room width room height and the size of these are going to be room width divided by 40 and room height divided by 40 and with that that actually creates our grid now we actually want to save it because otherwise you can't do much with it so we're going to make a variable called room town grid and we're going to assign that to it that way we can actually do something with it now inside of our event I'm going to come in here to the draw and I'm going to set draw set alpha to 0.2 now we're going to actually draw the grid and the grid we're going to draw is room town grid and we're going to set the draw alpha back to one that's because if you don't do this the grid that draws over is going to be not transparent at all and cover up your town completely now inside of here I'm gonna come in and I'm gonna take out the grass because it is so green that you can't even see the grid when it's being drawn but if we take it out you can now see that each of these little boxes is about the room height divided by 40 in room width divided by 40 which gives it a good cell size it covers every object in here is going to cover a good amount of cells and it will provide enough options for my guard to find a good path through it so those are two functions that are very useful drawing your grid you need to see where it is and then when you do that you can also see the instances and the objects that you have marked as unpassable and to do that it's actually very simple you just need to use the MP grid add instances and then you give it the idea of the grid it's going to and the object which we're gonna say it obj tree and if it is precise or not now if it is precise that means that when it's when it's actually inside of here so if this was the grid right here let's see if I can make these a little bit easier here we go so if this was the grid that was on our game if this was precise that means that all of these would be filled up and this tree right here let's say that it was if this reached out just a tiny little bit then every single thing that it touched every single cell it touched if it wasn't precise it would fill up the entire cell so it would use the bounding box of this to say if even one pixel is inside of this cell that entire cell is marked as forbidden and you cannot go inside of it and you won't be able to do that now that's not what we want because we want it to look intelligent and know that one leaf inside of a cell does not mark it as forbidden so we're gonna say true on precise we're gonna do this twice we're gonna add obj Rock say that's true and we're going to add in our walls that we already have so obj wall and this one I'm gonna set the false because I know that it's actually going to cover up every single square that it's in so we are still drawing it so let me press f5 and run and now all of a sudden we're gonna see that all of these are marked as forbidden and you can even see that some of them are marked as forbidden that don't even look like they're in there and that's because the bounding box is actually larger than what we see on there so if we come into the sprite and we open up this preview mask so it's covering up this entire thing and just part of that is actually touching the square so if we come in to automatic and we set this to precise and we run this let's see what that what happens next so you can see in here that now since this is precise it is now not touch those so some of them are a little little strange still but it gives you an idea that precise you can control exactly how close you want to get to that object so if that's something that you want in your game to be more precise that's how you would do it it requires more processing power just so that you know but if you're using it in a limited fashion than it can still definitely work free for what you are attempting to do with that so what's next is to actually set the obj guard to be running that path because we've created the grid we've added instances to it but now we need to actually say we are going to do something with that and it's actually very simple to do that all of this code is still in here so what I want to do is keep some of this so I'm going to comment out this stuff right here because I still want to create the goal that's how we're doing that but inside of a new if statement we're gonna say if instance exists I'm gonna blow this up a little bit so if instance exists over Jake you'll kind of the same thing we've got down here now we're going to say my path equals path add and we're gonna say MP grid path this is another MP grid function but it's using the path we're gonna call our room town grid we're gonna pass that in the path we want to actually set it up with that's why we have to dynamically create a path right there and we're going to say it starts at x and y which is the guards X&Y positions the X goal is still obj Gold X and obj gold Y and now we have the option to allow diagonal or not I'm gonna set that to true because it makes it look a lot smarter once you know when that when that happens if he can walk diagonally he looks a lot more intelligent than not then we're gonna say path start my path we're gonna give them a speed of three and action whose path action stop which you should be used to an absolute will also be true so now if we run this with just those few lines of code we come in here and we tell him to walk you can see that he is now moving I can readjust that path and he is going to dynamically go around these objects that we have marked as you know solid and not run into them now this is really slow let's jump up to the grid take off this draw event then we're going to come into the object guard and we're actually going to draw the current path that we are running on by saying draw self and then draw a path passing in my path and then x and y for the guard position and true for absolute and when we do this we can actually draw the exact path that he is going to follow except that of course he hasn't actually created that path yet so we are going to say if my path doesn't be cool undefined draw that path and in the create event we're gonna say my path equals undefined okay you can see here that he moves very quickly and he gets around but he is avoiding all of these rocks and the trees and he's avoiding them by even going to agonal and going right around them and finding the quickest path possible so this makes him look fairly smart fairly intelligent except when he gets stuck on a wall of course and that is because he actually has a collision with the wall which we want to take out which has been left in there because if he has collisions with things he's going to get very close to them and in the grid we don't want that to cause an issue like it did right there so now he is going to walk by it just fine and he can find his way around all of the trees all the rocks all of the houses that Emily as he moves so using a grid system gives you some advantages it allows you to have smarter AI they can move diagonally you can have more path options and you still get to use the path functions which gives you all of those built-in variables that we are used to having and this is the smarter system if you're going for more advanced AI that can do more things because you can add all of these instances to them and it gives you a lot more flexibility because there are a lot of functions that we didn't even go over if I open this up you can see that go back to motion planning you have all of these functions that you can also throw in there you can dynamically add cells and rectangles into your grid that would not be allowed for the player to pass through like if you knew that something was going to appear at 200 and 300 X & Y position you can create a rectangle around that and says that the player cannot pass through it you can clear the grid whenever you need to or just a specific cell or rectangle and you can also pass it into a deist grid if you ever need to access that data through not just an MP grid so it allows you a lot of flexibility and a lot of power but remember that creating AI isn't about having more complexity it's about having the best system for what your game needs if your game needs a grid that I can utilize these functions and can be better for it then definitely use them if not go with the simplest function that will yield the best result but that's great for you that is the three motion planning's that game maker studio has built-in that you can use and utilize to create AI and paths inside of your game hopefully you know which one you need in your game now and a little bit of how to use them if you have any questions or comments please leave them below or find me on Twitter or jump on my new website at beyond us Gamescom hop on the forum and just start asking questions there I look forward to hearing from you guys and talking with you more and as always have fun making great games and I'll talk to you later if you'd like to support me more than just liking and subscribing to my channel please consider supporting me on patreon like all of the awesome people on the screen right now they get to vote on upcoming tutorials and get one-on-one training sessions with me each month thank you very much and I will talk to you [Music]
Info
Channel: Let's Learn This Together
Views: 8,272
Rating: undefined out of 5
Keywords: Game Making, Making Games, Game Maker, Game Maker Studio, Beyond Us Games, Game Maker Tutorial, Game, Maker, Studio, Tutorial, Training, Learn Game Maker Studio, Learn Game Maker, Learn Game Making, Grid, Grid functions, mp grids, motion planning, how to use grids, how to use motion planning, Gamemaker studio 2 tutorial, learning game maker studios, game maker projects, Game Maker Studio Grids, Game Maker Studio Path, grid functionality, Game Maker Navigation, gms tutorial, GMS
Id: xfkryP7Ekd8
Channel Id: undefined
Length: 18min 24sec (1104 seconds)
Published: Mon Aug 28 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.