NikiTo said:
you should first solve that “two nested while true loops” first. Maybe save at some place the current best walked path distance and when you want to visualize it, don't for(;;) it again, but just read from a list that was prepared as an extra during the scan.
If we look at execution flow, there are no nested loops in my example code, notice the inner loop is started only after the goal has been found, and after extracting the path from the data it breaks out of the outer loop as well.
It would be better to break out of the path creation loop when the goal is found, and start the path extraction after that to make this clear (and help the compiler to figure this out).
NikiTo said:
Maybe the last thing you do, just before publishing your finished game, could be looking at the bytecode and trying to improve it.
This does not work for two reasons:
If we do not care about performance all the time, we would end up at rewriting everything after we are done. So spending too much time and introducing to many bugs.
And second, it becomes impossible to optimize because we can not find all the bottlenecks. We may find a small number of really bad ones, but after optimizing those few cases we no longer see any further bottlenecks. Every part of the program takes roughly the same 1% of processing time.
Looking at this it seems there is no more reason to optimize further because nothing will have any noticeable effect. So we would decide to leave it at this, although in reality the whole code may be slow.
That's why we better care for performance all the time. Even if we have other goals as well which may appear more important.
Calin said:
it`s still buggy here`s a scenario that doesn`t work
Looks like it visits disconnected islands… it's a total mess, man. I am not surprised it does not work - i was pretty sure it would not.
Listen: Dijkstra is the simplest possible algorithm to find shortest paths. You say you want a simple solution easy to maintain, and it does not have to be as fast as possible. This is exactly what my example code on page 3 is doing. (It is simple Dijkstra algorithm without using a queue to add path segments in optimal order to find the path faster).
So you have two options:
1. Continue to reinvent the wheel, which would bring you to the exact same algorithm!!!
(We would have and use this algorithm even if Mr. Dijkstra never existed - it's just coincidence the algo is named by him.)
2. Understand how my example works, or just use it. It is fine to use things even if we do not know exactly how they work. E.g., almost nobody understands quaternions, but everybody uses them. And it just works.
After some time, you may need to understand to improve. Usually this happens, and you learn those things even if you did not understand all the details in the first place.
If you want to understand it fully right now, simply ask on a certain detail you do not understand. Then you usually get good answers, if you describe what you have, what you want, and what does not work.
The difference between those 2 options is the amount of wasted time which could have been invested in a better way.