Luke Pierson
Member
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:
this is the code in the level generation grid that spawns the enemies:
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:
Destroy Event:
Alarm Event:
Step Event:
Room End Event:
-----This is the script for the pathfinding:
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
Any help would be greatly appreciated!
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);
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);
}
}
}
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;
Code:
/// Destroy the path
if (path_exists(path)) {
path_delete(path);
}
Code:
/// Move towards the player
get_path_to_player();
alram[0] = room_speed;
Code:
/// Update the depth
depth = -y;
Code:
/// Destroy the path
if (path_exists(path)) {
path_delete(path);
}
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);
}
}
Any help would be greatly appreciated!