Fixing an enoying Bug


While testing the game myself, I figured out that there was a nasty bug hidden in my Code. You can reproduce it now by playing the old Version which is attached to this DevLog.

Reproducing the Bug

(install the BUGGY! version attached to this devLog)

Open the game, play a little bit (for example in the new Zenmode). After some time (doesn't really matter how long), you let your self die by getting killed by one of the enemies. 

Getting killed

checkout the red marked section under Game...

Then in the "End Game" Screen click on retry.

The game opens again:

2nd Time game gets opened this time with "retry"

Pay again attention to the red marked section... do you see a difference?

Understanding the Bug

As might have seen in the captions, the value labeld "sum" in the second one is higher than the first one, as we played 2x it got doubled. This continues on whith every time you reopen a game with "retry." So 1x = 4.25, 2x = 8,5, 3x = 12.75 and so on. Keep this in mind as I explain further.  

The way I decide which Powerup gets choosen if an enemy gets killed is as follows:

Code which defines if and which powerup gets spawned if an enemy gets killed

The first function "SpawnPowerUp" simply chooses if a Powerup should be spawend. Random.value delievers a float value between 0 and 1, if this value is smaller than my #spawnProbability (in this case 0.1 which results in a 10% chance) a random powerup gets spawned.

The second function "PickRandomPowerUpBasedOnPropability" is a little more complicated. It would be boring if each powerup would spawn with the same probability. I decided for balancing reasons, that the health powerup has to be spawned more often then the others and that the respawn has to be rare.  To solve this, I attached a propability to each powerup. So if Powerup A has a probability of 1 and B has a Probability of 2 than B is 2x more likly to get picked as A.

As I initialize the PowerupController, I calculate the - as I call it - "totalPorpabilitySum". The following picture shows the coresponding code.

Init function of PowerUpController
The code which calculates the propability sums

As you can figure out yourself, the function "PickRandomPowerUpBasedOnPropability" uses a random.Range(Min, Max) function which generates a value from 0 to  totalPorpabilitySum (4.5) , lets say  2.14325 . 

The Init Function calculated a Range for each Powerup in which this generated Value (i.e  2.14325) must be for it to be choosen (these are stored in the propabilitySums-Array. If you want to now if the 3rd Powerupwas chosen you just have to check if the generated value is bigger than propabilitySums[3-1 = 2] and at the same time smaller than the propabilitySums[3] value. If thats the case you got your Powerup. Compare the image bellow.

Propabilitysum explained

Example for Powerup picking

As with any "Retry" the  totalPorpabilitySum gets higher and higher, BUT the propabilities for each Powerup stay the same, wierd things start to happen. Each time the random value gets generated, it is now no longer between 0 and 4.5 but after 3 times already 12.25. to get in the range for the powerup 1 (with propability = 1) which had ad first a propabilty of 1/4.25 = 23.52% it is now: 1/12.75 = 7.84% which is a lot smaller. Due to my implementation of the "PickRandomPowerUpBasedOnPropability" this caused always the first powerup to get chosen.

Fixing the Bug

Once identified it was easy to solve, I just had to make sure that when calling the Init-Function I set the propability sum back to 0 before calculating it again.

Solution

Fixing the bug by setting totalPorpabilitySum to 0

Files

AI-Buster-windows-setup.exe 15 MB
Version 1 Dec 26, 2019

Get AI-Buster

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.