A very confusing, yet simple weighted spawn system.
--
Today we will be creating a weighted powerup spawn system. This article won’t have much to it but could be very confusing. As I’m writing this one, I’m still confused about how it works. I looked up several other articles on this type of event and found one that makes the most sense, but has some things I think were missing an explanation. This is the article I read and used to get my system working as well. It’s almost identical, but I feel there is room for explanation.
To start, we will open up our ‘SpawnManager’ script. We’re going to add some variables here. We will add two private integer variables called ‘_randomNumber’ and ‘_totalWeight’. We will also create a public integer array named ‘table’. We will add the following numbers into the table: 40,20,10,10,10,5,5. We have to set the values from biggest to smallest in the table or the spawning could give us the wrong number.
In ‘Start’ we need to calculate the value of the powerup table we just created. We will do this using a foreach loop that will add all the values of the table.
We will now create a new private function called ‘ChoosePowerUp’. We’re going to optimize our position to spawn location just a bit and just create a new float called ‘randomX’ and place our -8f and 8f inside a Random.Range. The reason we’re doing this is that we will be replacing some code in the powerup coroutine with this function.
We will also now set our ‘_randomNumber’ variable equal to a Random.Range between 0, and our ‘_totalWeight’. Let’s write a debug log to tell us what the random number generated would be.
This is where we are so far. We still have some more to write in this function. We need to create a for loop with the table.Length. Inside that, we will check if ‘_randomNumber’ is less than or equal to table. If so, then we will Instantiate our ‘_powerups’ array, at a new location of ‘randomX’, then 7 on the y axis, and 0 on the z, with no rotation. Else, ‘_randomNumber’ is subtracted from the table array.
This is what was really confusing to me and I will explain more after we finish the script.
We’re going to move to the ‘SpawnPowerupRoutine’ and remove all the contents of the while loop except the wait for seconds. Before that, we will just call our ‘ChoosePowerUp’ function we just wrote.
This was super confusing at first, but then located a YouTube video that explains the system very well. Pretty much a random number is being generated, let's say it’s 39. Then we’re asking if that number is less than the first element, in our case is 40. 39 is less than 40 so we will spawn the item associated with the 40 on the table. Let's say our random number is 45. We do the same thing again. We ask the system if 45 is less than 40, in this case, it’s not. So we subtract 40 from 45 to get 5. We then ask the system if 5 is less than the next element, which is 20. It is, so we will spawn that item. Here’s the video to watch over and explain the system better than I can.