Pathfinding question!

Get help using Construct 2

Post » Sun Nov 17, 2013 4:27 am

Morning all!

I'm still playing PacMan, and one of the things I'm still having problems with is the ghosts finding the quickest route home once they've been eaten (without just passing through the walls!)

So I thought I'd try the pathfinding behaviour.

I've been through the manual and any tutorials I could find, but it simply won't calculate a path. It just sits there calculating it but never finds a path, and never fails to find a path either!

Here are my initial settings, the two lines of code that run at start of layout, and finally the events to kick off the pathfinding, followed by two events to handle the path found, or to re-run the pathfinding if it doesn't find a route home.



Any ideas what I'm missing here? The manual says it might take a fraction of a second to find a path, but I can go and make coffee and it's still working on it when I get back... something must be wrong!

The game so far is here if anybody wants a look! :)

Ta!
B
10
S
4
G
2
Posts: 8
Reputation: 1,896

Post » Sun Nov 17, 2013 4:49 am

From the picture I can't tell, when and how often "Add obstacle" is used. If you do it at start of layout, it'S ok. But if this happens several times throughout the running of the game, you will have a lot of issues, because there sems to be no "Clear obstacles" action. Without that action you are constantly adding to the obstacle map (the same object several times), which makes the obstacles map so huge, that it runs forever^^
Also, event 68 doesn't make sense. If it fails to find a path, it won't find one, when just repeating the same action. And regenerate obstacle map is a redundant action in this event. Nothing has changed, so no need to regenerate the map.

In short:
Try adding a "clear obstacles" action, followed by regenerate map, if you are using "Add obstacle" anywhere else than in start of layout.
Remove the actions from event 68. Instead, for debugging purposes, you could fill a text with "Path not found" or something.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Sun Nov 17, 2013 5:08 am

I see you have 'is calculating path (inverted)' + 'animation "eaten" playing'. This could be your problem, because as soon as it finishes calculating, it is no longer calculating and will keep trying.

So check to see if they are under a trigger condition (I can see they are a sub-event, but not of what). If not, you should add one like "on animation 'eaten' started".
B
28
S
8
G
5
Posts: 429
Reputation: 7,007

Post » Sun Nov 17, 2013 6:04 am

Thanks @tulamide and @Tobye for the replies.

The "add obstacle" is used only "on start of layout".

The reason I added the "is calculating path (inverted)" at line 66 was because I was unable to run the code from within a sub-event (I got a message at runtime saying the GUI will prevent this in the future!), so I had to add my code on the root path. For this reason I figured it was prudent to add the "is calculating path (inverted)" or it would continously re-initiate the "calculate path" code and never actually allow it to finish!

But thanks to your replies I set a text output on "On pathfinding failed to find path" and that's exactly the problem - it can't find a path, so it was continously re-running the pathfinding routine, and giving the impression it was always running but never finding a path.

So my next question has to be... why can it not find a path?! I've only added the walls as obstacles, so it should seemingly be able to calculate a route from anywhere within the grid to the "GhostHome.X,GhostHome.Y" position, which is in the centre of the screen. From what I've read if it couldn't find a path because the home location was blocked off then it should pick the nearest spot instead.

Aside from the pathfinding behaviour the ghost just has a bullet behaviour, which I disabled before I started the pathfinding routine in case this would affect it.

Am I right in thinking it should be able to find a path? What am I missing?! 8 direction movement maybe, or some other pre-requisite? I tried making the GhostHome sprite visible as well, in case it has to be visible for the pathfinding to work, but that didn't help :(

Thanks again! :)
B
10
S
4
G
2
Posts: 8
Reputation: 1,896

Post » Sun Nov 17, 2013 7:12 am

I would just do 'for each "ghost"'+'"eaten" is playing' + 'trigger once' = find path to ghost home. Or maybe link it to the event that kills the unit. So like: '"ghost" health =< "0"'+'trigger once' = 'find path to ghost home'.

How you have it now will continue to loop the pathfinding. Even if you add trigger once to what you have, it will still loop because as soon as it finds a path it is no longer finding a path and the animation is still playing.

If your pathfinding is failing, it could be that you have the cell sizes too large, meaning they can't fit through the gaps. Try lowerig it to something ridiculously small like 2 and see if it works then.

Also a capx can really help us to debug ;)
B
28
S
8
G
5
Posts: 429
Reputation: 7,007

Post » Sun Nov 17, 2013 3:02 pm

Can't add anything to the last post. And @Tobye good find, I missed that one completely, but indeed can be a problem.

As Tobye said, if you still can't solve it @Tig0r try creating a demo capx with the error. It would make it much easier to track down the issue.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Sun Nov 17, 2013 7:27 pm

@Tobye you make an excellent point, I had completely overlooked that! I've changed it to use an instance variable which I set when pacman eats a ghost. The pathfinding routine is then called, and I reset the instance var at the end of that block of code, so it's definately only being called once now.

I tried setting the cell sizes right down and whilst it didn't fix it, the ghosts did move fractionally, so I knew I was along the right lines.

I tinkered with the settings, and eventually came up with a working combination (more-or-less!) of a cell size of 32 (which is my tile size), and a cell border of -1.

(I say more-or-less as the ghosts do tend to skim the corners, but hey, they're ghosts trying to get back home as fast as they can! :)

Anyway thanks very much for your help, Tobye and @tulamide, I doubt I would have got there without you! :)
B
10
S
4
G
2
Posts: 8
Reputation: 1,896

Post » Sun Nov 17, 2013 8:18 pm

@Tig0r - No problem, glad to help! :) There are ways to make the movement exact, but I haven't done it for a long time so don't remember how to. You can search the forums for many answers on it, but I believe they all take a little work.
B
28
S
8
G
5
Posts: 429
Reputation: 7,007


Return to How do I....?

Who is online

Users browsing this forum: nahima and 9 guests