[Feature Request] New event for Pathfinding

Discussion and feedback on Construct 2

Post » Mon Sep 26, 2016 7:50 pm

In my project a solid tilemap is randomly generated and right after that the obstacle map is regenerated.
Later on the "find path"-action is triggered on a enemy-sprite since i want them to move to the player as soon as possible.
In "path found" i start the movement as usual but the path which was found is always a straight line to the player ignoring all obstacles
BUT if i put a System.wait(5 sec) after the "regenerate obstacle map"-action it works correctly. so i guess the regeneration of the obstacle map is an asynchronous process. But in that case there should be an event like "on obstacle map regenerated" where i can hook in to start the pathfinding.

Or did i something wrong?
B
4
Posts: 15
Reputation: 232

Post » Mon Sep 26, 2016 8:33 pm

You probably need to wait 1 tick for the tilemap to be populated before you use it to generate the obstacle map.
A big fan of JavaScript.
B
74
S
20
G
71
Posts: 2,230
Reputation: 44,892

Post » Mon Sep 26, 2016 10:21 pm

Thanks for your feedback.
Well i don't think so since "generate obstacle map" is executed right after filling the tilemap without delay. to make it work i needed to put the "wait"-action after "generate obstacle map".
B
4
Posts: 15
Reputation: 232

Post » Mon Sep 26, 2016 11:40 pm

Wait 0 second could also work but your game may lag on older computers or mobiles when it happen if there are a lot of events happening at once on that particular tick.
Test both and see which one works the best for you.
B
36
S
18
G
11
Posts: 248
Reputation: 8,694

Post » Mon Sep 26, 2016 11:58 pm

Regenerating the obstacle map is not done instantly, it's performed after your events. Colludium is right, you have to wait until the next tick. From the manual: https://www.scirra.com/manual/154/pathfinding

"Regenerate obstacle map
Determine whether each cell in the obstacles grid is an obstacle again. This is a very CPU intensive action and should not be used regularly. If only part of the obstacle map has changed, prefer to use one of the Regenerate region actions. Any changes made by using Add obstacle, Clear obstacles, Add path cost and Clear cost will take effect the next tick after this action. Note this means if you attempt to find a path immediately after this action, the obstacle map won't have been updated yet; add a 'Wait' action with a short delay to make sure the updated map is used in that case."
Moderator
B
95
S
34
G
33
Posts: 3,007
Reputation: 27,876

Post » Wed Sep 28, 2016 9:09 pm

@Sethmaster Wait (0) is not sufficient. 500ms works for me. maybe ~16ms (duration of a tick) does the same job.

But why do you not like the idea of having a new event which indicates that pathfinding can be safely used. using a system.wait to achieve this does feel like a dirty hack.
B
4
Posts: 15
Reputation: 232

Post » Wed Sep 28, 2016 9:36 pm

Wait 0 seconds doesn't work for this because it postpones the following actions until the end of the event sheet, not until the next tick.
Moderator
B
95
S
34
G
33
Posts: 3,007
Reputation: 27,876


Return to Construct 2 General

Who is online

Users browsing this forum: JesusJ and 11 guests