# Find "Duration" of a Path

#### TheMagician

##### Member
Does anybody know of a script that calculates the duration (in Steps) it takes for an instance to follow a certain path from beginning to end?

Things are easy enough if the path only uses speed factors of 100. In that case you simply divide path_get_length()by path_speed.

However, I'm looking for a solution that takes varying speed factors into consideration.

#### TheouAegis

##### Member
Code:
``````var L= path_get_length(argument0); //junk line
var S = argument1;
for(var V,D,T=0,n=path_get_number(argument0)-1,i=0; i<n; i++) {
V=path_get_speed(argument0,i) * argument1 / 100;
D=sqrt(power(path_get_point_x(argument0,i)-path_get_point_x(argument0,i+1))+power(path_get_pont_y(argument0,i)-path_get_point_y(argument0,i+1)));
T+= D/S;
}
return T;``````
I think...This assumes there are no curves, only straight lines. I can't see how you would find it out with bezier
curves in an efficient manner. I'm curious how GM actually stores curved paths, and knowing that might yield a solution.

#### TheMagician

##### Member
Thank you for your answer. Unfortunately it's not as easy as that - you can't just use the speeds at the individual path points. Instead you have to take the acceleration between the path points into consideration.

After some tests I came up with the correct formula to calculate the path duration (for paths that only use straight lines) - only to find out that the real duration in GameMaker is always a bit longer than the theoretical duration.

The difference depends on the speed factors of the individual path points. If there are more points with low speed factors then the deviation from the theoretical duration becomes larger. Also, the longer the path, the larger the deviation. The reason must be in the internal mechanism with which the movement along the path is calculated.

All in all, I came to the conclusion that the built-in path system cannot be used to calculate reliable durations (especially not for paths with Bézier curves). So I ended up coding my own path system where the way I measure the duration is consistent with the way the instances are moved along the paths.

#### Joe Ellis

##### Member
Couldn't you just simulate the movement with a while loop that makes it move along the whole path and end right back where it started, and measure each step of the loop's movement distance?

#### scorpafied

##### Member
in the case of a curved path, couldnt u just increment a variable when the path starts, then stop incrementing once it reaches the end. this eould give u the total frames it took. then divide by room speed if u want it to be in seconds.

that would be my approach. because no matter the acceleration your gonna get the right answer.