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

GML [SOLVED] NPC slightly phases through walls?

Dr_Nomz

Member
I have a good path finding AI that allows an NPC to move pretty well:
Code:
///STEP
  if !place_meeting(x,y,obj_Character){
  move_speed=5
  mp_grid_path(global.grid,global.path,x,y,obj_Character.x,obj_Character.y,true)
  path_start(global.path,move_speed,"",true);
    if (distance_to_object(obj_Character) > 2000){
      status = npc_state.npc_normal;
    }
  }
But sometimes when turning corners it just kinda goes through the wall a little bit, even though it's supposed to hug the wall, or at the very least not be inside of it.

Any way I can fix this?
EDIT: I also have a walking code for the player that might help
Code:
var i=run_speed; while !place_free(x,y-i) i--; y-=i;
var i=run_speed; while !place_free(x,y+i) i--; y+=i;
var i=run_speed; while !place_free(x-i,y) i--; x-=i;
var i=run_speed; while !place_free(x+i,y) i--; x+=i;
Now I don't want to change the NPC code drastically since it works fine, but any chance I can use the same concept here to keep it from sticking into walls?
The code I'm talking about is the part where the while loop keeps the player from being inside of a wall, because if he gets stuck in one for some weird reason, moving just 1 pixel shoots them out on the correct side of the wall.

EDIT2: Actually tested the while loop, can make the NPC shoot out the wrong direction and teleport. <_< Other solutions, or just accept the fact that it partially moves inside of walls?


ALSO could this cause a memory leak the way it's written? See, it calls code from a control object at the start of the game:
Code:
///CREATE
global.grid = mp_grid_create(0,0,room_width/50,room_height/50,50,50);
global.path = path_add();
mp_grid_add_instances(global.path,obj_Wall,1)
I heard from the tutorial I pulled this from that it could cause that, so I altered slightly to hopefully prevent that. If I got it wrong it would be really good to know.
 
Last edited:

marasovec

Member
I think the memory leak is because you are running the pathfinding code in every step. Run it every half a second or every second
 

Dr_Nomz

Member
I think the memory leak is because you are running the pathfinding code in every step. Run it every half a second or every second
I'm not asking how to "fix" a memory leak, I'm just asking if how I've done could CAUSE a memory leak.
 

Dr_Nomz

Member
Updated thread, just want the first question answered, but if you can answer the second one too that'd be great.
 

TheouAegis

Member
So two issues here.

The mp_grid functions just plot a path through the grid. They have nothing at all to do with instances; the grid and the path plotted through it are all independent of any instances in the room, including the instances following those paths. The instance will be placed with its origin centered on the path and then follow that path absolutely. If you don't want the instance to clip the walls, you need to make either the instance smaller or the walls smaller, either of which will increase the gap present when not clipping.

------------------------------------------------------------------------

You shouldn't need to worry about a memory leak if that's in the Game Start event, since that event will get run only one time in the very first room of the project at the start of the game after all the instances in that first room have been created. If that code is anywhere other than the Game Start event, though, it could potentially cause a memory leak if you accessed that room multiple times, thus creating multiple paths.

If you use game_restart() at all, you're going to have a memory leak anyway (at least prior to Studio 2, not sure if they fixed the game_restart leak), but it could potentially be an even faster leak if you don't explicitly destroy both the grid and the path before restarting the game.
 

Dr_Nomz

Member
I recently saw another tutorial that better explained path finding, so the memory problem shouldn't be an issue anymore, thanks for for explaining all that. :D
 
Top