Object Pooling?

Monkey Forums/Monkey Programming/Object Pooling?

Amon(Posted 2011) [#1]
Does anybody have a basic idea of implementing this for bullet firing? Am I correct in thinking that I can just stop the bullet moving when offscreen, making sure it has an ID first, then send it back to say the ships current coordinates to be used again?

Is this what is meant by Object Pooling?

Reason I ask is because I noticed some lag when lots of bullets were fired from my ships position; I then researched why and found out that creating new objects on the fly is a performance hit for phones and to use object pooling, object pooling being the reuse of objects already created.


AdamRedwoods(Posted 2011) [#2]
I think I used a for-next loop to find an empty spot, say a "dead" bullet from a class array[] of however many bullets (20-30?).

bullet.alive = 0 ''dead from hitting something or offscreen
bullet.alive = 1 ''moving or begin firing


I guess another method would be to keep two Lists<>, one would be live bullets, the other is dead bullets. You would still use a class array[] for all the bullets. Move the bullets from list to list as they change state. (although one thought is that lists may be making and destroying objects as well, probably not as much overhead).


therevills(Posted 2011) [#3]
Basically create all your objects (ships, bullets, aliens, particles etc) before your game is in its main loop, in Monkey (if using mojo) you would want to do this in the OnCreate method.

So say you know that there are only every going to be 10 bullets on screen, create 10 bullets in OnCreate - then when the player fires a bullet use the first one, if the player fires a second one use the second one - then the first bullet goes off the screen and the player fires another bullet reuse the first bullet again. This way you are never creating a new object at runtime...

Mark actually posted a couple of examples:

A Linked list approach
http://www.monkeycoder.co.nz/Community/post.php?topic=860&post=7067

A Double buffered approach:
http://www.monkeycoder.co.nz/Community/post.php?topic=860&post=7069


Playniax(Posted 2011) [#4]
Does anyone know for certain when an object is removed from a list but 'kept alive' in another list if this causes a performance hit also?


ziggy(Posted 2011) [#5]
On XNA it does not cause any performance hit.


Tibit(Posted 2011) [#6]
The way I do it:

Instead of: new Bullet
I go: Bullet newBullet = BulletPool.Pop()

After removing it from the list I go:
BulletPool.Push(newBullet) and since not all references is remove no GC action is taken

However I do this as little as possible since this is effectivly the same as using pointers - you need to micromanage your memory. It gives speed in some situations, but it does take more time and can lead to hard to catch bugs since you are re-using old objects, and if an "dead" object is reused and some other thing in your game still references it even tough it should not, you are in trouble ;)

For particle systems I have found this especially beneficial. And are not bullets "almost" particles? :)


Jesse(Posted 2011) [#7]

and since not all references is remove no GC action is taken


you are forgetting that whenever you add an item to a list, a node is created and a node is deleted when the object is removed.

on the game SURVIBALL I submitted here. I use a "storage List" and a "in use list" and move them as needed. I use it for the particles. although there are no noticeable hiccups I am sure that if the game runs long enough all of the residue memory holes left from the node removal will eventually start to cause problems with performance.


Tibit(Posted 2011) [#8]
Jesse, good point. I do not use a linked list. I use an Array, Queue or Stack.

As a sidenote I did make a pooled linked list, solving the problem once and for all :)


Playniax(Posted 2011) [#9]
Ok, as long as you keep a reference to an object the GC doesn't free it and your safe on this part (common knowledge I guess). And better to use a Stack instead of a List.