simple and effective way to path finding in game maker 2.3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're gonna talk about how you can implement pathfinding like this so this this red square is the player and whenever you click somewhere it will find the path for that you can see that there is a wall and there is a tree in here and the player can find its path to that position that you click so without any further ado let's begin [Music] i'm sorry in this video we're gonna talk about how you can implement pathfinding in your game maker project so for that let's just create a new project let's just call it have finding save and here we need some a sprite and object some sprites for obstacles like wall and tree and some object like player so let's just create them go to sprite and create first the sprite let's just call it spr wall changes dimension to 32 for simplicity but you can change the dimension whatever you want i will explain it how it is matter click apply and edit image for a while and this i want to use this color and that's it for a while ctrl b to duplicate duplicate it one more time and in here i want to call this tree edited it's for tree so let's just give it a green color let's name this wall this one is for player let's just change its color to red or player as a red color or enemy or whatever so we have three sprite one for player one for three and one for wall just close them though but before we close them uh we should anchor them on the middle so pathfinding will work greatly so middle center three middle center as well and for the player and wall it should be middle center as well so now that we have over a sprite let's just create object off of all of them so create object obg player and give it a sprite of player that we create this is duplicate duplicate player to time for wall and for tree so wall change its sprite to wall this object object layer 3 that we duplicated for 3 change its sprite to 3 so we didn't do anything till here you know all of this so let's just close them know that we create them now that we have our objects let's just go to room and create our room let's use this wall object to create our wall so after that let's just create some maze here so that is our maze let's just put some tree in some places to see if our player can find its fare its way around them now that's or mace and or player should be here so if we click in here our player should find a way to go through all of these and find its way to here where we click on left most button so before we move on let's just explain how pathfinding work in game maker so this is our player right and these walls and three are obstacles so game maker create a grid of this room and find every path possible for our player to reach its destination so for that we should create an object and create a grid in that object so right click on object and create an object in here let's just call it obg grid add a create event in here and what you should do you should just write mp grid create that's it now i want to show you the documentation for this grid so click on middle mouse so it will open the documentation for mp3 create you can read all of this but expl i will explain everything to you first input is left and top of the grid that we want to create so if we go to room we want to create a greet of all of this room so the top left corner is here zero and zero so let's just put that there zero and zero so let's see what else what it shows horizontal cells mean number of horizontal cells that the map grid will contain so how we can calculate that let's just go through to explain to you something in here to calculate how many cells there is in our room for horizontal cells or vertical cells so because we want to find a path for our player so the cell size should be the same as the size of this player that we have here so we should calculate how many player we can put on this room for that we can get the height and width of our player with cool sprite get wheat and this price that we want to find the v target is player just copy and play paste this for height as well player right yes i took this is the player width and player height we want to see how many of this player we can put on our room h cells we didn't use this because we don't want any float value so whenever you say div like this it don't it won't give you any floating value so let's just do it for vertical cells as well divided by player height and so let's just see it one more time h cells is the number of horizontal cells that the map grid will contain so we can use this in here and if we sell in here and the cell width and cell height you know that it is player read and player height player tweet and for sell height player height so this will create our grid let's put it on the on a variable so we can use it everywhere so let's just say global dot my grid and in example in here you can see it it will put it on the global variable so we can use that grid wherever we want after we did this we should define what objects are obstacles so player is not obstacles so how we can do that we can say mp grid add instances and for the id we should say global dot my grid you can go where you want and the object we wanna avoid object that is obstacles in our room so in here is obg wall and the last is the precise inputs let's just see what it is in documentation so you see the precise is like this and the knob for size is like this i always use not precise but depending on your project you can use precise it will be a slow you can just put true in here but it will be a slow you can test it on some emulators some phones and some windows to see if it's a slow just put it i'm not processed but it if it's not as slow just put it in precise in here i want to say false because i don't want it to be precise and we should do this for obg3 as well because the tree is obstacles as well so obg3 and that's all for our obg when we create this object you should go to room and add that all we agreed to our room in here and one more thing that you should consider you should go to room and in instance creation order in here you should put this obg grip at the above of everything now that we add this we can go to player and right code for that so for our player let's just create a global global left pressed event in here because of all of this comment and in here we said we want to say mp grid path so with this let's just go to documentation of this function to see what it does it use our grid to find a path for our player so for the first input is index of the mp grid that is to be used so we create that three global migrate and the path we should add a path first and then use it in here so let's just create a create event in here and create a path just to call it my tab so you should you know that you could you can call it once let's say path and that's it you create a path and you say path add and now you can use it in this pathfinding function have next input is x start and y start they're starting x coordinate of the new path so the start is always the each and one the destination that player we want to find the path for i want to say the where most heads press left button so you can say device mouse x and device y as you need here for the last input let's just go to documentation indicate whether diagonal moves are allowed instead of just horizontal or vertical in this case i want to say hello true because we want to have diagonal moves and what this function gives us it give us a boolean if it's true so we know that this function find find a path but if if it give us a false it means that it couldn't find any path so we can use that in a if a statement in here we say if this is true have start and if we go to documentation of this you see that the first input is path second speed and action and the absolute that is whether the calling instance should follow the absolute path as it is in the editor or a relative to its current position so i wanna yeah i wanna test both of these so you can see what is the difference so for the first or fourth first input the path is action half action stop so what is this five action desktop we can go to documentation and you can see all of this and action in here if we put this for the input it will stop at the end of when when it reached its destination if we put this action it's continue from the start position jumping to the store if the path is not closed so we wanted to stop and for absolute let's just put through here so know that everything is set up we can't play our game on here you can see that it has diagonal moves let's just click in here to see if it can find a path and you can see it avoid all all the walls and trees in its path that's cool let's just change this speed to something crazy like 20 and test some have finally in our maze cool so that works for everything one question that i that i know all of you have is when you have a player that have direction in it so you can't use path start for following the path that this function might be path finding fine for you so there is a way to do that but if i want to explain it in this video it takes so much time if you want to know how you can use the path that we find in here and separate it line by line and follow that path by yourself by your function move function that you have with direction with the speed or with whatever you have just leave it in comment i will explain it later thank you very much for watching please for motivating me to create more video like this just hit that subscribe button like button for bell button again thank you thank you thank you very much for watching bye [Music]
Info
Channel: navidrct
Views: 144
Rating: undefined out of 5
Keywords:
Id: TjLYSCy7Tm8
Channel Id: undefined
Length: 19min 40sec (1180 seconds)
Published: Mon Sep 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.