diff --git a/src/MultilevelAStarEx.cpp b/src/MultilevelAStarEx.cpp index 74fc974..61a267a 100644 --- a/src/MultilevelAStarEx.cpp +++ b/src/MultilevelAStarEx.cpp @@ -222,11 +222,19 @@ bool MultilevelAStarEx::can_move(const Node *current, int x, int y) const TypedArray MultilevelAStarEx::generate_path(const Node *current) const { - TypedArray arr; + // count the number of nodes in the path + int cnt = 0; + for (const Node *n = current; n != nullptr; n = n->parent) cnt++; + cnt--; // because we're not adding the starting node + + TypedArray arr; + arr.resize(cnt); + + int i = cnt; while (current->parent != nullptr) { - arr.insert(0, Vector2i(current->x, current->y) + _trans); + arr[--cnt] = Vector2i(current->x, current->y) + _trans; current = current->parent; }