Group Group Group Group Group Group Group Group Group

Chapter 5: 3 Logic Errors in alien spawn function

#1

Hi, I found that the alien spawn function has 3 logic errors. (maybe I wrong)

  1.  if (aliensPerSpawn > spawnPoints.Length)
                 {
                     aliensPerSpawn = spawnPoints.Length - 1;
                 }
    

「This limits the number of aliens you can spawn by the number of spawn points.」

My Opinion: This limits the number of aliens less 1 than number of spawn points, I think it should be

aliensPerSpawn = spawnPoints.Length;

  1. aliensPerSpawn = (aliensPerSpawn > totalAliens) ? aliensPerSpawn - totalAliens : aliensPerSpawn;

「This means a spawning event will never create more aliens than the maximum amount that you’ve configured.」

My Opinion: If totalAliens is 1, aliensPerSpawn set 11, it will end be 10, still more than totalAliens. Should we first check the smaller number between totalAliens and spawnPoints, then limit aliensPerSpawn less than the smaller?

int smallerNumber = (spawnPoints.Length > totalAliens) ? totalAliens : spawnPoints.Length;
aliensPerSpawn = (smallerNumber > aliensPerSpawn) ? aliensPerSpawn : smallerNumber;

3.int randomNumber = Random.Range(0, spawnPoints.Length - 1);

「This line produces a random number as a possible spawn point.」

My Opinion: The Range doesn’t include spawnPoints.Length - 1, so the range will be 0-9, actually we have 0-10. So if we set aliensPerSpawn 11, the program will probably enter an infinite loop.We should modify as follow:

int randomNumber = Random.Range(0, spawnPoints.Length);

  1. I don’t understand the usefulness of maxAliensOnScreen, what the difference with totalAliens? Hope you could explain, really appreciate.

@crispy8888 @vegetarianzombie

#2

About point 4, I just search the aliensOnScreen in the book, and find in Chapter 8,

public void AlienDestroyed() 
{
  aliensOnScreen -= 1;
  totalAliens -= 1;
}

then the setting of maxAliensOnScreen does make sense.

#3

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]