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

GameMaker Motion Planned pathfinding isn't working

E

E.M.I

Guest
Hey there! I'm making a top-down adventure game with GMS2 and wanted to code a pathfinding system so the enemies in my game could avoid obstacles when chasing the player. However, the code doesn't seem to be working.
This is my code:
In my grid object's Create event:

Code:
globalvar enemyGrid;

enemyGrid = mp_grid_create(0, 0, room_width/16, room_height/16, 16, 16);

In the enemy's Create event
:

Code:
enemyPath = path_add();
In the enemy's Step event:

Code:
mp_grid_path(enemyGrid, enemyPath, x, y, obj_eitim.x, obj_eitim.y, true);
       
        path_start(enemyPath, 0.9, path_action_continue, true);
       
        if (path_start(enemyPath, 0.9, path_action_continue, true))
            show_debug_message("Working");
What's weird is that, when I try drawing the path I'm creating with the draw_path function, the path appears correctly and changes when the player object moves. However, the enemy just doesn't want to move.
Also, when I replace the if statement in the second-to-last line to check if mp_grid_path was executed correctly, it works. So the problem is in path_start.
Thanks!
-Emi
 
Don't call path_start() and mp_grid_path() every step.

Put it in an alarm or check if the enemy is already following a path, and don't call it again for at least a second or whatever is appropriate for your game.
 
if (path_start(enemyPath, 0.9, path_action_continue, true)) show_debug_message("Working");
That check is never going to work as path_start does not return a boolean value. Get rid of it.
And have you tried using the relative location instead of the absolute location? Try changing this:
Code:
path_start(enemyPath, 0.9, path_action_continue, true);
to this:
Code:
path_start(enemyPath, 0.9, path_action_continue, false);
And see if that makes any difference. All that does is have the object use the path relative to the objects current position rather than the absolute position.
 
E

E.M.I

Guest
That check is never going to work as path_start does not return a boolean value. Get rid of it.
And have you tried using the relative location instead of the absolute location? Try changing this:
Code:
path_start(enemyPath, 0.9, path_action_continue, true);
to this:
Code:
path_start(enemyPath, 0.9, path_action_continue, false);
And see if that makes any difference. All that does is have the object use the path relative to the objects current position rather than the absolute position.
Thanks for the help! Though, it didn't give any different results,
 
E

E.M.I

Guest
Don't call path_start() and mp_grid_path() every step.

Put it in an alarm or check if the enemy is already following a path, and don't call it again for at least a second or whatever is appropriate for your game.
Thanks for the suggestion! I'll try it out.
 
E

E.M.I

Guest
Don't call path_start() and mp_grid_path() every step.

Put it in an alarm or check if the enemy is already following a path, and don't call it again for at least a second or whatever is appropriate for your game.
Alright, so I tried adding a little system that made the amount of times that the path was created and used a lot smaller. The modified code in the enemy's Step event is:
Code:
if (!position_saved)
        {   
            eitim_pos_x = obj_eitim.x;
            eitim_pos_y = obj_eitim.y;
            position_saved = true;
        }
        
        if (alarm[0] == -1)
        {
            if (eitim_pos_x != obj_eitim.x or eitim_pos_y != obj_eitim.y)
            {
                mp_grid_path(enemyGrid, enemyPath, x, y, obj_eitim.x, obj_eitim.y, true);
                position_saved = false;
            }
        
            path_start(enemyPath, 0.9, path_action_continue, false);
            
            alarm[0] = room_speed * 5;
        }
In the enemy's Create event:
Code:
alarm[0] = room_speed * 5;
So what I did is, first off, make a timer that lasted 5 seconds (it's gonna be less in the actual game but this is for ease of testing) and checked wether or not the player's position (obj_eitim) had changed. If yes and 5 seconds have passed, another path is made and used, and the alarm is reset. However, when I add the alarm, the path is neither created nor used. Any idea what's happening here? Thanks!
 
Did you add an Alarm 0 Event to the enemy object? If you want to use the built in alarm variables, you need to have the event as well, with at least a comment in it, or the alarm will not count down.

From the manual entry on alarms:

"NOTE: An alarm with no actions or code in it will not run. However, even with just a comment and no code, the alarm will count down."
 
E

E.M.I

Guest
Did you add an Alarm 0 Event to the enemy object? If you want to use the built in alarm variables, you need to have the event as well, with at least a comment in it, or the alarm will not count down.

From the manual entry on alarms:

"NOTE: An alarm with no actions or code in it will not run. However, even with just a comment and no code, the alarm will count down."
Oh, that was a dumb mistake, sorry about that. The alarm works now and the paths are made as before, but path_start still doesn't work. Any other idea what it could be? Thanks a lot!
 
Can't think of anything at the moment.

Try commenting out all your path code, and just try running the mp_grid_path() and path_start() lines in the Create event.

Also you could show_debug_message(path_position) in your Step event to check that its value is increasing once the path has started.
 
Top