x


How to prevent infinite loop when Array Index Is Out Of Range

Basically, my problem is that I'm using variable "i" to flip through an array of spawn points. When i becomes greater than the amount of spawn points available, "i" obviously leaves the range of the spawn point array. My solution is to simply reset "i" inside the scope of the for loop, but this doesn't seem to solve my problem. What's going on?

 function spawnWave()
 {
     if(waveActive)
     {
         for (var i=0; i<=waveNumber; i++)
         {
             if (i>8)
             {
                 i = 0;
                 print(i);
             }
             Instantiate(enemy1, Vector3(spawnArray[i].transform.position.x, spawnArray[i].transform.position.y, spawnArray[i].transform.position.z), Quaternion.identity);
             //something to delay spawning for more than six
         }
         waveActive = false;
         needSpawn = false;
     }
     else
     {
         yield WaitForSeconds(1);
         waveActive = true;
     }
 }
more ▼

asked May 05, 2012 at 12:34 AM

dvineinfekt gravatar image

dvineinfekt
3 6 4 5

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

you can use Invoke("callYourFunction", 3) for example, it will call the function in 2 seconds. In your case you can change the value of waveActive = true; inside a function and call the function from Invoke, hope it helps!

more ▼

answered May 05, 2012 at 08:04 AM

moghes gravatar image

moghes
2.3k 180 454 216

(comments are locked)
10|3000 characters needed characters left

If you want to iterate through all elements of spawnArray, it's easier to do the following:

    for (var spawnPt in spawnArray){
      Instantiate(enemy1, spawnPt.transform.position, Quaternion.identity);
    }

But if you need the index, use the array length in a regular for loop:

    for (var i=0; i < spawnArray.length; i++){
      Instantiate(enemy1, spawnArray[i].transform.position, Quaternion.identity);
    }

Anyway, you don't need to create a new Vector3 with the position values - position itself is a Vector3, thus you can use it directly.

more ▼

answered May 05, 2012 at 12:46 AM

aldonaletto gravatar image

aldonaletto
57.9k 34 97 311

hmmm....out of curiousity, is there a way to have it step through the array slower? I've tried "WaitForSeconds(3)" for instance, but it doesn't seem to do anything. Adding "yield" seems to create another infinite loop. Think you can help me out again? And thanks for answering my previous question!

May 05, 2012 at 03:34 AM dvineinfekt

It's easy in JS:

    for (var i=0; i < spawnArray.length; i++){
      Instantiate(enemy1, spawnArray[i].transform.position, Quaternion.identity);
      yield WaitForSeconds(3.0); 
    }
But you must understand how coroutines work: when you call a coroutine, it starts running and returns to the caller when the first yield is encountered - but continues running in the background, being resumed automatically each frame until its completion. You should thus make sure to not call the coroutine again until it ends, or multiple coroutine instances will run at the same time, crowding your level with tons of enemies. A common approach is to use a boolean flag set at the coroutine start and cleared upon return:

private var spawning = false;

function spawnWave(){ if (spawning) return; // abort calls while spawnWave is running spawning = true; // set flag "spawnWave is running" if (waveActive){ ... } else { ... } spawning = false; // clear flag right before returning }

May 05, 2012 at 10:29 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x798
x708
x74
x36
x18

asked: May 05, 2012 at 12:34 AM

Seen: 1118 times

Last Updated: May 05, 2012 at 01:34 PM