Game Development Community

How to properly use T2DSpawnObject [RESOLVED]

by Randy Lutcavich · in Torque X 2D · 10/02/2009 (2:43 am) · 2 replies

Functionality:
I had a spawner that created enemies just fine but I wanted to wait to start spawning objects instead of spawning them when the scene loads. So I turned the spawner object into a template. But when I attempt to clone this template and place it in the scene, nothing spawns in the game.

Problem:
How do I clone a SpawnerObject template? It doesn't seem to be spawning anything with the code below but it also doesn't crash.

Code:
// look up the template for the spawn object that already exists in TXB object
            T2DSpawnObject _enemy1SpawnerATemplate = TorqueObjectDatabase.Instance.FindObject<T2DSpawnObject>("Enemy1SpawnerATemplate");

            if (_enemy1SpawnerATemplate != null)
            {
                // clone the template to create the spawner object
                T2DSpawnObject _enemy1SpawnerA = (T2DSpawnObject)_enemy1SpawnerATemplate.Clone();

                // place the player object
                _enemy1SpawnerA.Position = new Microsoft.Xna.Framework.Vector2(0, 10);

                _enemy1SpawnerA.Name = "Enemy1SpawnerA";
                // register the spawn object with the TorqueObjectDatabase
                TorqueObjectDatabase.Instance.Register(_enemy1SpawnerA);
            }

Solution:
The game was actually crashing and the assertion line told me how to fix it. I had to add in a CopyTo method to the T2DSpawnObject.cs file.

T2DSpawnObject obj2 = (T2DSpawnObject)obj;
obj2.SpawnTemplate = SpawnTemplate;
obj2.SpawnOnce = SpawnOnce;
obj2.MinSpawnTime = MinSpawnTime;

#1
10/02/2009 (3:21 am)
I take it back, it does actually crash. I read the assertion and it had me add in a CopyTo to the T2DSpawnObject.cs file.

I added:
public override void CopyTo(TorqueObject obj)
        {
            base.CopyTo(obj);

            T2DSpawnObject obj2 = (T2DSpawnObject)obj;
            obj2.SpawnTemplate = SpawnTemplate;
            obj2.SpawnOnce = SpawnOnce;
            obj2.MinSpawnTime = MinSpawnTime;
            obj2.MaxSpawnTime = MaxSpawnTime;
        }

The code for the spawner looks like this:
// look up the template for the spawn object that already exists in TXB object
            T2DSpawnObject _enemy1SpawnerATemplate = TorqueObjectDatabase.Instance.FindObject<T2DSpawnObject>("Enemy1SpawnerATemplate");

            if (_enemy1SpawnerATemplate != null)
            {
                // clone the template to create the spawner object
                T2DSpawnObject _enemy1SpawnerA = (T2DSpawnObject)_enemy1SpawnerATemplate.Clone();

                // place the player object
                _enemy1SpawnerA.Position = new Microsoft.Xna.Framework.Vector2(0, 0);

                _enemy1SpawnerA.Name = "Enemy1SpawnerA";

                // register the spawn object with the TorqueObjectDatabase
                TorqueObjectDatabase.Instance.Register(_enemy1SpawnerA);
            }
#2
10/04/2009 (6:31 pm)
Looks great, Randy. Can you please email me? jkanalakis@envygames.com

John K.
www.envygames.com