Game Development Community

Remove Template Clone from Scene

by Stuart Walton · in Torque X 2D · 05/12/2008 (7:29 am) · 7 replies

I created a class for my enemy ship that i can call by using:

BlueShip ship1 = new BlueShip(-46, -28);

this creates a new enemy ship at given location on the scene from the BlueShip class:

public class BlueShip : TorqueComponent
{
public T2DSceneObject SceneObject
{
get { return Owner as T2DSceneObject; }
}
public float FireProbability = 0.02f;
public T2DSceneObject _enemyShip;

public BlueShip(int posX, int posY)
{
T2DSceneObject EnemysShip = TorqueObjectDatabase.Instance.FindObject("EnemyTemplate");
_enemyShip = (T2DSceneObject)EnemysShip.Clone();
_enemyShip.Visible = true;
_enemyShip.Position = new Microsoft.Xna.Framework.Vector2(posX, posY);


TorqueObjectDatabase.Instance.Register(_enemyShip);
}
}

What im struggling with is that i want all instances of _enemyShip to be deleted from the scene if the player dies.
Does anyone know a simple way to achive this?

#1
05/12/2008 (11:22 am)
In your BlueShip class:
public static List EnemyShips = new List();

in your constructor:
EnemyShips.Add(_enemyShip);

in your PlayerDied method:
foreach(T2DSceneObject ship in BlueShip.EnemyShips)
ship.MarkForDelete = true;


Alternately you could do a FindObjects() and iterate through each one checking to see if (object.Components.FindComponent() != null) and mark those for deletion.
#2
05/12/2008 (11:58 am)
:) Thank You Works spot on

Iv been struggling with this for days
#3
05/12/2008 (1:02 pm)
Well it works for the first wave of spawned enemies as long as none have been destroyed.

Game spawns 6 ships
If no enemy ships destroyed by player fire all enemy ships dissapear as expected.
if any enemy ships destroyed by player fire - Error Cannot mark object for delete if not added to database
If player is killed and all enemy deleted and then re created - same error
If player shoots all 6 enemy ships and then enemy ships are re created - same error.

error happens when try to MarkForDelete

i have 3 enemy classes so i added the code to each class
BlueShip = EnemyShip1 List
RedShip = EnemyShip2 List
GreenShip = EnemyShip3 List
#4
05/12/2008 (1:07 pm)
In your PlayerDied method add
BlueShip.EnemyShips.Clear();

after marking deletions so your list gets cleared out. It's trying to MarkForDelete on ships that were (partially) deleted last time.
#5
05/12/2008 (1:37 pm)
That fixed the error when all 6 ships exist or have all been killed and respawned but i still get the error if any of the enemy ships have been destroyed by player fire.

I have 2 blue ships if one of the ships is destroyed by player fire and the other ship kills the player i get the error - Cannot mark object for delete if not added to database

I presume that both blue ships are added to the list and when one is killed by the player it isnt removing one of the ships from the list. So when it tries to MarkForDelete that ship its creating this error?
#6
05/12/2008 (1:40 pm)
Yes. Add a check for IsRegistered when you're deleting, like this:

foreach(T2DSceneObject ship in BlueShip.EnemyShips)
if (ship.IsRegistered)
ship.MarkForDelete = true;
#7
05/12/2008 (1:48 pm)
That fixed it cheers :)

i had been trying

foreach(T2DSceneObject ship in BlueShip.EnemyShips)
if (ship != null)
ship.MarkForDelete = true;

Thank you for the help and fast response