x


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.

more ▼

asked May 06, 2011 at 04:01 PM

Herman gravatar image

Herman
4 4 4 7

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered May 06, 2011 at 05:14 PM

kromenak gravatar image

kromenak
2.3k 154 70 87

Thanks for your reply, I tried storing the bullets in a built-in array instead 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?

May 07, 2011 at 02:50 AM Herman

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. Makes 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.

May 07, 2011 at 03:18 AM Herman

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.

May 08, 2011 at 01:11 AM kromenak
(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered May 10, 2011 at 05:58 PM

DudeWithPot gravatar image

DudeWithPot
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x45
x17
x11
x10
x1

asked: May 06, 2011 at 04:01 PM

Seen: 1828 times

Last Updated: May 06, 2011 at 04:01 PM