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.
This is the innards of the MoveTo script kindly provided to me by one of the helpful people here on the forum.
This is the code within the persistent grid creating object within all rooms.
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:
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.
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.
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;
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;
}
}
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);
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.
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.