Issue with pooled objects in Ace On Steroids

Jul 18, 2012 at 12:37 AM

I've run into this issue with my code which also affects to the AOS source which i've modelled my code structure on.

In the bullet factory AOS uses a pool of bullet SceneObjects. In the Create method of the bullet factory it retrieves an instance from the pool _bulletsPool.New() and then adds a bullet agent bullet.Components.Add(SessionManager.CurrentSession.CreateNonPlayerAgent<BulletAgent>()); I can't find anywhere in the AOS code where this component is removed possibly meaning that the bullet has multiple copies of the agent component - (if it's not removed then this is another issue). Then in the OnAddedToParentObject method of the bullet agent it adds a bullet collision behaviour Add(_bulletCollisionBehavior) which in turn runs this code in it's Initialize method

_bullet = (Bullet) Agent.ParentObject;           
_bullet.CollisionTriggerEvent += OnBulletCollided;

This adds an event handler to the pooled SceneObject every time a bullet is initialized as the sceneobject is re-used this means that the collision trigger event handler is called multiple times per event. In AOS this never becomes visible since the bullet hits the asteroid and kills it, however it becomes apparent if your bullet only does a small amount of damage - eventually it becomes a superbullet capable of decimating small cities in one go since the trigger event handler is called repeatedly per collision.

There doesn't appear to be a nice way of handling this since there is no built in mechanism for identifying whether a SceneObject returned from the Sunburn PooledObjectFactory is a newly created object or a recycled one. I've ended up adding an initialized flag to my pooled entities so i only do things necessary to recycle the object if it has already been initialized.