Jump to content


Photo

Framerate drops in TD game


  • Please log in to reply
10 replies to this topic

#1 MATTHEWGS

MATTHEWGS

    GMC Member

  • GMC Member
  • 81 posts

Posted 08 March 2012 - 03:10 PM

I'm making a Tower Defense game. The towers and creeps I've created all function properly. There is one big problem: When a new wave of enemies starts, and there is about 30 creeps in the view, the framerate drops dramatically, until the point where the game freezes. I've tried running the game in debug mode to see if there were too much instances of objects created, but there was only about 130 instances when the game froze, which seems about right, and instances were destroyed when they arrived their destination point. I've tried running the game without the background tiles, without the creep health bars, without any towers on the map, but with no difference in the framerate.
What could be the cause for the framerate drop? Any help would be appreciated.

Also something weird happened: I tested the game, and started the wave. The framerate dropped when there was about 30 creeps in the view. Then I pressed a button to restart the room. I started the wave again, but now the framerate started to drop when there was about 20 creeps on screen. I restarted the room again and started the wave, and the framerate started to drop even earlier. How could this possibly happen?

Thanks in advance!

Edited by MATTHEWGS, 11 March 2012 - 05:40 PM.

  • 0

#2 kelson

kelson

    GMC Member

  • GMC Member
  • 323 posts

Posted 08 March 2012 - 03:33 PM

whithout seeing any of the code it is really hard to say i would be there is a looping code in your step even of your creeps that slows down the game. but i couldnt tell you what it is without looking at it. but with an entier game slowing down, and you dont know why it would be best to let someone see the whole gmk file. so if you want me to take a look at it pm me the file and i give you my word that all i will do is see if i can find the problem.
  • 0

#3 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 15936 posts
  • Version:GM:Studio

Posted 08 March 2012 - 10:32 PM

drawing text?
using particles?
badly implemented collision code? like ray tracing?
  • 0

#4 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21726 posts
  • Version:GM:Studio

Posted 08 March 2012 - 10:44 PM

Are you using mp_grids? If you are, do you destroy them as well when they are no longer needed? And paths? Do you create paths dynamically and then destroy them too? You really do need to let us know how you are making the system work as well as if you are creating any type of resource that can cause a memory leak like paths, particles, data structures etc...
  • 0

#5 MATTHEWGS

MATTHEWGS

    GMC Member

  • GMC Member
  • 81 posts

Posted 09 March 2012 - 10:48 PM

Thanks for the comments guys. The link to a GMK file is here: http://www.mediafire...owerdefense.gmk
Please let me know if you find what's causing the FPS drop.
  • 0

#6 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 15936 posts
  • Version:GM:Studio

Posted 09 March 2012 - 11:37 PM

change you draw to
draw_sprite(spr_orc,image_single,x,y);

if(bbox_left < 0) exit;
if(bbox_top < 0 ) exit;
if(bbox_right > room_width) exit;
if(bbox_bottom > room_height) exit;
multiplier = 100/this_creep.creep_maxhealth;
draw_healthbar(x-10,y-18,x+10,y-14,this_creep.creep_health*multiplier,c_black,c_red,c_green,0,1,1);

Pretty sure you have a slow PC and it does not like that draw_healthbar.

actually. remove the draw event from all your creaps.

Add the draw event in the creep parent
draw_sprite(sprite_index,image_number,x,y);

if(bbox_left < 0) exit;
if(bbox_top < 0 ) exit;
if(bbox_right > room_width) exit;
if(bbox_bottom > room_height) exit;
multiplier = 100/this_creep.creep_maxhealth;
draw_healthbar(x-10,y-18,x+10,y-14,this_creep.creep_health*multiplier,c_black,c_red,c_green,0,1,1);


you can probably do the same for all your creep events that are the same in all the creep types. move the code to the parent, make the code more generic like I did with the sprite_index image_number bit


You can also remove the precision Collision from your sprites, given you use
// makes sure the tower turns to the right direction
image_single = direction;

// following the closest creep passing if it comes within range
if(distance_to_object(obj_creep_basic) < towerrange) {
    direction = round((point_direction(x,y,instance_nearest(x,y,obj_creep_basic).x,instance_nearest(x,y,obj_creep_basic).y)/360)*32);
}

// fires a bullet when the creep comes within tower range
if(distance_to_object(obj_creep_basic) < towerrange) {
    if(fire_bullet == true) {
        instance_create(x,y,obj_ranger_bullet);
        fire_bullet = false;
    }
}
to find the instance. distance_to_object is a little intensive, precission OF may make it faster. but here is the optimal code

creep create, add
radius = point_distance(0,0,sprite_width,sprite_height) /2;

and your range finder code above can be changed to
ins = instance_nearest(x,y,obj_creep_basic);
if(ins == noone) exit;
if((point_distance(x,y,ins.x,ins.y) - ins.radius) < towerrange)
{
    direction = point_direction(x,y,ins.x,ins.y);
    if(fire_bullet == true) {
        instance_create(x,y,obj_ranger_bullet);
        fire_bullet = false;
    }
}

  • 0

#7 MATTHEWGS

MATTHEWGS

    GMC Member

  • GMC Member
  • 81 posts

Posted 10 March 2012 - 08:49 AM

Thanks for the suggestions guys!
I'm not sure the healthbars or ranger finder codes are the problem though... I've tested the game with the first wave set to 50 creeps, and I've tested it with towers on the map, without towers, with healthbars, without healthbars etc. with no difference. The framerate still dropped every time.

I'll change the code though, since using more generic code is probably a bit faster :)

Any other suggestions?
  • 0

#8 DanRedux

DanRedux

    GMC Member

  • GMC Member
  • 1403 posts
  • Version:GM:Studio

Posted 10 March 2012 - 09:16 AM

Your obj_gnome is creating the mp_grid continuously. That needs to change as it's causing ridiculous lag.
Create the grid one time at the beginning of each level (room start event) and have all creeps use the same grid.

About a minute of running and I had thousands of mp_grid's in memory, slowing down everything.
  • 0

#9 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 15936 posts
  • Version:GM:Studio

Posted 10 March 2012 - 06:55 PM

I missed that.

You only need 1 mp grid. In a controller object. you create it on room start and free it on room end.

Your creep can access the mpgrip with ObjGridHandler.TheGameGrid.

If you plan to change the grid like the original tower defense did, then simply cause an update when you add a new tower.
  • 0

#10 MATTHEWGS

MATTHEWGS

    GMC Member

  • GMC Member
  • 81 posts

Posted 11 March 2012 - 05:39 PM

OK, I now have 1 grid created at the start of the room. Every creep uses this grid.
In every creep's Create Event I now have creep_path = path_add();
The grid is created by obj_create_grid, and the script scr_create_path only uses the grid.
This solved the problem!

Thanks a lot for all the replies and suggestions guys!

Edited by MATTHEWGS, 11 March 2012 - 05:39 PM.

  • 0

#11 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 15936 posts
  • Version:GM:Studio

Posted 11 March 2012 - 07:59 PM

Don't forget to delete the path when the creep dies and when you leave the (non persistent) room.

Same thing with the mp_grid.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users