GML [SOLVED] help with efficiency to keep frame rate from going down.

Discussion in 'Programming' started by BananaPandaDemographic, Jul 23, 2019.

  1. BananaPandaDemographic

    BananaPandaDemographic Member

    Joined:
    May 26, 2018
    Posts:
    39
    So I have a Pac-Man like game and it's functional but the frame rate goes down when too many enemies are seeking out the player. there are only 10 enemies on the board at one time plus the Minotaur who is always chasing down the player. I think maybe grid pathfinding might help but I would have to learn how to use them.

    I do know that when I tried to put (if player exists) instead of (collision circle) that the frame rate almost crashed the game. I also think game states could help but not sure where or how to implement them.

    Enemy step:
    Code:
    var myPath = path_add();
    if (collision_circle(x,y,576,objPlayer,false,true)) {
        mp_potential_path(myPath,objPlayer.x,objPlayer.y,4,4,false);
        path_start(myPath,4,path_action_stop,true);
    }
    
     
  2. Geners

    Geners Member

    Joined:
    Jan 28, 2017
    Posts:
    31
    Is your player sprite using precise collision?
     
  3. BananaPandaDemographic

    BananaPandaDemographic Member

    Joined:
    May 26, 2018
    Posts:
    39
    my player is using automatic/ rectangle with no animation. i do have a floating sword sprite that's rotated by mouse position, which is also automatic/ rectangle. the precise collision mask that i do use is on the slash sprite, with no animation.
    33d15bd1-d828-4178-8a1e-52733ae08c36.png 4bdb431d-2312-414d-8042-e1c4bfd810a7.png 5ca612c7-cedc-408c-8a1b-08ae0c2e4dcc.png
    player draw instance:
    Code:
    draw_self();
    var dir = point_direction(x,y,mouse_x,mouse_y);
    draw_sprite_ext(sSword,0,x,y,1,1,dir,image_blend,image_alpha);
    
     
  4. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    415
    states and other AI improments may help, but I think the main culprit is that collision_circle. I would try replacing that with a basic distance check for pursuit. distance_to_object
     
  5. Geners

    Geners Member

    Joined:
    Jan 28, 2017
    Posts:
    31
    I agree with Curato.

    Use distance_to_object and then check if that distance is less than the radius of the detection circle you want to use. This will have a similar effect as having a collision circle.
     
  6. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    You should begin by not creating a new path every step, that can't be good for your framerate or memory. Then, if you still have issues, you can make path recalculation rarer. I would begin by only recalculating the path every 5 steps or so, and only doing so if the player has moved a decent amount.

    I don't think collision_circle is the culprit, even if it's ten times slower than a simple distance check (which is unlikely, it's probably just a rectangle-to-circle intersection test) it's still only called 11 times per step.
     
  7. BananaPandaDemographic

    BananaPandaDemographic Member

    Joined:
    May 26, 2018
    Posts:
    39
    if (distance_to_object(objPlayer) < 576) acted pretty much the same with the same frame rate loss with more enemies closing in. but i get what you are saying, this is checking for less information than collision circle.
     
  8. BananaPandaDemographic

    BananaPandaDemographic Member

    Joined:
    May 26, 2018
    Posts:
    39
    yea that's what i was thinking when i was thinking about putting in states. where the state is collision checking and then switches to pursue. but i'm not sure how i'd have it check every five steps. maybe with an alarm or count down
     
  9. JackTurbo

    JackTurbo Member

    Joined:
    Oct 19, 2016
    Posts:
    824
    Creating a new path every step is overkill and deffo a big resource hog. Do it in an alarm and call that at random intervals. I have my AI tick around every third of a second +/- 10 frames.

    Randomising it a little helps spread the load out across multiple steps so you don't get certain steps where all enemies pathfind
     
  10. BananaPandaDemographic

    BananaPandaDemographic Member

    Joined:
    May 26, 2018
    Posts:
    39
    so i dont really know how to do alarms correctly but someone helped me with this code to fix another problem with my alarms. so far it's fixing the frame rate drop but i cant use it everywhere like with the minotaur, where i have him go faster when he is closer and with the delay he pauses between cruising and chase speeds while cycling through the .2*room_speed. lowering that number helps with the delay but i dont quite remember how to put my count down by frame instead of room_speed for this equation. i'm sure it'll fix it if i set it to about 5 frames count down though. otherwise, it's running way smoother.

    Enemy create:
    pathcheck = .2*room_speed;

    enemy step event:
    Code:
    if (distance_to_object(objPlayer) < 576) {
        if (pathcheck > 0){
            pathcheck -= 1;
        }
        else
        {
            mp_potential_path(myPath,objPlayer.x,objPlayer.y,4,4,false);
            path_start(myPath,4,path_action_stop,true);
            pathcheck = .2*room_speed;
        }
    }
    
     

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