GML How do I prevent this AI from causing massive lag?

Discussion in 'Programming' started by Dr_Nomz, Dec 16, 2018.

  1. Dr_Nomz

    Dr_Nomz Member

    Joined:
    Oct 31, 2016
    Posts:
    429
    A while back I managed to get a good AI system working, but the problem is I can't run it without it draining massive amounts of CPU every step, and I can't figure out how to get it to only run every few steps without breaking.

    Tutorial I used:


    Code:
    Code:
    ///CREATE
    globalvar grid;
    
    grid = mp_grid_create(0,0,room_width/50,room_height/50,50,50);
    mp_grid_add_instances(grid,obj_Wall,true);
    Code:
    ///DRAW for showing the grid during gameplay
    if keyboard_check(ord("H")){
      draw_set_alpha(0.9);
      mp_grid_draw(grid);
      draw_set_alpha(1);
    }
    Code:
    ///GAME END to prevent memory leaks
    mp_grid_destroy(grid);
    Code:
    ///CREATE
    planner = instance_create(x,y,obj_Path_Planner);
    planner.follower = id;
    Code:
    ///END STEP
    if !place_meeting(x,y,obj_Character){
    motion_add(point_direction(x,y,planner.x,planner.y),1);
    if speed>5{
      speed=5;
    }
    
    target_x = obj_Character.x;
    target_y = obj_Character.y;
    
    with (planner){
      x=follower.x
      y=follower.y
      scr_NPC_Path(other.target_x,other.target_y);
    }
    
    }else
    speed=0;
    Code:
    ///WALL Collision to prevent clipping
    durr = direction;
    move_bounce_solid(true);
    motion_add(durr,speed/2.5);
    Planner Create event:
    Code:
    path = path_add();
    Script:
    Code:
    ///scr_NPC_Path(target_x,target_y)
    target_x = argument0;
    target_y = argument1;
    
    mp_grid_path(grid,path,x,y,target_x,target_y,true);
    path_start(path,100,path_action_stop,false);
    I tried to get this fixed before, but I didn't get anywhere so I'm putting ALL the code and tutorial I used here so all the necessary information is available.

    All I really need is a way to make the NPC work as normal, but without having to run the path finding code every step, or just any way to have good path finding AI without massive lag.
     
  2. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    408
    You could use an alarm to stop it happening too often. This is the basic gist of it:

    create event
    Code:
    do_path = false;
    alarm[whatever] = whatever;
    alarm whatever
    Code:
    do_path = true
    step event:
    Code:
    if do_path
    {build path
    alarm[whatever] = whatever;
    do_path = false;}

    It's just resetting itself. The step event triggers the alarm, and then shuts itself off by setting the condition of do_path to false. The alarm counts down, and sets do_path to true. The process repeats.

    I'm not too sure if you want to be deleting the path before it's recreated. The assumption is that it's simply being overwritten each time, but this might be a bad practice.
     
  3. Dr_Nomz

    Dr_Nomz Member

    Joined:
    Oct 31, 2016
    Posts:
    429
    Yeah I get that, but this:
    Code:
    with (planner){
      x=follower.x
      y=follower.y
      scr_NPC_Path(other.target_x,other.target_y);
    }
    being put in an alarm just makes the planner jump occasionally to the target_x/y, while the NPC stupidly tries to go through a wall, ignoring the path. So just simply putting an alarm on it won't work, unless there's some other way you wanted me to do it.
     
  4. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    408
    Code:
    if !place_meeting(x,y,obj_Character){
    motion_add(point_direction(x,y,planner.x,planner.y),1);
    if speed>5{
      speed=5;
    }
    I think this piece of code is your problem, but am not 100% certain. It is the only bit that I can see which might contradict the pathing.

    When you use an mp_grid and path it's obviously meant to go around the objects you place into the grid. It is possible to draw paths onscreen, so you can verify that they exist and are working as intended.

    If I'm reading your code correctly:
    1) planner paths to the object

    2) If there is no collision with obj_Character then the follower aims at the planner, and sets motion

    Code:
    path_start(path,100,path_action_stop,false);
    This bit of code is setting the path speed to 100. This is very fast. Imagine that the planner has reached a point on the path 100 ahead, where it has gone round a corner and you are telling the follower to move towards it. The follower is not checking that there is a wall ahead, since it is only aiming for the planner, and just goes straight ahead through the wall. The planner will have moved around the wall, but the follower will not.

    You have the bit of code where it is "bounced" off the wall, but if the planner is still around the corner when the path restarts then the follower will just keep bouncing off the wall as it aims for the planner.

    You also have the planner setting its x / y to the follower id x / y. I think this would see it jumping back and forth along the path, and seems a bit awkward.
     
  5. Dr_Nomz

    Dr_Nomz Member

    Joined:
    Oct 31, 2016
    Posts:
    429

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice