Need help stopping an NPC at the middle of a path

Discussion in 'Programming' started by TankRockett, Aug 28, 2019.

  1. TankRockett

    TankRockett Member

    Joined:
    Aug 28, 2019
    Posts:
    4
    So i've been following this tutorial on NPC paths :

    and successfully made the base obj as in the video.
    Here's the code(path related only):
    obj_NPCBase:
    <CREATE EVENT>
    //Path
    Target = instance_create(x,y,obj_target);
    Path = -1;

    <STEP EVENT>
    if(Path != -1){
    dir = point_direction(Target.x,Target.y,x,y);
    dist = point_distance(Target.x,Target.y,x,y);

    dx = lengthdir_x(1,dir);
    dy = lengthdir_y(1,dir);

    x -= dx;
    y -= dy;
    if(dist > 128){
    x = Target.x;
    y = Target.y;
    }
    }
    <USER DEFINED 0>
    ///Assign path
    with(Target){
    path_start(other.Path,1,0,1);
    }

    obj_enemy:

    <CREATE EVENT>
    event_inherited();

    Path = patrol;
    event_user(0);

    Now i want make the enemy stop at the middle of the path.I made an obj_pause and make it collide with the enemy,every time they collide the enemy will stop.

    The problem is that only the enemy will stop,the obj_followtarget just keep going.And when the enemy is moving back again,it will go out of the path to keep up with the moving obj_target.I've tried many ways to move the target but none works.

    My main goal is to stop the enemy,and resume the path when it need to.Any suggestions ?
     
  2. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    629
    You have the enemy following the target, so you need to stop the target. Enemies head towards an object on a path, when you want them to stop / start based on their own circumstances.

    In the video they don't have the guard follow the path because they are using physics, but you have removed that in your code. Having the target is unnecessary now. If you persist in following their tutorial, rather than setting it up for your particular conditions having deviated from it, you will have to tie in control of the target to the enemies collisions.

    If enemy collided
    {
    target path_speed = 0
    }
    else
    {
    target path_speed = whatever it was before
    }

    But by this point you may as well have the enemy following the path in the first place, as their method is redundant.
     
    Last edited: Aug 28, 2019
  3. TankRockett

    TankRockett Member

    Joined:
    Aug 28, 2019
    Posts:
    4
    So I've follow your instructions,not entirely but your method worked nonetheless.Now i want the enemy to enter a state where every 2 second he will change his sprite to left and then right (Which is like he's looking around for the player but i haven't set up a vision cone just yet)and then go back to his original sprite and continue down the path.Will you help me with this too ?
     
  4. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    629
    That would probably be something like:

    create event:
    Code:
    alarm_set = false;
    alarm_count = 0;
    is_side = 0; 
    
    step event
    Code:
    if path_position < 1
    {if !alarm_set
    {alarm[0] = ??;
    is_side = image_xscale;
    alarm_set = true;
    }
    }
    alarm[0]:
    Code:
    path_speed = 0;
    if alarm_count < ??
    {
    if image_xscale == 1
    {
    image_xscale *= -1
    }
    else
    {
    image_xscale = 1;
    }
    alarm_count += 1;
    alarm[0] = ??
    }
    else
    {
    path_speed = ??
    image_xscale = is_side;
    alarm_set = false; // if you wanted to have the process repeat again later
    }
    
    That would start an alarm once on a path, to whatever amount of time you want to set.

    When the alarm goes off it is switching the way the sprite is facing, and sets the path speed to 0.

    It also sets a count of the alarm so you can control how many times it does this before resetting.

    Then it reverts back to the original way it was facing, and resumes the path.
     
  5. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,744
    Would be even easier with a state machine.

    Start in state 0. In this state you set the path speed to like, 3. If path position > 0.5, go to state 1 and set the path speed to zero (i.e., stop - but don't stop following the path!).

    In state 1, you look left. Count down a counter (set during the transition), when it hits 0 you transition to state 2.
    state 2 is the same as state 1, except you look right.
    <repeat as needed>
    Finally, you transition into a state where you go down the path again (setting path speed to 3 or whatever)
     

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