1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 path_positionprevious

Discussion in 'Programming' started by mdbussen, Jan 17, 2018.

  1. mdbussen

    mdbussen Member

    Joined:
    Jul 8, 2016
    Posts:
    62
    I am either using the path_positionprevious variable incorrectly, or it is not working how it should.

    I have an instance that I have given a path (using mp_grid) and as it is traveling along its path I want it to "look ahead" and see if there is going to be a collision, because I am going to have multiple instances following paths from point to point and I want them to be able to intelligently avoid each other. My idea for how to do this was to "look ahead" a discrete number of steps. In order to do this, I have to determine the "step size" in terms of the path position, so that I can then use the path_get_x and path_get_y to get the upcoming x/y location of the instance and check for collisions using place_empty. So I have the following code in order to get the step size:

    Code:
    var path_step = path_position - path_positionprevious;
    
    // check to see if there will be a collision if the character moves to this location
    // we will look ahead 5 steps because otherwise there may not be sufficient time to course-correct
    var return_val = false;
    for (var i = 0; i <= 5; i++){
       var next_x = path_get_x(path, path_position + path_step * i);
       var next_y = path_get_y(path, path_position + path_step * i);
       return_val = return_val or !place_empty(next_x, next_y);
    }
    
    However, my code didn't seem to be working as intended. no matter how much I increased the number of steps in the "look ahead" (from 5 up to 30), it didn't seem to make a difference. The instance wouldn't course-correct until it had already collided. So I did some checking and what is happening is that the path_step variable is always evaluating to 0, because path_position and path_positionprevious are always the same. EVEN when I can SEE in the debug printout that the path_position in the previous step was 0.98 (for example), both path_position and path_positionprevious are 0.99.

    Am I using this instance variable incorrectly, or is it bugged somehow?
     
  2. icuurd12b42

    icuurd12b42 TMC Founder GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    1,840
    check if path_position and previous are the same... like x/y_previous only have a valid previous value after the step event... so your code would not work if it's in the step event or any event before that

    _previous variables are only valid from the collision event, the end step through the draw events
     
    mdbussen likes this.
  3. mdbussen

    mdbussen Member

    Joined:
    Jul 8, 2016
    Posts:
    62
    Thank you! This was the root of my problem. I moved the code to the end_step event and it is now working appropriately :)
     

Share This Page