• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Need Help With Randomly Generated Level AI Pathfinding

Hello! I'm currently working on a game that uses random generation for its levels. So far things have been working well, my enemies spawn and everything, but the AI pathfinding is not. So I've been following a wonderful tutorial by HeartBeast (below is the final video in his tutorial series, this one focused on pathfinding)


One of the primary differences between my game and HeartBeast's tutorial game is that mine is controlled with the mouse, though I doubt that would affect the enemy AI pathfinding. Thus, I'll post the code that revolves around enemy AI below:

this is the code within my level generation grid that creates the pathfinding grid:
Code:
// Create the pathfinding grid
grid_path = mp_grid_create(0, 0, width, height, CELL_WIDTH, CELL_HEIGHT);
this is the code in the level generation grid that spawns the enemies:
Code:
// Draw the level using the grid
for (var yy = 0; yy < height; yy++) {
    for (var xx = 0; xx < width; xx++) {
        if (grid[# xx, yy] == FLOOR) {
            // Draw the floor
        tile_add(bg_floor, 0, 0, CELL_WIDTH, CELL_HEIGHT, xx*CELL_WIDTH, yy*CELL_HEIGHT, 0);
        
            // Add some enemies
            var odds = 20;
            var ex = xx*CELL_WIDTH+CELL_WIDTH/2;
            var ey = yy*CELL_WIDTH+CELL_WIDTH/2;
            if (point_distance(ex, ey, obj_hero.x, obj_hero.y) > 80 && irandom(odds) == odds) {
                instance_create(ex, ey, obj_bat);
            }
        } else {
            mp_grid_add_cell(grid_path, xx, yy);
        }
    }
}
I have a room end event that destroys both grids (the level creator and the pathfinding one)

Here is all the code for the enemy object:

Create Event:
Code:
/// Initialize the enemy
depth = -y;
path = path_add();
alram[0] = room_speed;

// Set image speed
image_speed = 0.15;
Destroy Event:
Code:
/// Destroy the path
if (path_exists(path)) {
    path_delete(path);
}
Alarm Event:
Code:
/// Move towards the player
get_path_to_player();
alram[0] = room_speed;
Step Event:
Code:
/// Update the depth
depth = -y;
Room End Event:
Code:
/// Destroy the path
if (path_exists(path)) {
    path_delete(path);
}
-----This is the script for the pathfinding:
Code:
///get_path_to_player()
if (instance_exists(obj_hero)) {
var xx = (obj_hero.x div CELL_WIDTH)*CELL_WIDTH+CELL_WIDTH/2;
var yy = (obj_hero.y div CELL_HEIGHT)*CELL_HEIGHT+CELL_HEIGHT/2;

    if (mp_grid_path(obj_grid.grid_path, path, x, y, xx, yy, true)) {
        path_start(path, 2, path_action_stop, false);
    }
}
That should be everything that pertains to the enemy AI. I've watched HeartBeast's video multiple times, following every line of code but I cannot seem to figure out why the enemies won't move towards the player :confused:
Any help would be greatly appreciated!
 
Top