The game I am working on is based on a maze where each level is procedurally generated. The pathfinding grid is a 2D array where each node holds its connections. Path requests are handled one at a time so there can't be two path calculations going on simultaneously. So far I have each node hold its data required for the A* (H,F,G scores and its parent) and manipulate them directly in the algorithm. Since the nodes are the actual nodes and not copies of them, every time the algorithm runs the data changes permanently and I have to manually set them on default values before the next run.

So my questions are :

1) Is this a valid way of handling pathfinding in a game?

2) What other data manipulation techniques I could use in order to avoid that?

If you need a copy of the code it's all on my github under Pathfinding and Managers/PathfindingManager.


You can avoid the manual reset by also adding a counter to each node that is set whenever the data needs to change. You only need to store f though as that is what is being used to compare. Though that will depend on how expensive h is to calculate.

Every time you run the path search you increment the counter and if the counter in the node does not match it you treat it as if it's default. Only when that counter rolls over will you need to do a manual reset, if the counter is 64 bit then that won't happen in any reasonable-length play session even if you do >1000 A* per frame.

You will need to hold the result path somewhere so your entities can use it.

|improve this answer|||||
  • \$\begingroup\$ Thanks a lot for your reply! I will test out your solution and see if it works better than what I already have. Cheers! \$\endgroup\$ – Erotokritos Vallianatos May 9 '18 at 19:28
  • \$\begingroup\$ @ΕρωτόκριτοςΒαλλιανάτος If this answer solved the problem, don't forget to mark it as accepted - it helps others who may run into the same or similar problems. Also, once you get at least 15 rep, upvote it as well. \$\endgroup\$ – Pikalek Aug 25 '18 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.