- Home /
Freeze lag with object pool/recycler, why?
I am firing bullets using raycasts and I am placing bullet holes consisting of planes with decals on them at the hit point of the raycast.
To avoid instantiating GameObjects during gameplay, I created a simple object pool. My ObjectPool class has an instantiateBulletHoles function that I call from from my gun script on Awake() that creates a specified number of bulletHole GameObjects and puts them in an ArrayList
function instantiateBullets(newBulletHole : GameObject, machinegunBulletHoleCache : int){
numberOfBullets = machinegunBulletHoleCache; machinegunBulletHoles = ArrayList(numberOfBullets);
for (i=0; i < numberOfBullets; i++){
instantiatedBulletHole = GameObject.Instantiate(newBulletHole); machinegunBulletHoles.Add(instantiatedBulletHole); } }
This seems to work fine, my bulletHole game objects are created as soon as I start the game. Then, in my OnHit function in my gun script which fires when my raycast hits something, I call the getBulletHole function of my ObjectPool, which takes the next bullet hole in my ArrayList and places it at the hit point. If there are no more available bullets, it loops back around and recycles the first bullet hole in the list.
function getBulletHole(position : Vector3, rotation : Quaternion) {
nextMachinegunBulletHole +=1;
bulletHole = machinegunBulletHoles[nextMachinegunBulletHole];
bulletHole.transform.position = position; bulletHole.transform.rotation = rotation;
if(nextMachinegunBulletHole > numberOfBullets-2){
nextMachinegunBulletHole = -1;
} }
The problem is that when I start firing I get a split second freeze lag. If I continue firing after that it keeps placing bullet holes smoothly, but if I stop firing for a few seconds and then start again, it freezes again. This happens both on my Android device and on my six core, 12gb RAM workstation (framerate drops from 600+ towards zero).
Does anyone have any thoughts on what might be causing this lag? Any pointers to what can be improved in this code is greatly appreciated.
Answer by kromenak · May 06, 2011 at 05:14 PM
Not sure if these are the causes of your problems, but here are a few ways you could possibly improve this code:
1) Since you know precisely the number of bullets you need to cache and that value never changes, it might be more efficient to use a built-in array to hold the bullets, like
var bulletHoles = new GameObject[numberofBullets]
2) As you are instantiating bullet holes, you can call gameObject.active = false to deactivate the bullets. Then, you just activate them as they are needed and then deactivate them when they hit their "fade out" time. If your bullets have children, you can use SetActiveRecursively to accomplish this as well.
You may have already tried this, but also take the bullet hole code out of the gun and see if you still get the issue - it could be something other than the bullet holes that cause the gun to seize up while firing.
Thanks for your reply, I tried storing the bullets in a built-in array ins$$anonymous$$d but it didn't make a difference, as a matter of fact you might have hit the nail on the head in your last line, something else entirely seems to be causing the lag.
Since we are on the subject though I have a question regarding your second suggestion, isn't activating and deactivating the bullet holes more costly than keeping them active and just moving them into position? I did it this way because I imagine there is some overhead to constantly activating and deactivating items?
Okay, so after some script diving I seem to have gotten rid of the lag, and it looks to me that what was causing it was a couple of Debug.Log functions I was using to check what my rays were hitting. $$anonymous$$akes sense I guess, I didn't consider the fact that it might be computationally heavy but at a high rate of fire it obviously seems to have a noticable impact.
Cool, good to hear you were able to fix it. Regarding the activating/deactivating stuff, I've read that it might be more costly to activate/deactivate than not, but I think it might also depend on your platform. I know it is pretty common to use deactivate unused objects on mobile devices, for example. I guess this is because the cost of keeping it active is more than the cost of switching between active and unactive. This might not be the case on desktops.
Answer by DudeWithPot · May 10, 2011 at 05:58 PM
thanks! this has helped me solve my problem. Actually i had used a print(); to output a number while my bullets get shot. this has caused a lag. thanks!