Refactoring and improving performance. Update #7


This week no big visible changes from last update. After testing my last build more I realized I got huge fps drops all the time. So I decided to work on improving performance. After some quick searches of how to improve performance and where to look this is what helped me:

One of the biggest changes was implementing object pooling.
Without going into too much detail I figured that allocating memory for every projectile being shot just to remove it from memory second later might be the cause. Instead they should be just disabled when they hit and later re-enabled as new projectiles from a different unit (or the same one). Luckily this could be done with minimum changes.

First I created method GetPoolItem() which both returns pooled item or creates new one if there is no free to use object of that prefab type. Then I had to add Some sort of OnResuse() function to make sure object is set properly as now I can't depend on Unity's Start() method.

Other huge benefit came from trying out Unity Profiler Tool. With this I could better analyze what else causes frame drop and work from there. It was a little confusing at first, a specially when at first it seemed that performance issued were outside of my code.

But I dug deeper and realized I had a problem with unit attack script. To detect if unit is within attack range I used collider set a trigger. When something entered the trigger the script would look if the object had a GameEntity script attached to it. I didn't know GetComponent was considered resource cost intensive method. But worst of all, trigger collider component was left inside an object that was in default layer so it got triggered by literally everything - not just units, but also projectiles, trees and such. Just creating a new layer for range triggers that collide exclusively with GameEntityies was enough to get additional boost in performance.

hope this helps someone else as it helped me.

Files

rts.zip 70 MB
Oct 07, 2023

Get rts prototype

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.