GM:S 1.4 Caterpillar follow with running (Arrays)

Discussion in 'Programming' started by knru, Aug 14, 2019 at 12:05 AM.

  1. knru

    knru Member

    Joined:
    Jul 18, 2019
    Posts:
    4
    English isn't my first language and I'm new to GML, so I'm sorry if I say something weird. I'm trying to be as clear as I can.

    So, I'm trying to make a party member to follow a path player lays out, but never gaining too much distance to the player. I used this reddit post as a reference and it works out fine, as long as I'm just walking or running.

    The problem is, when I stop running, the party member walks back the path.

    The player (obj_hero_rpg) Create
    Code:
    array_size = 30;
    for(var i = array_size-1; i >= 0; i--){
        pos_x[i] = x;
        pos_y[i] = y;}
    
    The player (obj_hero_rpg) Step
    Code:
    //Running
    if canRun==1 {if (key_run) { if key_right or key_left or key_up or key_down {if spd < 5 spd+=0.5;}}
    else{ if spd > 2 spd-=0.5; }}
    
    //Party follow
    
    if (x != pos_x[0]) || (y != pos_y[0]){
        for(var i = array_size-1; i > 0; i--){
            pos_x[i] = pos_x[i-1];
            pos_y[i] = pos_y[i-1];
            }
        pos_x[0] = x;
        pos_y[0] = y;
    }
    
    The Party Step
    Code:
    record= 30 / obj_hero_rpg.spd;
    //So when running at full speed it equals to 6, and when walking it equals to 15
    
    x = obj_hero_rpg.pos_x[record];
    y = obj_hero_rpg.pos_y[record];
    
    If I have understood correctly, the problem is that when I'm pressing the run button, it's following variables pos_x[6] and pos_y[6]. But when I release the run button, it returns to the variables pos_x[15] and pos_y[15], which makes it go back the path.

    I tried to manually tell, that when the key_run is released, would pos_x[7...15] and pos_y[7...15] variables equal to pos_x[6] and pos_y[6]. It kinda worked, the party member didn't jump back anymore, but now it stayed in place for some time before starting to follow player again. Which isn't what I'm looking for at all.

    I then tried to find a way to make double array. I created arrays pos_x[0,i] , pos_y[0,i] and pos_x[1,j] , pos_y[1,j]. My goal was to make the pos_x[1,0] , pos_y[1,0] be equal to pos_x[0,1] , pos_y[0,1] with setting the variable j = i-1, and making the party memebr to follow pos_x[1,j] and pos_y[1,j] but I couldn't get it to work at all.

    I looked into some enemy chase AIs as well, but I'd really like the party member follow the same exact path, not find its own path to the player. I tried the ds_queue thing as well, but I couldn't get the party member stay close to the player when running, so sticked with this code.

    How would I make the party member to follow my player's path at a certain distance at all times? Is there any sense in what I've tried to do? Is even arrays the way to go or should I be using something else?

    I'm running out of ideas so help would be appreciated.
     

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