- Home /
Spawn "n" Enemies Every "nth" Wave?
So I have my game spawning one kind of enemy every wave, and each wave, there are 5 more then last time, so Wave 1 has 5 enemies, Wave 2 has 10 enemies, etc. I'm not too familiar with arrays and I think that's how I would have to go about solving this, but have no idea where to begin...
I want to be able to make 5 of the simple enemies spawn every wave until the 5th wave, then on the 6th wave, it would reset so that only 5 of the simple enemies appeared, but then another kind of enemy spawned as well. It would look kind of like this:
Wave: Simple Enemies: Dangerous Enemy:
1 5 0
. 10 0
. 15 0
. 20 0
5 25 0
6 5 1
7 10 2
Thanks so much!
If you want to see the script I have so far, it's here: http://dl.dropbox.com/u/55162513/SpawnEnemiesEndless_Script.js
If you don't want to use an array and the logic stays the exact same, each wave could just spawn following the simple formulas: simple = (wave%5)*5; dangerous = (wave-5);
Sorry if I sound extremely, dumb, but what exactly does the wave%5 do? I haven't seen that before...
% is the modulus operator. It returns the remaining of a division. IE: 5%1=0, 5%2=1, 5%3=2, 5%4=1, 5%5=0.
So what would happen if the number got higher, say, 15? Wouldn't it spawn 50?
Unless a for loop is nested within another, having the same variable declared shouldn't affect anything. They should act independently from each other.
Answer by syclamoth · Jan 05, 2012 at 09:48 AM
Try having an array of symbolic 'wave' objects, each of which has information about what should be spawned that wave. Then, you can set them up in the inspector and have your waves spawn correctly!
class SpawnWave
{
var weakEnemies : int;
var strongEnemies : int;
}
var waves : SpawnWave[];
Then, when the time comes to spawn enemies for a given wave number, do this-
var currentWave : SpawnWave = waves[currentWaveNumber];
for (var i = 0; i < currentWave.weakEnemies; i++)
{
// Spawn a weak enemy!
}
for (var i = 0; i < currentWave.strongEnemies; i++)
{
// Spawn a strong enemy!
}
So to make the "wave" objects, would I create a bunch of empties all with the same script, then in the inspector change the first one to say weakEnemies = 5 and strongEnemies = 0, and on another one it might say weakEnemies = 10 and strongEnemies = 5? If so, how could I get this code here to know which empty to call the information from?
Thanks for such a quick reply btw!
Who said anything about empties? You don't need more than one gameobject here. The array of waves will appear in the inspector! Try it, you'll see.
Okay, I took the "wave" objects literally as game objects... I had to add the currentWaveNumber as a variable that equaled zero, but when I go to change the inspector, the currentWave and the Wave arrays both ask for the number of weakEnemies and strongEnemies?
Sorry, currentWave shouldn't be a class variable. It should only exist within the scope of the function that spawns the wave. It gets declared immediately before the two for-loops.
Your answer
Follow this Question
Related Questions
Way Point Assignment 4 Answers
Enemy Wave 1 Answer
Can anybody help me fix this wave spawning script please? 1 Answer
Can you name something that you spawned with a spawner script? 1 Answer
c# enemy's not spawning 1 Answer