• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ Pathfinding Confusion

BQubed

Member
I've currently hit a logic wall with working on pathfinding. My NPC currently finds the right path (according to the draw_path function) but there are whack-a-mole style problems occurring. Here's some code:

This snippet is from a state machine switch statement.
GML:
    case NPCSTATE.WANDER:
            
        MoveTo(295,189)
        MoveUpdate(walk_speed)
        
        myPath = path_add();
        mp_grid_path(global.RoomGrid,myPath,x,y,TargetX,TargetY,false);
        path_start(myPath,walk_speed,path_action_stop,true);
        
    break;
This is the innards of the MoveTo script kindly provided to me by one of the helpful people here on the forum.
GML:
function MoveBegin() {
    // Initialize all the variables
    TargetX = x;
    TargetY = y;
    IsForcedMoving = false;
}
function MoveTo(x, y) {
    // Set the new target posiion
    TargetX = x;
    TargetY = y;
    IsForcedMoving = true;   

}
function MoveUpdate(spd) {
    // if not forced movement, exit
    if(!IsForcedMoving) exit;
    // get the distance to the target
    var dis = point_distance(x, y, TargetX, TargetY);
    // if we are still far away from the target, then move to it
    if(dis > spd) {
        // get the direction from us to the target
        var dir = point_direction(x, y, TargetX, TargetY);
        // use trig to calculate the deltaX and deltaY (how far to move in each axis this frame)
        var dx = lengthdir_x(spd, dir);
        var dy = lengthdir_y(spd, dir);
        // actually move
        if (!place_meeting(x + dx, y, oCol)) { x += dx; }
            else { local_frame = 0; NPCstate = NPCSTATE.IDLE; }
        if (!place_meeting(x, y + dy, oCol)) { y += dy; }
            else { local_frame = 0; NPCstate = NPCSTATE.IDLE; }
            
        var previous_sprite = sprite_index;
        direction = dir;
        sprite_index = wander_sprite;
        if (previous_sprite != sprite_index) local_frame = 0;

        var total_frames = sprite_get_number(sprite_index) / 4;
        image_index = local_frame + (CARDINAL_DIR * total_frames);
        local_frame += sprite_get_speed(sprite_index) / FRAME_RATE;

        //Animation Loop
        if local_frame >= total_frames
        {
            animation_end = true;
            local_frame -= total_frames;
            
        } else { animation_end = false; }

    } else {
        // snap the last frame to the target and end force movement
        x = TargetX;
        y = TargetY;
        IsForcedMoving = false;
        
    }
}
This is the code within the persistent grid creating object within all rooms.
GML:
var _w = room_width / 3;
var _h = room_height / 3;
global.RoomGrid = mp_grid_create(0, 0, _w, _h, 3, 3);
mp_grid_add_instances(global.RoomGrid,oPlayer,true);
mp_grid_add_instances(global.RoomGrid,oCol,true);
The issues I'm having are bizarre and I'm having trouble pinning down the cause. The path the NPC draws each time is correct. Here's a visual:

1608864816431.png

In this one he basically walks in place in the position you see in the picture. There's enough room to get around the player character but he just doesn't.

1608864993017.png

In this one he starts in the same place (that empty red rectangle) and starts walking but speeds up to double the speed then walks in place at the position in the picture without completing the path even though it's clearly an empty spot.

This is my first foray into pathfinding so please be patient with me. If anyone has any insights/ideas, I'd be appreciative.
 

Padouk

Member
Sound like your Collision Mask is not proprely set on that brown sprite.
You seem to have the default rectangle instead of just the feet. Your « idle » seems fine that’s why the path is found... I would double check the « walk » sprites habe the same collision box than the « idle »

Image 1 showed the characters beeing blocked by the first upper left block
Image 2 showed the characters overlapping the blocks beeins stuck in place.
 
Last edited:

BQubed

Member
I checked the mask and it's set at the feet, same as the idle sprite. NPCs are coded to stop moving when they hit an instance of oCol but this guy just keeps walking in place.
 
Top