# Need help to understand this code.

Attention Topic was automatically imported from the old Question2Answer platform.

Hello, friends. I want to learn 2d path finding. I’ve downloaded a project and imported it on my Godot engine just to understand how this works. Here’s the link…

https://godotengine.org/asset-library/asset/117

I just can’t understand what this code actually means.

``````func move_along_path(distance):
while path.size():
var distance_between_points = last_point.distance_to(path[0])
if distance <= distance_between_points:
character.position = last_point.linear_interpolate(path[0], distance / distance_between_points)
return
distance -= distance_between_points
last_point = path[0]
path.remove(0)
character.position = last_point
set_process(false)
``````

I know how loops and if/else statements actually work. An empty array was assigned named path. In the while loop, I can’t understand the condition. I mean, there’s a size of this array. Is it equal to something? Or greater or less than something. And I don’t understand this part last_point.distance_to(path[0]). So it is hard for me to understand how this function works. Need help to understand this.

Are there any good tutorials which covers the details of path finding and navigation 2D? Any advice will be appreciated.

For `while path.size():`.
`path.size()` returns an integer, which, in a boolean context, are evaluated to false if equal to 0, and true if different from 0. Here, you could replace `path.size()` with `not path.empty()` for the same result.
Most types will tell you how bool(x) reacts depending of x in the documentation. For quick access to the documentation or definition of things, try ctrl+clicking on names.

For `last_point.distance_to(path[0])`
I am guessing that last_point is a Vector2 defined somewhere else in the script. Those are called “member” variables, and are accessible anywhere else in that script.
Vector2s have a function called distance_to, which takes one argument of type Vector2 and returns a float (real). This functions returns a value corresponding to the distance between the two vectors if they represent points on a 2D plane.
path[0] returns the first element of the Array name path, which I assumes contains only Vector2s.

`set_process(false)` is quite important, as it disables automatic execution of this script’s _process() function.

You can read a bit more on Navigation2D in it’s documentation, or look at answers on the subject.

Once you become confident with Pathfinding2D, please consider making a tutorial on it.

To put a literal _ inside messages here, you have to write `\_`, or put it inside ``.

Montasir Raihan | 2021-07-24 03:27

Hi,
lets step through this …

``````func move_along_path(distance):
# here you have missed out an important line
# there the last point is set to cahracter position
var last_point = character.position
# this path variable must be a object property, it must be a array
#and is an array of Vector2 points of a path
# so, while the path array is not empty (size zero) do this loop
while path.size():
#this calculates the distance between the character position
# (look at first line) and the last path point
var distance_between_points = last_point.distance_to(path[0])
#if the distance between the points is smaller then the desired
#stepwidth distance do this if where the next point of the path
#cannot be reached in one step
if distance <= distance_between_points:
# position the character between the points a stepwidth further
character.position = last_point.linear_interpolate(path[0], distance / distance_between_points)
#now return and wait for rendering and the next execution
return
#the next step takes the character over the next point no decrease the stepwidth(distance) to the length bejond the reached point
#so that in the next loop of this while only the remaining length will be walked
distance -= distance_between_points
#set the last_point eg the character position to the reached point
last_point = path[0]
#remove the reached point from the path
path.remove(0)
#do the loop to walk the remaining length to keep a constant speed
#the end of the path have been reached, set the character on the last point
character.position = last_point
#stop processing until another _update_navigation_path call initiaölizes a new path to walk
set_process(false)
``````

i hope this helps

Thanks man. It really helps.

Montasir Raihan | 2021-07-24 03:43