Respawning?

How to script a monster to respawn?

This one. Provided by someone who was nice enough to enlighten me.

var newObject: GameObject; 

function Start() { 
   while (true) { 
      yield WaitForSeconds(45); 
      Instantiate(newObject, transform.position, transform.rotation); 
    
   } 
}

enjoy
AC

thanks ~! :stuck_out_tongue:

Hi Targos,

I see the commands “while (true)” often in scripts. I know that “while” is a loop, but I’m a little unclear of what “while (true)” does?

thanks

I dunno man. If I translate it into human, it kind of says “if its happenning”. As far as making it happen, I think thats just the nature of it-if its written down it executes…therefore it starts and once its run it keeps running.

This one of the slightly more organic things I see in scripting. Its cool huh? It was on the irc I discovered this. Thanks guys.
AC

A while loop executes while the condition is true. In this case it’s always true, so it’s the same as saying “do this forever”. There’s no “forever” keyword in JavaScript or C# though, so you use “while (true)” instead.

Of course, such kind of eternal loop usually has some sort of “breaking” out of it. Or it runs as a coroutine, repeatedly performing some tasks.

In this script, what condition is the “while (true)” referring to? It seems like “while (true)” is saying that while some conditition is true, execute the following commands. What condition is true?

var newObject: GameObject; 

function Start() { 
   while (true) { 
      yield WaitForSeconds(45); 
      Instantiate(newObject, transform.position, transform.rotation); 
    
   } 
}

thanks

while (true)

It just means that it is always true, thus will loop forever.

In this case however we are also using yield, so execution will be paused for the given amount of time, 45 seconds in this case.

      yield WaitForSeconds(45);

after 45 seconds the script will be resumed. Everthing after the yield will be executed, so we hit the instantiate and after that the while true.

So this function will just continue instantiating objects at 45 second intervals forever.
Since it is a coroutine it doesnt stall the machine while looping forever, and when you stop playing execution of the function will also end automatically, same happens if you eg. delete the game object the script is attached to.

Thanks for the clarification.

var wave1: GameObject;

function Start() {
   while (true) {
      yield WaitForSeconds(5);
      Instantiate(wave1, transform.position, transform.rotation);
   }
}

How to set a period of time for the first wave of bots?
means when it reach a period of time, it stop spawning the wave1 bots, but going to a wave2 bots(a diff model of bots)?

you could do it a couple ways. Based on time or waves.

Since you said waves–I’d do it like this–I didnt test this code, you may need to fiddle with it–im sure there is a clever way to use a counter as a string variable too…

Replace (5) or (45) with a variable

add a counter

var wave1: GameObject;
var wave2: GameObject;
//etc
var counter = 1;
var timeToRelease = 5.0;

function Start() {
while (true) {
timeToRelease = counter * 5
yield WaitForSeconds(timeToRelease);
if (counter == 1){
Instantiate(wave1, transform.position, transform.rotation);
}
if (counter == 2){
Instantiate(wave2, transform.position, transform.rotation);
}
counter = counter + 1
}
}

Thanks heaps:)

i was searching how to make bots respawn and found this. whenever i put one of my bots into my game and they die, that gameobject gets removed from a variable in the other teams ai, making that ai shut down. is there a solution?

Nice lil script.
I had it all (the FPS tutorial shows how to create rockets and I use that to spawn enemies), but the while helps a lot in stead of a funky timer running and resetting all the time.

Is there a way to make the script spawn a total of 5 creatures and have it check if there are less then 5 so it respawns more? Say a maximum of 5 enemies it can support. This way I won’t have a huge memory leak.

I don’t think the script keeps tabs on who it spawned.
You can easily make the spawner (barracks or whatever) die.

I do think I can do without keeping tabs, Just set a distance to trigger the enemy. And have the enemy chase you, so there would be a lot less flooding.

I’m currently collecting little scripts like the cool lego parts when I was young, before starting to build a game.

create a var, and name it spawnMax, set it to 5; created another var called currentlySpawned. when you spawn a creature create an if statement with the parameter(currentlySpawned < spawnMax). Then when you spawn the creature, increase currentlySpawned. When you destroy the creature, decrease currentlySpawned.

Example:

var spawnMax : int = 5;
var currentlySpawned : int;

function Update()
{
    SpawnCreature();
}

function SpawnCreature()
{
    if(currentlySpawned < spawnMax)
    {
       [spawncreature code];
       currentlySpawned++;
    }
}

function Destroy()
{
    if([creature is dead code])
    {
        currentlySpawned--;
    }
}

That should give the desired effect.

Thank you Sil3nt Pr0digy,

That sounds like the correct solution. I’ll use this for my “made for” iPhone monster game.

Regards,

Heliora

You are very welcome Heliora. I am glad to hear that I could be of service.

  • Sil3nt Pr0digy