An item with the same key has already been added. (Pools)
Posted: Tue Mar 01, 2011 12:16 am
I create a number of boxes and place them in a pool during my load method:
Now I take a box from the pool:
Use it in the Space for a random period and then return it to the pool:
The next time I take the same box from the pool and it is involved with a collision, the following error message occurs during the space.Update method:
Code: Select all
box = new Box(position, size, size, size, 1);
Code: Select all
public void Spawn(Matrix worldTransform)
{
isValid = true;
colourTimer.Reset();
colourTimer.Start();
box.WorldTransform *= worldTransform;
// Force inertia tensor to update
box.BecomeDynamic(box.Mass); // Can change collision group
box.IsAffectedByGravity = false;
game.Space.Add(box);
}
Code: Select all
public void Kill()
{
isValid = false;
colourTimer.Stop();
// Reset entity (remove all forces)
box.LinearVelocity = new Vector3();
box.AngularVelocity = new Vector3();
box.WorldTransform = Matrix.Identity;
game.Space.Remove(box);
}
How can this be avoided with pooled entities?An item with the same key has already been added.