• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [SOLVED] Problem with path_position, while, and x/y coordinates

TobyMoby

Member
Hey guys!

I stumbled upon a rather strange behavior of GMS2 regarding paths, path_position and x/y coordinates.

Following scenario:

I have an "obj_player" that can move 2D platformer style.
A second object, called "obj_pathpos" (for path position) is assigned to path "pat_test".
pat_test goes from left to right, while also slightly going down.

path1.png

Now I want obj_pathpos to always match the x-coordinate of obj_player.

So my code is:
Code:
while (x<obj_player.x)
{
    path_position+=0.01;
}
(code is only for the case player x is greater)

So, if the x-coordinate of obj_player was to increase above the x-coord. of obj_pathpos, it would increase the
path_position of obj_pathpos in 0.01 increments, moving it to the right along the path, until this is no longer true.

The problem is, this does not seem to work. The game freezes completely everytime.
Why is that? Am I missing something obvious?
At a certain path_position, the x of of obj_pathpos becomes inevitably greater than obj_player.x, no?

If I replace "path_position" with just "x". so:
Code:
while (x<obj_player.x)
{
    x+=0.01;
}
...it works just fine (if I comment the "path_start()" out, of course.

Also, if I use a simple if-statement instead of "while", it seems to work, but then it's not following fast and precice enough.

Has anyone any idea, why my code is not working?

Thanks in advance!
 

Slyddar

Member
It's freezing because updating the path_position does not move the object along the path that step, so it gets stuck in an infinite loop. Running the debugger would show you that. I believe the movement actually happens after all the step events are complete.
 

TobyMoby

Member
Thanks for the clarification on this matter!
I'm pretty unskilled when it comes to using the debugger properly, I definately should look into this, more.

Thanks to your comment, I could figure out a way to pull it off. I'm now simply using path_get_x to get the x-coordinate of the upated path position, instead of the x-coord of the instance:

Code:
while ( path_get_x(pth_test,path_position)<obj_player.x
       && obj_player.x<path_get_point_x(pth_test,1) )
{
   path_position+=0.005;
}

while ( path_get_x(pth_test,path_position)>obj_player.x
       && obj_player.x>path_get_point_x(pth_test,0) )
{
   path_position-=0.005;
}
Thanks, and have a nice day!
 

Slyddar

Member
Top