Game Development Community

Clone or New Object?

by Shaun Hyde · in iTorque 2D · 06/11/2011 (5:44 pm) · 2 replies

Hi All,

First time poster and still getting my feet wet in iTorque 2D looking for a push in the right direction after much stumbling around.

I could use some help with the following question to use clone with behaviors for spawning enemies or create them with a new object command from torque script.

I'll briefly talk about the requirements of the game: Have endless enemies spawning to a max of 72 enemies on the screen. They must spawn randomly so I am killing the objects once they die as opposed to just putting them in a queue to respawn.

My problem currently is that I have already setup the spawning of endless enemies via cloning and am now having difficulty referencing them. I understand that when I do something to the affect of %this.enemySpawn.cloneWithBehaviors(); I will have a object ID returned. I suppose I could store all the Object ID's in a global array (array being row/column) so that it is available to my other scripts but this seems to be bad coding. Main reason I need to reference each enemy is that based on a scheduled function I need to change the y position of all the enemies individually.

If there is any working documentation for going through and creating my own objects and classes completely from code and adding to the scene please advise. Note that I have looked at the level script files and the difference for me is that I need to be able to spawn objects mid game not just when I load a level. If you have any suggestions or thoughts on how best to go about this I'd love to hear it. If more information is needed let me know.

Thanks,
Shaun

About the author

Recent Threads


#1
06/13/2011 (12:52 am)
An easy solution would be to store them in a simset, which is Torque's linked list.

$enemieslist = new SimSet();
$activeEnemieslist = new SimSet();

// create 72 enemies at the start of the game

for (%i = 1; %i <= 72; %i++)
{
   %enemy = enemyTemplate.cloneWithBehaviors(true);
   $enemiesList.add(%enemy);
}

// to spawn an enemy

%newEnemy = $enemiesList.getObject(0);
$enemiesList.remove(%newEnemy);
$activeEnemiesList.add(%newEnemy);
%newEnemy.setPosition...etc

// to modify all the active enemies at once

for (%i = 0; %i < $activeEnemiesList.getCount(); %i++)
{
   %enemy = $activeEnemiesList.getObject(%i);
   %enemy.setPositionY(...etc...
}

#2
06/13/2011 (10:58 am)
Thank you this looks amazing! I haven't worked with SimSets yet and didn't understand how to use them but this is very easy to understand. I appreciate the help.

Thanks,
Shaun