n00b programmer
n00b programmer

Reputation: 2701

Game Maker 2 path breaks when changing rooms

I'm making a game in GMS 2. As part of the game, I have on object that is following a path, and I want it to continue following the same path after I change rooms. I noticed that this doesn't work - the path continues as usual, but the X and Y coordinates of the object change completely, for no reason at all - i tried this at debug mode, and at some point they just change. This only happens if I change the rooms while the path is active. This is the path creation code:

if (mp_grid_path(my_grid, my_path, x, y, dest_x, dest_y, 1)) {
            path_start(path, ny_speed, path_action_stop, false);
        } else {
            show_debug_message("no path!!!");
        }

Upvotes: 0

Views: 204

Answers (1)

n00b programmer
n00b programmer

Reputation: 2701

The way I found to resolve this, is to save the path at room end, and then restore it from the same point, at room start. At room end:

if(is_walking) { // only do this if the object is currently walking
path_end();
}

At room start:

if(is_walking) { // only do this if the player is currently walking
if (mp_grid_path(my_grid, my_path, x, y, dest_x, dest_y, 1)) {
    path_start(path, my_speed, path_action_stop, false);
} else {
    show_error("no path!!!", true); // if there is no path at this point, we have an error - because it existed before we changed the rooms, so it should exist now
}
}

Hope this will help anyone who gets stuck on this in the future.

Upvotes: 0

Related Questions