Simplified 2D Grid PATHFINDING in Godot 4.2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video I'm going to show you how to quickly set up pathf finding with AAR grid Tod on a tile map now to easily begin this project I created already the ti map and the camera 2D and on This Ti map we are going to set up AAR grid Tod I'm going to Simply create a new script set load and let's say here bar AAR equals to AAR read Tod do new and this AAR grid Tod is a feature of godo 4.2 which is basically going to allow us to define a region on which an AAR algorithm can Define paths now the AAR grid Tod also has some properties one of the properties is a star. region so let's simply say a star. region and we see if we click on it that this is going to be a rect to I now we want this region to basically cover the whole TI map how do we do this well we simply go back to our script and we need to get the surface area of our tile map so let's simply say VAR tile Map size equals to get used re do end minus get used W do position what this does is basically it says create a size that is defined by the rectangle end which is here and the rectangle and the rectangle position which is here now that we have this size we just simply need to convert it to a rectangle so I'm simply going to create a variable VAR map re which is going to be equal to re to I and we see that this has in the Constructor a position from where the rectangle should start and also a size so the position is going to be Vector 2 I do zero and the size is going to be obviously C Map size now that we have that we can simply assign it to our AAR region the next property is going to be AAR do cell size and this is going to basically be equal to the size of a tile now to access the size of the tile we need to access the tile set so let's just say VAR tile size is going to be equal to what well to get tile set and this tile set has the property tile size okay we simply set the a star to cell size and we can move on now we are going to need to apply some heuristic for both computation and estimation so let's say AAR do default compute fistic and this is going to be one of these heuristics and these whole heuristics behave differently and depending on what type of movement you want in your game then you can choose any of these but I'm going to Simply choose Manhattan because this has more rectangular movement and again I'm going to do the same for a star. default estimate heretic this is going to be Manhattan again now we don't want to walk diagonally at all so I'm going to say AAR do diagonal mode and have it set to AAR grid Tod and diagonal mode never okay having this if we click on as. region we see that this the region of grid cells available for p finding if changed update needs to be called so we are simply going to comply and let's say a star. update okay now we have basically set a grid here on our TI map now we also need to have some way of defining where we have a wall and where we have walkable space in order to do that we need to go to our T map and to add a custom data for our TI set so if we click on T set we can see here that we have custom data layers we click on that and we can add some data let's call it type because we referring to the type of wall we have here and let's make it a simple string now if we go to our tile set maybe let's make this larger you see that we can select here the custom data we can select type let's call this wall so the type is going to be set as wall let's basically go over anything that we consider to be a wall so let's say these are walls and also these are walls and maybe these are walls as well okay now that we know everything that is going to be considered a wall we can go back to our script and we can Define that now how are we going to do that well a star has a property called set point solid so wherever we have a ball we are simply going to set a solid let's simply iterate over every cell in our T map so let's go for I in t Map size. X for J in F Map size doy and let's simply say here that VAR chords so the current coordinates are going to be a vector to I between I and J and in order to get the data from the cell we are going to say VAR tile data equals to get cell stle data and the first parameter is the layer we know that we don't have other layers so it's going to be zero second parameter is going to be the coordinates so we have the chords here and now if we actually found something we can check if that something that we found is a wall and simply set it a solid so first of all if T data so basically if we found something and T data. get custom data and here we have layer name the layer that we set is basically the one that we have in here in the custom data layer so it's type and the value of that layer is going to be wall so what we do is go back to our script and let's simply say here that if TI data. get custom data of type equals to wall then we are happy and we are going to set a solid here a star. set point solid and where do we want to set that point solid at the current coordinates that we founded okay now that we are done with this we can go and create a character so let's simply go to add child node and let's select character body Tod I'm going to create it and the character body Tod is going to need a Sprite Tod create it as well and another note that we are going to add is the Collision shape to the and maybe let's take a look I'm going to set the shape to a simple rectangle so let's have rectangle shape here and let's add this something to the Sprite now the Sprite is a bit blurry so I'm going to say texture and go to nearest and now we can see clearly our pixels we can log these for now and let's move our character body here and we see that it's a bit too big so let's try to change that a little let's see if we go to our Sprite to D we can transform it to make it a bit smaller 0.5 maybe that's too small 0.7 yeah this looks fine let's make the Collision shape the same size as well the Collision shape is going to be like this we don't really need it but yeah I I think it's it's fine like this now we can take this and move it a bit higher something like this doesn't matter too much it's going to snap anyway so let's continue to add also a script to character body to the now what do we want to do with this character we want to move to the mouse position whenever we click somewhere so in order to do that we first need to reference our TI map because that is going to define the path that the character is going to follow so let's simply say on ready our TI map let's set this to be equal to our actual TI map I'm dragging and dropping it here and let's say a variable for current path which is basically going to be an array of Vector 2 I and this current path will represent the path that uh uh the character will follow in order to get to our Mouse position now we're not going to need our ready function we're only going to need the process and a way to handle our clicks so let's say funk unhandled input event and here whenever we have an unhandled input we are going to find the click position so let's say our click position equals to get Global Mouse position and now that we got the global Mouse position we want to see if we're able to move to that position because we do not want our character to move inside a wall for example so we are going to verify if the current position is not a wall now in order to do that let's go back to our Tel map and let's create here another script and let's simply call it HK is point walkable and let's just Define some position and this position is going to be some position clicked on the screen but we want this position to be relative to our map so let's Simply Save Our map position which will be equal to local to map and let's have here position and now we want to see see if the rectangle that defines this style map actually contains that position so we're not clicking somewhere outside of this and we also want to see that we're not clicking on a wall now for that we need to have access to the map rectangle but we already have it here so I'm going to make it a global variable let's say simply bar map rect and let's initialize it to a rect to I and now here we can simply say if map re do has point so it basically if it has the point map position and this point is not a wall so not a star dot is point solid of this coordinate and we are going to return true okay otherwise we return false now what's wrong here no nothing is wrong okay now we can go back to our character body today and make this verification now what we can do is to check if an action has been pressed so let's say if event dot is action pressed and we have not defined our action but we can go to project project settings and here let's add a new action let's say move two and this action is going to be defined by the left click clicked there and it automatically filled into the left Mouse button I'm clicking okay and now I can say move to here so if basically we clicked [Music] somewhere we verify if that point is walkable so if T map dot is point loable of the position then we are simply going to update our path so this current path Vector so simply say current path equals to what equals to TI map. AAR dot get ID path and this get ID path basically gets as the first parameter the initial point in which we are and as the last parameter the point in which we want to arrive so the initial point is going to be the current Global position local to the T map so T map do local to map Global position and the next point is going to be again local to the map but it's going to be the click position so we set the click position here and what is this going to return so this function is basically going to return an array which basically returns all position from the global position to click position obviously following the AAR algorithm now for example we could have the first position to be 1 one the next position to be one two third position to be 1 three then to have something like 2 three and so on until the last position this also includes the initial position so we do not want that so we are going to Simply say slide one which basically gets everything from the element on index one forward and we are going to return this array okay now that we are done with handling the input we only have to move to that path and how do we do it well first of all if the current path is not yet defined so if current path is empty we can't do anything we don't have anywhere to go so we are simply going to return otherwise if we have something on the current path we want to move through each element of that current path so basically if we want to go here we would go through this cell and this cell and this cell and this cell now to do that we are simply going to look in our current path array and let's define our next Target and this next Target is going to equal to current path dot front because we want as we had before we want to get the element in the front and one other thing we have to take into account is also that the current path contains the position relative to the ti map so basically this is going to be z0 this is 01 and 1 one and we need those to be local positions so in order to convert them we simply say t map do map to local and we add this as a parameter and now that we have the next position we simply have to go to the next position so let's simply say Global position equals to Global position do move towards and let's say the target position and let's give it a speed something five it doesn't matter too much and now we have to remove that position from our list once we reach it so if I say if Global position equals to Target position then we simply remove it so we say current p. pop front Okay now that we also remove this our code is pretty much done if we run it we can see that if I click here our little character is going to follow us and it's going to take the shortest path possible to get to us and it's also avoiding this obstacles well I hope this has been helpful to you and hope to see you in the next one bye-bye
Info
Channel: Cashew OldDew
Views: 8,027
Rating: undefined out of 5
Keywords: godot, 4.2, game development, astar, astar grid, godot pathfinding, a*, algorithm, simple, AI, GDScript, 2D, Tutorial, Pathfinding, pathfinding, godot engine, godot 4, godot 4.0, Learning Godot, Godot Engine 4.0 Tutorial, Godot Engine Tutorial, Gamedev Tips, Game Design, Programming, 2D Game Development, Coding Tutorial, Indie Game Development, Game Dev Tutorial, Godot Tutorial, Tilemap, AStar, Game Programming, Game Development
Id: OMrDS0zlr-k
Channel Id: undefined
Length: 16min 28sec (988 seconds)
Published: Tue Jan 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.