Roguelike - NPC grid based movement

Hi everyone I have asked this question before and I know others have too but have never come to a solution. I want to make a grid based roguelike but the enemy pathfinding moving one grid space at a time has become I think the most confusing task I’ve had and haven’t been able to figure out for the life of me and I think it’s just not understanding the given pathfinder system.People have recommended getting pathfinding nodes, would anyone be willing to walk through the steps and trying to figure out the system together? The issues I have mostly are jumping out of their tiles because of applied force or they become kind of dumber than a box of rocks trying not to use the pathfinding system. I would also like to use this for other entities though whether it be ranged combat with the arrow traveling across each tile or nova walking to a marker or places. Something similar to Astar pathfinding which I’ve figured out in Godot but not gdevelop. Any help would be appreciated but if anyone can help walk me through the steps what is events to use and how they work to make it work would be so so appreciated!
IMG_5573

(Attached picture is not my project but to represent what I’m tryi

Hi @BigBoss!

Which precisely is or are the dysfunction(s) seen by you in your game?

At first view, interrupt then repermit pathfinder behavior can perhaps, be a solution but nothing is less certain at this stage.
But it should be made at the right moment, of course.

A+
Xierra

1 Like

@Amigo54 The one thing using the pathfinder I am seeing is because it uses a acceleration force it can bounce or skip through tiles (unless you use get node path or next node which I’m not sure how to set up but have seen in the forums). How would I set up a check condition to set that up at the right moment I’ve tried before and wasn’t able to get it working the npc still seemed to bounce back and forth or jump through walls because of the acceleration.

1 Like

GDevelop’s pathfinding uses the A* (A-star) algorithm.
What you’re looking for is a way to round the values from the pathfinding or snap your object to a grid. This can be done without using the pathfinding behavior. Check the topic mentioned by ZeroX4, as the answer with the math details is provided there.

1 Like

I guess my main question would be how to use that to move one call at a time? Also thank you!

That’s the main trouble I’m having is using the pathfinding system to move that one grid cell at a time to a location with obsticales and looking above I’m trying to parse out the events to make it be one grid space at a time towards the location.

I save links to topics i find important
I did post here correct one
But i forgot to post 2nd one
I mean i saved 2 of them and i did not notice on my list there is another one cause usually i do not do that like i care to stick to 1 link per whatever i save

1 Like

It’s more complex than it should be or maybe it’s just me. I’m going to try to break it down.

There are 2 ways to move. Either allowing diagonals or not.

If you allow diagonals. It creates a path using the shortest route and fewest nodes.

If you disable diagonal movement then for some reason it creates a path from grid point to point instead of just using the fewest nodes.

If you disable diagonals it can look unnatural because it goes as far as it can in 1 direction before going in the direction.

If it was moving to a diagonal position it would move in the y direction first and then the X direction. Instead of creating a sort of fake diagonal of say “left, up, left, up…” it does “up, up, up… Left, left, left…”

So, the easiest way would be to disable the diagonal and the just use the X, Y of the next node. Unfortunately, that doesn’t look natural. Obstacles can help make it seem more natural but with open spaces it looks like it’s not taking the shortest path.

So, how can you move diagonal 1 grid at a time? Well, it can be complicated.

My idea would be to get the angle to the next node and then move 1 grid spacing in that direction. The problem is if you move diagonally, the distance is greater than if you move vertically or horizontally.

I guess you could round the angle to the next node to the nearest 45° and the get the X, Y to the position using the angle and the grid distance but then you would need to round it to the nearest grid position.

This was my first try. This would work for the diagonal movement bc I found out that the diagonal spacing can be calculated by multiplying the grid spacing by the sqrt of 2.

So, I guess there’s at least 2 methods. Either use an event for diagonal movement that uses the sqrt and another event for non-diagonal movement. Or calculate the X, Y and round it to the nearest grid point.

3 Likes

Thank you all a ton I’m going to try looking at all these methods and found some prototyping I’ll write back here if I have issues but I gravely appreciate it!

1 Like

Just to add another option. There is also the linked objects pathfinder. It uses objects that can be in a grid to move from object to object. A similar approach can be used to move to the next object.

https://wiki.gdevelop.io/gdevelop5/extensions/link-tools/

1 Like

One question the method with the diagonal movement did you have any other conditions to the left or is it just the events you posted? Just wanted to double check and ask before I started prototyping some stuff out

I was just using a mouse released.

1 Like

Awesome gonna try it out thank you again!

1 Like

So I tried copying your player movement and it seems to work 50 percent of the time it will move one grid space over but sometimes it shoots across 3 or 4 vertically and sometimes will jump out of the rounded grid space??? Any ideas maybe it’s the acceleration or speed which I have as base values but what would you suggest

This may sound daft and I’ve had a beer or two and I’ve only dabbled a couple of times with pathfinding but how about you turn the pathfinding off once it’s gone a certain number pixels from a point …reset that point and then use the snap to grid extension and turn pathfinding back on

Hey that’s a good idea I’ll have to give that a go!

I would née to see your events bc that was an example of diagonal movement. I agree, you could just use the angle and the grid spacing and round the X, Y to the grid.

Also, it could be that the 2nd route isn’t reachable so the first route doesn’t get overriden. I would check and make sure each path is found.

Hello dave!

You think like me apparently but this possible solution is not the simplest (a good synchro time is difficult to manage in the cas of our friend BigBoss)

A+
Xierra

I’m possibly wide of the mark - its just that occurred to me yesterday while going through the ‘how do i’.