• 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!

Legacy GM Objects not following path

O

Orange Lightning

Guest
I am trying to create a top-down stealth game a lot like the original Metal Gear for the MSX2. It was all going so well until I ran into a little issue with some path-finding code. My efforts to fix the problem have often resulted in me rewriting code in unusual ways to desperately try to solve the problem. As it stands I am questioning the efficiency of my program.

My enemy objects have effectively got three modes: Patrol, in which they follow a predefined path continuously until they spot the player. Alert, in which they actively chase down the player. And Return to patrol, in which they return to that start of their patrol route.

The problem I am having is when the enemy objects are either on alert or return to alert. They have a tendency to get stuck. At first I observed them getting stuck on walls, and I figured there was something wrong with the grid (I still believe this to be the problem). But I have caught them getting stuck in the middle of a clear area on a few occasions.

Seeing as how this issue likely has to do with the layout of the room itself, I am wondering, first of all, how I can actually request any help with my project? Pasting portions of my code here may not be enough to allow anybody to help be get to the root of the issue if it has to do with collision masks, or the positions of items in the room compared to the grid.

Most of my code rests in events such as the step event. There are hundreds of lines of relatively messy code in my enemy objects, lots of it completely irrelevant to this issue. Although it is possible that some code somewhere has caused this bug, I don't think dumping it all here would be a good idea. Here are some isolated lines responsible for path-finding:

Code:
/*  Instance Variables
 *  grid = mp_grid_create(0, 0, room_width/16, room_height/16, 16, 16); (create event)
 *  enemyHealth = 3; (create event)
 *  instPath = a unique pre-defined patrol path depending on the instance of obj_enemy (create event)
 *  alert = 0; (create event)
 *  shoot = false; (create event)
 *  patrol = true; (create event)
 *  spd = 1.3; OR spd = 1.5; (create event)
 */

mp_grid_add_instances(grid, obj_rock, true);
mp_grid_add_instances(grid, obj_col_full, true);
mp_grid_add_instances(grid, obj_col_vertHalf, true);
mp_grid_add_instances(grid, obj_col_diagHalf, true);
mp_grid_add_instances(grid, obj_col_diagQuart, true);
mp_grid_add_instances(grid, obj_col_diag3Quart, true);

with(obj_enemy)
{
    if(id != other.id && enemyHealth > 0)
    {
        mp_grid_add_instances(other.grid, id, true);
    }
}

var cond = false;
if(instPath != noone)
{
    cond = !((x > path_get_x(instPath, 0)-1 && x < path_get_x(instPath, 0)+1) && (y > path_get_y(instPath, 0)-1 && y < path_get_y(instPath, 0)+1));
}

//If the player has been spotted, then this enemy will begin to follow a path to get the player
if(alert > 0 && enemyHealth > 0)
{
    shoot = false;
    patrol = false;
      
    path_end();
    pathX = path_add();
  
    if (mp_grid_path(grid, pathX, x, y, obj_player.x, obj_player.y, false))
    {
        //path_start(path, 0, 3, 0);
        path_start(pathX, spd*1.5, path_action_stop, true);
    }
  
    if(alert < (10 * room_speed))
    {
        path_speed = spd;
    }
}
else if(cond && patrol == false && enemyHealth > 0) //return to patrol
{
    shoot = false;
  
    path_end();
      
    pathV = path_add();
    if (mp_grid_path(grid, pathV, x, y, path_get_x(instPath, 0), path_get_y(instPath, 0), false))
    {
        //path_start(path, 0, 3, 0);
        path_start(pathV, spd, path_action_stop, true);
    }
}
I am sorry if this is not specific enough, or unclear. I am relatively new here, and have yet to get the hang of things. I would provide a download link of my project if I could.
 
Last edited by a moderator:
O

Orange Lightning

Guest
Bump.

I really do hope that somebody can help me with this. I've been stuck on it for the last 2 weeks, and it's proving to be quite the challenge. I spend every day on it, and am not closer to identifying the issue than I was 2 weeks ago.
 
Can you post a video / gif of the enemy behaviour in general and where they get stuck?

Do your wall / obstacle objects all fit neatly within the grid squares and not overlap the edges which could collide with an enemy causing it to get stuck?

Have you used mp_grid_draw to draw the grid and confirm it is mapping correctly to your objects?

If your objects sprites are origins are not set to 0,0 you can get out-of-alignment objects that do not fit properly in the grid squares?

Have you used draw_path in combination with mp_grid_draw to double-check what paths are being generated?

EDIT : Are you updating the grid with the enemy instances positions as they move around, and are you removing from the grid their previous position so that it frees up the space?
 
Last edited:
Top