How do I Get Pathfinding to work

Get help using Construct 2

Post » Fri Jul 29, 2016 5:24 am

Message: CryingOnion15 can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
I'm trying to make a Pacman like game and I can't get the "ghosts" to path find with obstacles or to even work properly.
Here is my work so far me know if that link doesn't work),
Is there something I'm doing wrong or am I forgetting something? I'm sort of new to this and still learning the layout of Construct 2, so and any help is appreciated.
Posts: 9
Reputation: 385

Post » Fri Jul 29, 2016 8:55 am

You are using the path finding a bit wrong.

The way you can think of the path finding behaviour is by following these steps:

1. Find path
2. Path found -> Move along path
3. Path not found -> do something else, could be to try to find a new path or whatever.

Find path
This is something that is handle in the background of C2 so your game will actually continue running while the path is found (calculated)

Therefore you would normally use the "Find path" based on some conditions, like a unit having reached its destination and needing a new path or you could make it trigger based on a state machine, mouse click if its a strategic game and so forth.

Path found
Once the "Find path" have found an calculated a path this event will trigger. The reason I call it a trigger is because you will notice that when you add it, it have a small green arrow to the left of it. All events or almost all event with such arrow are triggers, there are some events which look like they are triggers, but ain't (think its a bug). But as a general rule a green arrow means an event is a trigger. This means they will "activate" once a certain condition occurs, in this case it will trigger once a path have successfully been found.

Since its a trigger it needs to be placed on the top level or as a starting condition.

Code: Select all
On path found -> <Do something>   (Correct)

On Mouse click
        - On path found -> <Do something> (Not correct)

Typically once a path is found you want to use the action "Move along path" as it will make the unit execute the path it found. So a normal path finding setup would look something like this:

Code: Select all
1. Unit.Have_orders = False
    For each Unit                      --->  Find path (X,Y)
                                             Unit.Have_orders = True

2. On Path found                    ---> Move along path

3. On path found fail             ---> Unit.Have_orders = False

3. Path not found
This is used so should a unit fail to find a path you can react on it. but it works the same way as On path found.

Your game
After reading the above, I guess you can see why yours are not working correctly.

1. You have added the find path in an every tick event. So it will try to find a new path 60 times a second. So you will "overload" it with trying to find new paths.

2. The On path found trigger is placed in a sub-level of the every tick event. But since its a trigger it needs to be placed on the top level as a starting condition.

For your map you really should look into using a tilemap instead of sprites. It will increase the performance a lot and will also make it a lot easier for you to work with when designing the map it self. Just add a solid behaviour to it and the units will collide with any painted tile of the tilemap.

Hope that explains it a little and how to fix it.
Posts: 1,182
Reputation: 6,838

Post » Fri Jul 29, 2016 6:13 pm

Besides all things nimos pointed out:

Your player is a solid. Since you have set solids to be obstacles, the player is an obstacle.
You can not find a path to a position that is inside an obstacle.

Defining objects (as you do with the walls) as obstacles has no function if you have solids set as obstacle.

Would you set obstacles to custom and add the walls to the obstacles, then (since they dont move) once (in the on layout startup) is enough. Adding objects to the obstacles must be followed by a 'regenerate obstacle map'.

I am afraid the grid is to narrow for official pathfinding plugin, afraid it can not find a path that keeps the enemys out the walls.
Posts: 2,493
Reputation: 20,950

Post » Fri Jul 29, 2016 8:17 pm

Here is a way to find solid working paths in tight places & move a player in tight places. ... .capx?dl=0

Need those plugins.
Posts: 2,493
Reputation: 20,950

Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, MikaelS, nahima and 21 guests