How do I make an object follow a predifined path?

Hello, I have an enemy that I want to move around some blocks. When it gets to a dead end it is supposed to follow the same path in reverse. I looked at the path movement behavior but couldn’t find a way to make it do what I’m trying to do. So I created some events that make the enemy move in different directions and turn when it hits different objects. However this method is very complicated and it doesn’t always work. Sometimes the enemy doesn’t change directions when it is supposed to and ends up going off its path and leaving the room.
I have attached screen shots of the events I used and what the scene looks like. The fireball is the thing that I want to move and the arrows are the objects that change its direction. They are invisible when the game is running.

Is there a simpler and more reliable way of doing this?

Screen Shot 2022-02-09 at 1.05.41 AM




Someone more experienced than me will come along, but just checking, did you give the grey blocks the Obstacle for Pathfinding behaviour?

Your events are too complicated for me, but I think what I would do is assign each ‘destination’ to a variable and then have the fireball go to one of the destinations but mix it up by randomising the choice of variable. But if anyone else gives advice, take it, as my experience is limited.

Hi Bubble. No, I didn’t use pathfinding behaviour or obstacles for pathfinding behaviour. I couldn’t understand how to make that work and also it doesn’t appear to be the appropriate behaviour for what I’m trying to do. I want the object to go in specific directions in a specific order. Like left to the corner, then down to the next corner, then right to the stairs, then reverse, etc. It doesn’t need any randomising because I want it to always follow the same path.

Well, if you’re willing to try again with pathfinding, give the fireball the pathfinding behaviour, turn off diagonals (to make the movement up/down, left/right only) and turn off rotation.

Then give the grey blocks the pathfinding obstacle behaviour. After you’ve done that, you can choose the ‘Move to a position’’ action for the fireball by giving the xy co-ordinates of the destination. Just get it working for one destination then use that to keep going for the rest of them.

Because you have a narrow path created by the grey blocks, the fireball won’t have any choice on how to get to the destination. It will just know where to go and what to avoid on the way.

Pathfinding may be a suitable option in this maze setup, but it won’t be if the intended path is longer, more complex or a more round-about route than the path found by the pathfinding behaviour.

With the direction setting blocks, @Andrew can make a complex path for the fireball to follow.

My way of doing it would be to have only one direction changer block, with 2 object variables that hold the angles to apply the force depending on the direction of the fire monster. So one angle for the normal path, and one angle for the reversed path.

By using this method, rather than hardcoding the angle in events, it makes for more flexible angles and interesting paths, and much, much less code.

So I’d look at something like this (yes, this would be all the code) :

Where DirectionChanger is this object image, with 2 object variables - Normal_Direction_Angle and Reversed_Direction_Angle.

I also changed the isfiremonsterreversed to Direction_Reversed and made it boolean, and added a boolean object variable (to FireMonster) named just_collided_with_direction_reverser. It’s to avoid this known “issue”.


[EDIT]
Updated screen snip to include a variable (Direction_Angle) to reversedirections which holds the angle the player will have to move when it hits it.

Ok, thanks. I will try that. MrMen, Are you one of the makers of GDevelop? Would it be possible to implement a type of path movement like the one in this video? MMF2 Video Tutorial 18 - Path Movement - YouTube It would make it way easier to make objects move in situations like this.

No, I’m not one of the developers of GDevelop :slight_smile: . You can request the extra path following functionality as a feature.

OK, I requested the path movement feature.
I also tried your method for the enemy movement and it seems to be working. Thanks.

Nevermind. It looks like the same problem is happening with MrMen’s method.

This post is not solved. Can it be edited so it doesn’t say “solved” please?

20 days ago you said it worked. Then 3 days ago you say it doesn’t work. What did you change in the 17 days in between?

I’ve edited the title.

Thanks.
I said it worked because I thought it was working but it actually wasn’t. The problem only happens sometimes so with more testing I found that it was still happening. I didn’t change anything.

So it works sometimes, and not others? If so, what are the scenarios for each? When does it work and when doesn’t it? What are the events you have?

I think I’ve figured out the problem. In order for the moving object to change direction, its center point had to be inside the direction changer and the direction changer’s collision mask was very small. It was a square in the middle of the direction changer and it was only a few pixels in height and width. I made it a little bigger and haven’t noticed the problem happening again. Unfortunately by making the collision mask of the direction changer bigger, I can’t make the moving object change direction at exactly the point that I want it to, but it’s pretty close. It seems that the faster I make the moving object move, the bigger I have to make the collision mask of the direction changer.