Advanced Paths Functions in GameMaker Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Laughter] greetings and welcome my name is Aaron Greg with beyond us games and in this video we're going to be looking at the more advanced paths functions that are inside of game maker studio one and two so what we are going to be doing is picking up exactly where we left off in the last project I've actually downloaded it and I've got it running here in the background so follow along with me there if you want to copy along with the code but what we're gonna create is a guard that we were using last time and when we left click we're gonna create a goal for him to access and he is going to walk towards that goal and avoid obstacles to the best of his ability and you can see here that these walls are solid and he is going to find his way around and this is actually quite simple code to get up and running really quickly these functions are quite powerful and that give you a lot of flexibility if you use the right ones so let's dive into that now the first thing I want to do though is make the sprites we need so we can get that out of the way we're gonna create a SP our goal that little yellow box I had I'm gonna fill that in right here go get over to that help in just a second and then I'm going to make this a 16 by 16 because I don't want it to be too large and we're going to create one more sprite call it SP our wall well edit that and we're just going to throw in some text that is blue and just say that it is a wall so that we can know it is now the thing to note here is that your collision mask needs to be set to mode full image it wants to just wrap the collision mask around the text which is going to look really strange and not do exactly what we want when we put it in our level now let's make those objects as well so we need to obj goal assign the sprite it doesn't need any code same thing for the wall doesn't need any code except that we say that it is solid and then inside of our room make sure you're on the instances tab or the layer and let's just put in some of these wall so that we can see that it is working I'm not going to spend a lot of time on this because it's not too complicated or difficult to see how it actually works but if we set these up then you get an idea that our functions are actually working properly and how we want them to so that's all I want to do for that next we're gonna come into our workspace into our obj guard and press f12 and we're gonna set up the code to left-click and create our obj goal before we get into anything else because this doesn't really have to do with the paths functions this is just so that we can easily create new goals and have him move around just on the fly so we don't have to reload the game every time we're gonna do that through if Mouse check button pressed this is going to be M be left just like accepting keyboard input actually then we're gonna say if instance exists obj goal we're gonna say instance destroy obj goal now if you're using game maker studio 1 this will look just a tiny bit different you'll need to say something like with obj goal and then you'll need to say instance destroyed without that because gaming studio one doesn't have the ability to actually say instance destroy and pass in an argument but game a construed you to does so that's what I'm going to show on screen right here so we don't want any goals on their screen after we click except for the one we want so we don't want any extras on there so we're gonna create instance create layer and again get make sure you one's gonna be slightly different just in the sense that you don't need this extra part it's a lot easier in game maker studio one to make new objects actually so we are gonna say we're gonna make it at Mouse X Mouse Y the layer that we are currently on which is just layer and obj goal so we're going to create that make sure that it's the only one and then when we left click it show up and then we can do things with that so that is the basics of that let me press f5 and run it just to make sure the system is up and running our guard will continue along the path that he's on because we haven't changed that yet but if I left click okay perfect up and running now we're going to come in here to the create event and comment that out because we don't actually want him running that path anymore this path that we originally had set up is no longer going to be used so we can delete it if we want or keep it totally up to you now let's go into the help manual and look at the motion planning functions these are the more advanced ones and some of them are not paths you can see here that there's motion planning steps and paths and we have linear and potential down here is grid and that is what I'm gonna cover next because it's much more advanced and it requires a little more coding a lot more effort but it gives you a lot more in return as well so if you want to learn about grids jump on to the next video but for simple but effective path finding AI that just takes a tiny bit of code these are fantastic functions linear is going to be a one-way straight line trying to find the path it sells you that if you click on it right here this MP linear step it doesn't actually use paths but I'm still going to cover it in this path this video this function does not try to make detours it just tries it'll just fail and stop moving it'll tell you if it can reach the position or not though it's going to before it even starts moving figure out if it can make it and then it will tell you if it's going to succeed or not so that's not exactly what we want these linear functions can be useful especially if you're shooting like a bullet or a fire ball in the game you can tell it to go straight ahead and it will tell you if you can get there or if it like collides with something then you can do that but these are not that useful but let me show you exactly how they work so if we do MP linear step all we need is that function inside of the step event so if we come inside of here and all we do is say MP linear step let me just bring this up here MP linear step just requires four arguments so we're gonna say obj goal X o ridicul dot Y a speed of two and check all that is specifically if you want to check all instances or just ones that are solid and you put false if you just want the solid which is what we want I'm gonna wrap this in an if statement that says if instance exists obj goal then do that otherwise you're gonna get an error right away if you try to run this and I forgot one bracket okay so let's actually just press f5 and I'll show you this linear function it's super simple but it works it's gonna go straight to where it is but it's not gonna avoid any path it's just going to get stuck right there on that wall that we put on that tree you can tell it to then go somewhere else but it doesn't it's not what we want it's not bad and it's useful to know it exists which is why I'm showing you but it's not what we're looking for so instead we're gonna back out of here and instead go down here MP linear path and objects we'll get to that I'm gonna explain it in potential step because those are the ones we're going to be using so MP potential step is very similar to linear except that it's actually going to go a round objects intelligently and it works exactly the same so if we actually come down here and let me do that so you can see it but we're gonna say MP not linear potential step and we're actually going to say the exact same thing so obj gold x over giggle dot y speed of two and false and if we run that we're gonna get the exact same thing but now he's gonna avoid obstacles so if I click right here he's gonna know that there's a wall there because it's flagged as solid and go around it which is great this is one line of inside of your step of it and he moves you can see it's not perfect he's getting a little stuck because he's trying to figure out if he can go through there and he can't but he gets there eventually and that's just one line of code technically too since we have to use this instance exists but if you're in a path that I mean you don't even need a path actually that's part of the thing is right here this is not technically a path function because all it's doing is it is potentially taking a step and if it hits a solid or an instance that it's not allowed to it says okay I can't go here I'm gonna find a new path and tries to take another step and another step another step until it finds a way to get there now if it can't reach its goal then it's just going to wander around aimlessly as close as it can to that goal like if it was inside of a solid wall it would just wander around that but this is a really good start for simple AI and you might think that this is all you need but I'm going to show you that you want to actually use the one right below this the MP potential path MP potential path uses the paths that we've got we've used and talked about before and the nice thing about that is is it gives you all of those built-in variables you can see that when this guy was running we didn't have any knowledge about what he was doing we knew where he was going and how fast because we said it but that was about it we had no idea if he was close to it we'd have to write our own function for that or use like distance to object but there's a lot of information that is not easily accessible because this is not a path and we talked about all of the built-in variables that paths have last video and to access those we just want to use a path but continue to use this potential because it avoids obstacles automatically so if we come into here this NP potential path is what we're actually going to use so it needs a step size and a factor as well as a path right here so it requires a little bit more and I'll explain those as we use them so I'm gonna go ahead and that out as well and now we're actually going to come up here because we actually want to do it just once this is going to be a path and when you start a path your character will continue to go along that for as long as the path is going so all we need to do here is create a path and we're gonna go ahead and make this a far my path and we're gonna set that equal to path add so we need a path to pass in to this potential path function and we can actually just create that ourselves right here that's perfectly fine then we're gonna call that function MP potential path we're gonna pass in the variable we just made the goal is gonna be the exact same let me open this up so I don't off the screen now step size is again kind of the same thing as speed it's how fast you want to be going this factor though is a little tricky so they explain it as it needs to be larger than one otherwise you're gonna get an error and four is what they suggest for a good size but the larger it is the more path is going to look to see that it can take so if the path that you think your AI is gonna be taking requires a lot of a lot of detours that it's that it can't go straight at it you you're gonna want a larger number here a smaller number means that the path is actually going to be fairly straight forward it doesn't need to expand how much it's looking outside of that kind of linear path the larger number it is the more ideas that we'll have of how to get there but if you make it too large it might just go on forever so you need to be very careful so with that in mind we're just gonna set it to 4 as that is what they suggest and again check all we're going to set to false because we don't want it to avoid all instances just the ones that are solid now this will be a good time to point out that this is the big difference between MP potential and the object paths so right here you can see that you need an object to block the path so this is if you just want it to look for a specific object in your game like a wall or something like that then this will avoid that object we are going to be doing the just the potential path and just avoiding all solids but again you have the object you have the potential if you want to do object functions instead to just avoid simple one objects in your game which can be useful it's good to know they exist so we've got potential path now all we have to do is say path start with my path a speed of 2 give it a path end action so this is just gonna say stop as soon as he gets there and it's going to be true which is perfectly fine because it's actually starting where he is and then ending where we're telling him to go with that and with that right there he's actually well soon as we comment out that malformed if statement that is he is going to dynamically avoid the paths now you might be thinking well ok he was already doing that before right but let me show you the difference if we come down here and I say show debug message half position now all of a sudden because he is on a path we have access to all of those variables that are built into paths so that we know where he is where he was where he's going how far along the path that he is at we have access to all of those because now he is on a path so you can say if he is halfway to the path pause activated cutscene if he is 3/4 of the path have him fire out of fireball you now have all of this built-in functionality like we talked about last time you have all of these built-in variables that you can now use to then change dynamically the path that your AI is running on so that's what I would recommend using it's it's a couple more lines of code you can see here you have to create a path and then tell it to start as opposed to MP potential step is just literally one line of code but the path gives you more flexibility and more knowledge about how your AI is actually functioning as they are moving along that path but those are the advanced path functions that gaming car offers they are useful and they're great for getting your a I setup and for some instances that's all that you need if you don't need super smart AI then don't add it complicated AI is not good AI by itself so if you can get away with simple then I would recommend you do that and use these functions to the best of your ability to make the best games you can but that's all that I've got for you so thank you very much for joining me I hope that you've learned something 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: 15,551
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, how to use paths, GMS 1, GMS 2, advanced paths, path functions, how paths work, gamemaker studio pathing, mp_path functions, motion planning, game maker paths, gamemaker studio 2 tutorial, game maker projects, learning game maker, game maker AI, game maker path finding
Id: jlj3i6_bpPg
Channel Id: undefined
Length: 17min 21sec (1041 seconds)
Published: Thu Aug 24 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.