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

Object references question

A

Alan Fuller

Guest
Hey all. Pretty basic question here. I'm making a zombie video game and the mobs move off of the motion planning functions listed below.

grid = mp_grid_create(0, 0, room_width/16, room_height/16, 16, 16);
path = path_add();
mp_grid_add_instances(path, obj_wall, false);
mp_grid_path(grid, path, x, y, obj_player.x, obj_player.y, true);
path_start(path, spd, "", 0);

This is in the step of event of the zombie object. My question is this. The zombies get stuck on one another and then they can't move, so I was going to try to and another mp_grid_add_instances that would make zombies avoid other zombies. The line of code would probably look like this.

mp_grid_add_instances(path, obj_zombie, false);

If I do this though, the zombies don't move at all. I think the problem is that the zombie are trying to avoid themselves. Is there any way I can make each zombie only reference the other zombies?
Thanks.
 

NightFrost

Member
Not sure why your zombies would be getting stuck on each other, as all they should be doing is following a path. That's a lot of unnecessary work for the Step event though. Your map is static so both the grid and instance stuff would only need to be done once, and since the map is same for all zombies, you don't need to do that separately for each either. You are also recalculating the path every step. Optimally you should be doing that only when end of path has been reached or the target (player) moves to another grid cell, requiring a path refresh. In fact it could be this repeat recalculation that is throwing movement off; I've never done it myself that way so I don't know how GMS reacts to it.

The second problem on the other hand. You are blocking off every grid cell that contains zombies. If you take a closer look at the mp grid documentation, you'll notice that it works at grid cell level. The cells are either open or closed, and you are telling the game to close off every cell that contains zombies. So not only are most of the paths likely blocked off, the zombies themselves are standing inside walls as far as motion planning is concerned. The short of it is, you cannot use mp grid stuff to create entity avoidance. It is a more complicated issue that requires something like steering behaviors.
 
A

Alan Fuller

Guest
Not sure why your zombies would be getting stuck on each other, as all they should be doing is following a path. That's a lot of unnecessary work for the Step event though. Your map is static so both the grid and instance stuff would only need to be done once, and since the map is same for all zombies, you don't need to do that separately for each either. You are also recalculating the path every step. Optimally you should be doing that only when end of path has been reached or the target (player) moves to another grid cell, requiring a path refresh. In fact it could be this repeat recalculation that is throwing movement off; I've never done it myself that way so I don't know how GMS reacts to it.

The second problem on the other hand. You are blocking off every grid cell that contains zombies. If you take a closer look at the mp grid documentation, you'll notice that it works at grid cell level. The cells are either open or closed, and you are telling the game to close off every cell that contains zombies. So not only are most of the paths likely blocked off, the zombies themselves are standing inside walls as far as motion planning is concerned. The short of it is, you cannot use mp grid stuff to create entity avoidance. It is a more complicated issue that requires something like steering behaviors.
Thanks for the input. I probably can take the grid creation out of the step event because it is static, but I think I need to have the mp_grid_path(grid, path, x, y, obj_player.x, obj_player.y, true); in the step event so the zombie will constantly be updated to where the player is. If using the motion planning functions won't work, any advice on creating a topdown AI that will avoid walls?
 

NightFrost

Member
Yes, you would need to run the pathing command to update to player's current location, but you can lighten the workload by having it work more intelligently. The path only needs to be updated when player has moved from one grid cell to another. If they're standing still, the path wouldn't require any updating. Your grid appears to be 16x16 pixels. If you take player's coordinates, divide both by that and round down, you get their grid coordinates. You could calculate these in player step event after movement has been handled, compare to previous position, and tell the zombies to update their paths only if position has changed. Something like:
Code:
// Create
Prev_X = floor(x / 16);
Prev_Y = floor(y / 16);
global.Path_Update = false;

// Step
var New_X = floor(x / 16);
var New_Y = floor(y / 16);
if(New_X != Prev_X || New_Y != Prev_Y){
    global.Path_Update = true;
    Prev_X = New_X;
    Prev_Y = New_Y;
} else {
    global.Path_Update = false;
}

// Zombie step
if(global.Path_Update == true){
    mp_grid_path(grid, path, x, y, obj_player.x, obj_player.y, true);
}
Motion planning is adequate for avoiding walls, its just that when you start following the calculated path, you are locking yourself to that path, which makes any kind of avoidance impossible. A step up from that would be to consider the path only as directional guidance, and have the zombie walk manually towards target. This would make it possible afterwads to add code that tries to avoid other zombies.
 
Top