# Roguelike enemy movement behavior

Hi guys, I’ve been using Gdevelop for the last month or so and I decided to make a rougelike game inspired in the Lazy Devs rougelike series(Making a Roguelike #1 - Overview - Pico-8 Hero - YouTube).
I´m using the “RPG movement” that Lizard shared in the forums for Gdevelop5 with an 8x8 grid and a movement speed of 48pps for the player.( All my objects are 8x8px) I added a Scene variable for the players turn and another one for the mobs turn(snakes and skelletons) The idea of this is that all mobs will move after the player ended moving. The problem comes when I try to make the mobs move towards the player.
First I tried to make them move to a random position like this:
For each instance of Skelleton
Skelleton is in colliton with Barrier, Move Skelleton away from Barrier
Skelleton is in colliton with Skelleton, Move Skelleton away from Skelleton
scene variable Moving=0, Mob_Turn= Random(3)
Mob_turn=0, Skelleton X position Add 8
Mob_turn=1, Skelleton X position Remove 8
Mob_turn=2, Skelleton Y position Add 8
Mob_turn=3, Skelleton Y position Remove 8

The problem here was: When Skelleton collided with Barrier, the movement resolved in 2 diferent ways:

1. Skelleton moved diagonally to an empty space
2. Skelleton skipped the 8x8 Barrier, moving 16px in the Barriers direction.

The good thing here was that the Skelleton moved inside the grid 8x8 almost all the time.

Now I’m trying to use the Pathfinding behavior setting the Skelleton as a pathfinder, The player as the pathfinder’s destination and the Barrier and a “pot object” as an obstacle.

The thing is, after the Mob’s turn is finished, the skelleton still moves towards the player, I tried fixing it with this:

Here I have two problems:

1. The skelleton will move only when is his turn, but is not going to respect the 8x8px grid movement even if the Pathfinding virtual cells with and height are 8x8px.
2. And thanks to the acceleration quality that the pathfinding behavior have, sometimes the Skelleton is going to jump right into the player no matter the distance.

The good thing is that relation pathfinder/obstacle works fine.

Is there a way to make the mobs move 8px towards the player per turn while respecting the obstacles?

Hi,
There’s a new example called “board play” or something. I think it should help you figure out where are the osbtacles, so that if the skeleton tries to move towards one, you throw another random.

I don’t know why the pathfinding would ignore the grid you set up. If you can reproduce the problem in a dummy project, share it with us.

1 Like

Thank you so much for the response!, I’m going to check the example, if I continue with the problems I’ll make a dummy project so i can share it here.

is the player moving, when the enemys are moving?
if the player itself is not in a 100% right position of the grid, the enemys will not respect the path. my solution was, they dont move to the pos. of other mons (player in your case), but to the grid its in.

what i also did was, i corrected the position of my mons, everytime they finished movement, so that they dont snap out of grid (i use spriteblocks as grids, to store information for distance, is passable, enemy, friends, item, wich terrain and also animation etc.)

1 Like

Thanks for this solution!,

The player moves first, then, when his moving variable=1, the mobs Turn variable adds 1, when the players moving variable=0 and the mobs Turn variable is bigger than 8 the mob moves.

I find the spriteblock Idea really interesting , but I don’t know how to execute it (How and where do I set them on the grid, and how many of them?), and also, should I use the same DestY, DestX, for the player and the mobs? or should I make new Variables for them?

Sorry If those are too many questions but I’m a bit lost jaja.

if you want to have a gridsystem with sprite, you can ether place all of them manually (unneccecary work) or create them at the start.

the repeat 176 times event is under at the beginning of the scene, trigger once.
you need to change the numbers of cause, do the math for gridbocks 8x8 instead of 50x50.

how you use your grid is up to you, but questions like how and where to put them, is the same answer to your current system, you must have some idea, where and how you put them.
if your mobs move one by one, you dont need more then 1 DestX destY, if they move simultaniously, youd should have one for all of them, but since they all have the same destination, its not required.

if player 1 stops moving, make sure he´s in the pixelperfect position inside the grid (same with the mobs).
the problem with pathfinding is, that when you deactive it, it still can move a pixel or 2 more, depends on the setup. example, my mons have 10000 acceleration and 200 max speed, and they do overstep a little without position correction.

my project is a little broken, otherwise i would upload it as a study example (its like pokemon Conquest / Turnbased strategy game).

1 Like

Thanks for sharing this, I think I get it now, I made a noob mistake, I don’t have a gridblock object, and I was trying to make them move within the Gdevelop grid. Because of that I’m having a hard time with the Mob positions (since nothing make the mobs respect the grid).

I’m going to make the changes and test everything again.

Thank you so much for the examples and replies!