Search code examples
memory-managementframe-rateinstances

How can I avoid decreases in framerate in a game with a lot of instances of an object class?


I'm writing a game that requires a large amount of instances of a "Monster" class that contains very basic data - namely:

  • Angle of rotation
  • Damage potential
  • Max health
  • Current Health

And that's about it. The game starts out smoothly at 60 fps but once about 50-60 instances are created (and are active at the same time) the game dips to <30 fps. It's frustrating because I don't know how to fix it. For the record, I am removing the monsters from both the array in which they're contained and the scene in an efficient manner (I think).


Solution

  • One way to improve the framerate is to avoid creating and destroying a lot of objects. This can be achieved by pooling the objects. Basically you create the objects you need at the start of the game or level and when one is supposedly destroyed, you hide it and reuse it later.

    Depending on the technology you're using (which you haven't specified in your post ^^), there are various ways to implement this. You can check those links for more info and help: http://gameprogrammingpatterns.com/object-pool.html http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/objectpool.html and/or just google it. The principle is pretty simple and you should be able to use it pretty easily.

    Hope this helps ;)

    edit: Also, depending on the technology you're using, there might be some better ways to improve framerate. If you could provide the languages / APIs you're currently using, you would probably get more useful help !