How do I update a path's final destination without re-creating it?

R

RetroRain

Guest
The problem I've been having, I have been unable to solve despite my best efforts. I have searched many tutorials, forum and Reddit posts, and YouTube videos, and I can't solve this problem that I am having.

The problem is this:

I have a maze. In the maze, are the walls obviously, a player, and an enemy. I want the enemy to follow the player, avoiding the walls. Even though diagonal movement has been disabled, the enemy keeps cutting corners despite that.

I realized that it cuts corners, because of this piece of code in the step event:

Code:
if (mp_grid_path(global.grid,path,x,y,mx,my,false))
{
    path_start(path,1,path_action_restart,false);
}
I found out, that you're not supposed to have this code in the step event, as it keeps restarting the path, and causes that cutting corners that I don't want.

Anyway, so how do I update the x and y final destination so that the enemy can follow the player around the maze when he moves?

Here are the code blocks for my objects:

objGrid

CREATE EVENT

Code:
cellWidth = 16;
cellHeight = 16;

hcells = room_width div cellWidth;
vcells = room_height div cellHeight;

global.grid = mp_grid_create(0,0,hcells,vcells,cellWidth,cellHeight);

mp_grid_add_instances(global.grid,objWall,false);
objEnemy

CREATE EVENT

Code:
path = path_add();
mx = objPlayer.x;
my = objPlayer.y;
if (mp_grid_path(global.grid,path,x,y,mx,my,false))
{
    path_start(path,1,path_action_restart,false);
}
I tried putting this code in the CREATE even, and using alarms to delete the path, and re-create it right afterwards, and it throws the enemy object off the grid (snapped off the grid, and still follows the path and cuts corners)

I simply want to make it so my enemy follows the player wherever he goes on the maze, I just don't know the proper code on how to do it, despite various code syntax I have used. I have even used path_action_restart, and I just don't know what to do.

Any help would greatly be appreciated.

Thank you.
 

TheouAegis

Member
Why not just do it Pac-man style?

Code:
///Create Event
paths = ds_list_create();
dir = 0;
tiles[3] = 0;
GOTO = [3,2,2,3,3,0,0,3,1,2,2,1,1,0,0,1];

///Step Event
if x + y & 7 return 0; //if not aligned to an 8x8 grid, leave this script
ds_list_clear(paths);
tiles[0] = tile to the right;
tiles[1] = tile to the left;
tiles[2] = tile up;
tiles[3] = tile down;
var targetx = player.x, targetY = player.y;
for (var a,i=0; i<4; i++)
{
   if i == dir + 2 & 3 continue;
   if !tiles[i]
      ds_list_add(paths,i);
}
if ds_list_size(paths) > 0
{
   if ds_list_size(paths) == 1
      dir = paths[|0];
   else
   {
      var b, weight = 0;
      a = abs(targetX - x);
      b = abs(targetY - y);
      weight += (targetX<x) * 4 + (targetY < y) * 2 + (a < b);
      for(i = 0; i<3; i++)
      {
         if ds_list_find_index(paths,GOTO[weight]) > -1
         {
            dir = GOTO[weight];
            break;
         }
         weight++;
         if i == 3
            dir = paths[|irandom(ds_list_size(paths)];
      }
}
else show_debug_message("Help! I'm trapped in a box and I can't get out!");
 
Top