Creating an A* Pathfinding System in Godot (Devlog + Demo Pt.2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi this is codelagamorph welcome to my vlog last demo part 2. here i'm going to show you how to create a pathfinding system using an astar search algorithm to be implemented in a 2d top-down rpg game project i created this template with a demo so if ever you need also to create a pathfinding system in kudo this might help you now the reason why i want to implement the star search algorithm to making projects so that matter character and supposed to be moving in pieces in the game that i'm making can avoid obstacles and will always choose the best part in reaching its destination with the smallest cost in terms of distance and time so let's go first i'm gonna show you how the final output of this demo will look like so here it is so you can see the player character walks only in the brown tiles which is the ground reaching its target position and it avoids the green spots so as well that by standing in pc so these are the things you should expect for this demo now let's create new project and name it as a star demo alright now let's select the 2d scene for this is for 2d game and now let's rename this node 2d as main and we're going to set up now our project on project settings let us go to display tab and go to window and set the width and the height set the test width and the test height also for testing purposes leave use v-sync on and also the kit screen on the chart for orientation i will select landscape hide home indicator on stretch mode select 2d in the aspect i'll select expand and leave shrink to one and let us go to input devices tab from pointing let us check the emulate touch from mouse on now we're done now i will copy this styles and sprite and put it inside the folder where our demo project is located so that we can import it properly now we are going to add a child node of the main and select the white sword so we can sort in harmony everything inside and now let's add a child node for y sort and node 2d again and rename it as pathfinding another let's add kinematic body 2d for our player character i'm gonna name it player and let's add a child node here a sprite another one is a collision shape 3d and now in the sprite let's drag this player character sprite but before that let's go to the import tab let's set the preset to 2d we import set it as default for texture reimport and don't forget to uncheck the filter and finally import so do the same to each sprites and also for your tile sets all right now we're done it's time to drag the player character in the sprite node in the texture and select a collision shape but we're going to check the disabled tab for we are not going to use the collision shape in our a-star pathfinding system so now let us add a child node for the pathfinding and select a tile map we are going to rename this as ground add also another one for the restriction and we're calling it restriction we're gonna need also a sprite node for the cursor we're gonna call it as cursor and in the player node let's put a timer and then in pathfinding node we also need another node 2d naming it as a grid and in player node again we need also a line 2d we're gonna call it that line alright and save this as main.tps again now let's set up the ground tile map just go to tileset click the plus sign below now add the sample tiles let's select new single tile and click this hashtag looking symbol here and click the sample tiles and let's set the step of 16 by 16 since my sample tiles are 16 by 16 pixels and then click again the sample tiles and i will select only the tile with the brown color now it's done just click the ground tile mop so you can see it's not in appropriate tiling in order to fix this just go to the cell and set the size into 16 by 16 as well there you go let's do the same on the restriction tile map okay so we're done let's do some tiling take note the ground tails must fill all the area of your game and don't leave any blank space alright for this will be the basis for creating your a star pathfinding system now for the restriction tells it should not be seen in the outside of the ground tiles and every element of the game should only be seen inside the ground tesla right do do after we're done tiling the restriction let's create a script for the pathfinding saving it as a pathfinding that gt just to remove these let's start from the clearing variables by the way my game is to be exported in android mobile device so this demo is expected to work for android mobile device okay do do do do do now let's set the ready function and a process function then i will create a new function let's call it create navigation map getting a variable tile map do and this will be called when the pathfinding node is ready and also it should get the ground variable now let's fill some script in the create navigation map function do do do now we're gonna create also another function let us call it irreversible tiles getting an array type variable called tiles from the create navigation map function okay and also let's create a function called connect reversible tiles getting also the tiles now these two functions we're gonna calling it inside the create navigation map function do and now let's put some code in the ad reversible tiles function do do do and we're gonna need to create another function calling it get id for point to pass on the variable called id in the add reversible tiles function do do do now let's add an export boolean type variable should draw tile for debugging purposes later do do do do do do another thing we need to declare an export variable for color let's call this enabled color and also let's declare also for disabled color okay and now let's set up the color in the pathfinding node let's select a color for the enabled color variable and for disabled color variable go back to the script do do and now we are going to put some code in the connect reversible tiles function do do do do do do do do do do do by the way this code here and the other one below is not for diagonal but finding method i choose the straight pathfinding method so it will not take shortcuts in taking a path i will just write a note here okay do now it's time to set the obstacles let us go to the player node and node tab click the groups and write obstacles then add because the player is one of the obstacles for the supposed to be moving in pieces in my game and now let's create a node for our pc a kinematic body 2d naming it in peace and add the child node spray and also a collision shape 2d now clicking the inspector and then the sprite node let's drag a sample in pieces right here just hold the alt key to drag it in any position i will just drag it here now we're gonna change a little bit of its color appearance so it will not confuse us between the player and then pc sample sprite okay then let's put a collision shape but again we don't need this to be active so we will just disable this and now let's set also then pc as obstacle okay done going back to the pathfinding script let's put some code now here and get id for point function do and now we're going to create another function we're calling it get new path do and it should return an array and let's fill some code here do [Music] do do do do do do do do do now let's add a script to our player node saving it as player.gd and now let's declare variables do do do do do let's make a ready function and then an unhandled input function do also let's create a process function add also some code here do and now in pathfinding script let's just put a class name but finding so we can easily access it to the other node going back to the player script process function do do just leaving a comment here do do now we're going to create another function calling it set path line do do do do do [Music] do so this can be called out in the process function do do do now we need to declare an expert variable for a caller let's make for valid tile and the other one is for the invalid tile and after that just click the player node go to inspector and let's select color for the valid tile and also for the invalid 10 then just go back to the player script let's continue to code in process function now if you notice i do not elaborate every code that i write in every script because i wanted you to explore and analyze it better for your own self how code works all right and that would be a good practice when you inspect codes with diligency and deep analyzation do do do do do do do do now let us go to timer node let's set the wait time to 0.1 then click the node tab and then the signal stub we need to call this timeout signal in our player script let's connect it then let's wire it up with some code do now we're almost done and wait i almost forgot something let's just create another function here in pathfinding script for updating purpose of the navigation map and let us gonna call this update navigation map function and adding some code here okay do do oh i also forgot that this restriction tile map is included as an obstacle also so we need to add this in the group of obstacles okay now let's continue to fill the code back here in the update navigation map function in the pathfinding script do do [Music] do do do do do do do do now we're done this thing should run now oopsie nevstars did not work so i still forgot something here first i need to save branches scene the pathfinding node the player node and also then pc node do let's try to rerun the demo project and i need to transfer the position of this player character here so it will not stay in the edge when i run that test and let's do some recheck from the script oh i see i forgot to call the update navigation map function here in the process function in the pathfinding script now let's do some test run again oh so that works but the cursor is not appearing when we click a certain area in the game screen so let's figure it out do do did i forgot to declare the variable for the cursor i already declared it it's not the issue here do do do maybe it's not in the cold because i did not encounter an error when i execute the test run i'm not sure but let's see do oh that is why i forgot to put a sprite for the cursor in the texture now let's just use this good old icon as our cursor let's just resize this the same size as our sample tile all right now let's check wait i should hide it first and let's run it again oh it's working now but the positioning isn't bad it needs to be fixed but first let us test our debugging disabled pal indicator and navigation indicator to see if our a-star pathfinding system works well the disabled tail indicator is not appearing also i need to do some recheck again do do do do do do [Music] do do my mistake again i accidentally hide the grid node that's why the disabled tiles are not appearing in the screen but it's okay now so it's time now to apply the right positioning of the cursor to be sync with the player character positioning and let us also reposition the npc to be properly synced with our player character now let's check do maybe a little more push to the right now it is centered and the cursor works well and the disabled tile is displaying for the debug purpose let us also check if the path line is working well also okay it's fine but we need to minus the width let's just put two only okay now it is great we have now a working a star butt finding system so let us set things up in proper now and do the last test run oops their section was still hidden let's go to the main and unhide the restriction and we're ready to go so that's it for this development demo i hope you will find this helpful and lastly let's try to export this and my android mobile device if it still works do do now we're then exporting it to an apk file let's just copy this and install in my phone now it's already installed in my phone let's open this and see [Music] thanks for watching you
Info
Channel: Code Lagomorph
Views: 1,343
Rating: undefined out of 5
Keywords: made with godot, indie game, Christian-based game, a-star pathfinding system
Id: xlrviIQQBmM
Channel Id: undefined
Length: 90min 5sec (5405 seconds)
Published: Wed Sep 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.