GMS 2 [SOLVED] How to truely STOP a path

Discussion in 'Programming' started by Brennan Duncan, Jan 14, 2020 at 8:56 PM.

  1. Brennan Duncan

    Brennan Duncan Member

    Joined:
    Sep 19, 2018
    Posts:
    33
    Hello everyone. I appreciate you coming in to check on this thread.

    I've been working with GMS1.4 for the longest time, and now GMS2 (for a total of roughly 5 years of working with gamemaker as a whole). In all this time, I've ALWAYS written my own movement code. I've never, once, used the path functions, but had a basic understanding of their operation.

    Now, I'm dabbling in paths (more for a complete understanding of the IDE and its intricacies).

    My main issue atm is what to do when an instance has completed its path. I check "path_position == 1" to see if an instance is at the end of the path (NOTE: path_endaction = path_action_stop). The if statement returns true and runs code, but it runs the code every step, relentlessly (and i DO mean relentlessly). I can't seem to stop the code from running without starting a new path, moving the instance to a new position along the path (that isn't "1" obviously), or destroying the instance running the path.

    My needs are simple. When the instance reaches the end of the path, I want it to stop. I mean not just stop moving on-screen, but i want the path to finish, run a code block, and "vanish".

    The "vanish" part is where I'm stuck. The path is still assigned to the instance and therefor constantly registers the instance being at the end of that path. I want the instance to return to the "state" BEFORE assigning it the path (so it would have NO path attached to it).

    I feel like there would be some sort of "path_UNassign" function. Surely, i'm missing something.

    Any help would be greatly appreciated.

    Thank you.
     
  2. SeraphSword

    SeraphSword Member

    Joined:
    Aug 16, 2016
    Posts:
    38
    As far as relentless checking, why not just use a variable like shouldCheck = true, then at the end of you path set shouldCheck = false. So in your Step event, you use an if (shouldCheck == true), where the variable is set to true in the same spot you start the path. That way you are only checking when you have something to check.
     
  3. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,846
    GameMaker keeps track of the current path the instance is following in the variable path_index.
    If path_index is -1, the instance is following no path.
    Otherwise, if path_index is the index of a path, then the instance is following that path.

    By setting path_index to -1, you make the path "vanish".

    The "path_endaction = path_action_stop" should already reset path_index to -1 when the path ends, but it doesn't reset path_position.
    Simply resetting path_position to 0 once path_index >= 1 worked for me to make the if-block only run once when I just tested it in GameMaker 1.4.

    This is also a good option, although it should be unnecessary in this particular case.
     
  4. Brennan Duncan

    Brennan Duncan Member

    Joined:
    Sep 19, 2018
    Posts:
    33
    I actually have been using that method to ensure it doesn't check more than once at a time, but my issue isn't just making the sprite stop along the path and run code. The path is somehow attached (indexed?) to the instance (in this case the player object) once the "path_start" function is called, but I want remove that path from the instance.

    In your example the path isn't removed from the instance. Rather, it's still very much there and constantly telling the game that the player is at the end of the path until I assign it a new path (or destroy the instance).
     
  5. Brennan Duncan

    Brennan Duncan Member

    Joined:
    Sep 19, 2018
    Posts:
    33
    Thank you! I KNEW it would be something simple. I should've guessed that. It works the same for sprite_index (and most "_index" vars).

    EDIT: Unfortunately, while it may let you set the path_index, gms2 treats it as read-only. However, i was able to make it work with this:
    Code:
    // Step Event
    
    if( path_position == 1 )
    {
       path_position = 0;
       // run code
    }
    It works because the path_index is set to -1 by GM at the same time the position reaches "1" (end of the path).
     
    Last edited: Jan 14, 2020 at 9:42 PM
  6. SeraphSword

    SeraphSword Member

    Joined:
    Aug 16, 2016
    Posts:
    38
    Isn't path_index "read only"? That's what it says in the documentation, so I wasn't sure if you could set it manually (which was going to be my other suggestion).
     
    Brennan Duncan and jo-thijs like this.
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,335
    Then path_end()
     
    Brennan Duncan likes this.
  8. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,846
    You're right. It must have changed in GameMaker 2.

    Yup, that'd be the one to use then.
     
    Brennan Duncan likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice