The Basics of Paths in GameMaker Studio 2 - Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Laughter] greetings and welcome my name is Aaron Craig with beyond this games and in this video we are going to be talking about the basics of paths inside of game maker studio now this is going to work for game MCUs video 1 and 2 so follow along in whichever software that you are using now paths are way of getting an npc an object of any kind to follow along a path in that path you can create an editor or you can create it dynamically as your game is running through code both of them allow you to have an npc follow a path you can change that path as it's running you can reshape it you can reassign it there's a lot of functions that you can do and paths can be very useful if you know how to use them they are the building block for doing much more advanced things such as motion planning and grid based a star algorithms so we're going to look at the basics of them so you know how to use them and start them and stop them and then we will in another video get them to more advanced topics but let's dive into it now if you want to copy along with me I have the project for you in the description below so you don't have to worry about setting up a tile set animating a character all that's already in there so go ahead and download that in the description and follow along the first thing we're going to do is look at some of the built-in variables that a object has that deals with paths and the functions that are associated with them now these variables are built into every instance in your game you have the index that it's assigned to the position along the path you have position previous speed scale orientation and end action all of those are useful and if you need to get access from them or get information from them you can just by typing them in to start a path is actually very simple you have path start and that's it to end a path you have path end and so let's get started go into our workspace and we are going to create an object so we're going to call this obj guard we're going to assign a sprite to it and we're going to add a create event now before we type any code in here we are going to go over to the resources here and right click on create a path we're going to do this in the editor not through code dynamically so we're going to name this patrol path one and this is the path editor window now this isn't very useful I can make a pass in here but I have no idea where in the room it actually is going to relate I can see these grid lines and I know the X&Y coordinates but that's pretty hard to actually do properly so instead what I tend to do is open up the room come over here and create a new path letter and you can assign this path any name you want we'll just call the exact same thing and then you select your path right here and you can actually start clicking on here to make a path now by default it snaps to the grid at 32 by 32 but I want it to be 8 by 8 because my tile set is fairly small so I'm going to move this and I'm going to recreate the grid that we saw in the beginning just like that it is super simple to set up a path because now it will start here and it will end here and these are all the vertices the indexes in between them so the options to change your paths are right here you've got color so you can change the color of the path you can do straight lines or smooth curve it looks kind of like this when you do smooth curve you can also close a path so that means that it's going to from the end take it all the way back to the beginning somehow let me actually change this back to white so you can see a little better and then you have precision as well now precision it only matters when it's on a smooth curve so I might change this to 1 you can see that the precision is much more precise and if you could jump up to 8 well it's a little more curvy now I'm going to do a straight line here and I'm not going to have it be closed so that is the path inside of our room you can have as many paths as you want we're just going to set up one and then inside of our object inside the create event all we have to do is save half start we're going to assign it the path patrol paths one we're going to give it in speed of two and end action which there are four options you can do continue if you have a closed path when it gets back to the beginning it will just continue along the path again you can restart which will take the object that is following the path and at the end of the path it will warp it back to the beginning instantly you can reverse a little follow along the path but reversed and you can stop we are going to select reverse and the last option here is for absolute we are going to put true in here and then I'm going to give you an example of what that means you have absolute or not absolute inside of our room let's click on the instance layer and then drag in our obj guard I set the path follow to absolute that means that this guard is going to follow the path exactly where it is in the room along with x and y coordinates that means even if he starts right here he is going to be instantly warped to here when that function begins and then he's going to follow this path just as it is in the room if it was not absolute instead he would start following this path wherever he was at so if he would be right here that means that he would go down aways over aways down a lot and then over again but he would be able to go off the screen run into objects that you weren't expecting because he is following that path not absolutely he is following it but where he is at relative to the room now that's actually all the code we need we press f5 and it will run and then he is going to follow this path that function starts it and is really what you need to do very simple basic paths obviously there are a lot of other functions associated with the path so if we come over here and I type path underscore you can see those built-in variables we talked about there are the constants which are the path actions and then you have all of these functions that you can do pass add add point append all of these are for dynamically creating paths inside of your game through code the other important one is past end so if I were to want it to stop an object following a path at any time you would need to say path end you can also say half speed equal to zero if at any time you wanted that object just pause along the path and then you can resume the speed at any time that you would want to by putting it back to its original one or to a different speed you can also increase and decrease the speed anytime you want the only thing is path index is a read only variable this tells the objects which path it is assigned to and you cannot assign an object a path through path index it must be done through path start all of the other built-in variables you can get you can read and write to those so path position is another very important one this is actually going to return something between 0 and 1 and that value is going to be how far along the object is in the past so if we open up our room here if he was right that's a right about here he would be probably right about 0.5 maybe a little bit less along that path his past position would be 0.5 that means that he is halfway through the path and when he comes back over here he would be 1 he would be all the way done with the past so you can read path position but you can also set path position if he is starting the path and you want him to start the path absolutely but be half way through it already you can set path position right after path start which it would work and that actually covers the basics of path now if we run our game again compared to what you saw in the beginning you'll notice that our guard isn't changing his sprite based on the direction he's walking the cool thing about using paths is that you then have access to the built-in variables like direction which is being set because game maker is controlling the speed and the movement of the player we can use direction to our advantage if all you want to know about his past this is a good place to stop I'm going to show you kind of a cool way that you can set up your objects to change their sprites based on the way that they are walking so we're going to add a step event here and let me explain this before we go along we're going to be using the built in direction variable but direction in game maker studio it goes as follows so you have zero degrees being right 90 being up 180 being left in 270 being down so what you may think would make sense is something like this you say if direction is greater than or equal to zero and direction is less than or equal to 90 then you would say something like sprite index equals SPR guard block right now guard walk right is in the sprite I have well Jeff that in just a second but this is actually not going to work very well for you because even though 0 is directly right if your player inside of your room is moving like this where it's down a little bit but still mostly to the right where it makes sense for his sprite to be looking right if you're using 0 to 90 and in 90 degree increments you are going to then when he's like this we have him facing down and it's going to look very awkward and we don't want that so instead I've taken the time to figure out a fairly good system of numbers that you can plug in here and have your sprite while he's following a path or doing really anything at all has that have that object update his sprite index accordingly and the way to do that is through this very crudely drawn sketch pad right here so let me explain what it is 0 90 180 and 270 are the four directions so you've got right up left and down now these little I'm cutting material : they are the area that makes the most sense for the sprite to be turned when they are walking back direction so if he is walking to the right but he's going down a bit or up a little bit it makes the most sense for him to be facing right if he is going up but he's a little bit to the right or left then he will still be facing up and so on and so forth so these numbers right here are the actual values that to me I found work very well if you want your object to update their sprite and look naturally the only one that is different than the others is the first one so let me let code that right now so I can show it to you so this is actually going to be if Direction is greater than or equal to 306 and instead of an and we're not putting an and we're actually going to put or Direction is less than or equal to 45 sprite index equals SV our guard walk left and the reason that it's left is that we are going to take the image X scale and turn that around on itself so it's going to mirror itself along the x axis which will actually turn it to the right now the reason this is or is because when we are walking to the right when we're moving in the right direction we need it to be anything greater than this or less than 45 for the other ones it's going to look like this if Direction is greater than or equal to 26 and Direction is less than or equal to 135 and is the proper thing here to ampersand stands for and that is correct because we need it to be in between those numbers there is no number that is greater than 306 and less than 45 so we needed or right there so with that let's just type out the rest of this and I'll show you that then our guard is going to look right as he is walking around so I've got the numbers on another screen so let me just type those in so if Direction is greater than 136 and Direction is less than okay side less than or equal to 225 right index equal SPR guard lock left and image X scale equals one and the reason we're putting in that image x scale every single time is that we need it to reset itself because it might be negative one if we were just walking to the right so we don't want it to look weird okay let's press f5 run this and watch with our guard objects now we'll change his sprite based on the direction that he is walking and that just gives your AI a much smarter field and helps it to not stick out like a sore thumb as he is moving along that path so that's basic paths for you inside of game maker studio just using path start there are a lot of limitations such as if you put a solid object in front of that object that has fallen in the path it is going to run into it and just stay running into it for all of eternity it will never reach the end there are smart paths which is what we're going to look at in the next video we're going to look at potential steps that they can look at and say am i running into a solid object or a specific object and then how do I get around that so that will be the topic of our next video which I hope you will join me for in more advanced passing techniques thank you very much for joining me for this one I hope that you learned and as always have fun making great games and I will 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: 23,289
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, Path, Paths, Artificial Intelligence, How to use paths, gamemaker path, gamemaker studio path, learn to use path, pathing, ai path, ai pathing, motion planning, mp_path functions, game making projects, video games, game design, gamemaker studio, game maker paths, Learning Game Maker
Id: LYZRGEYZEHI
Channel Id: undefined
Length: 15min 27sec (927 seconds)
Published: Sat Aug 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.