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

NPC Wander Script Algorithm

C

ChaosX2

Guest
Hey everyone,

I'm trying to think on how to go about making an npc wander every so often. This would be dependent if the cells within the mp_grid is free or not. I was thinking of something along the lines of using place_meeting or place_free to see if up to two cells are free in each direction of the npc. I was thinking of maybe using an array (store 0, 1, or 2) to determine if there is no collision in each direction.

Code:
if (place free two cells to right)
npc_dir[0] = 2;
else
if (place free one cell to right)
npc_dir[0] = 1;
else
npc_dir[0] = 0;

if (place free two cells to left)
npc_dir[1] = 2;
else
if (place free one cell to left)
npc_dir[1] = 1;
else
npc_dir[1] = 0;

if (place free two cells to up)
npc_dir[2] = 2;
else
if (place free one cell to up)
npc_dir[2] = 1;
else
npc_dir[2] = 0;

if (place free two cells to down)
npc_dir[3] = 2;
else
if (place free one cell to down)
npc_dir[3] = 1;
else
npc_dir[1] = 0;

Now lets say it's free to move two cells in the left and right direction but not up and down. I would then randomly spawn a target for the npc to follow either on the left or right side. This also would randomly spawn either one or two cells next to the npc but only one cell in the event that any part of the array has a value of 1. Once spawned, it would follow the target using mp_grid_path. When the npc reaches the goal, it would destroy the target and repeat all within the grid space generated for the npc.

Any advice on how to go about this either in the way I mentioned or a better way?
 
T

TimothyAllen

Guest
If your using an mp_grid why bother with the collision functions. Simply check the mp_grid cells to see if they are occupied... assuming you are correctly using the mp_grid.
 
Top