Hi all,

I want to randomly generate some powerups as my game objects are loaded (bricks in Breakout).

I was thinking something like:

``````var randomNumber = randi()%101+1
if randomNumber == 1:
#then we have a 1 in 100 chance of being picked
#do stuff here
``````

Can anyone give feedback on the code?

The idea is I want to have different chances of a specific powerup being chosen. So something like:

``````if powerup == "speedBall":
#make speedball a 1 in 100 chance of being drawn
var randomNumber = randi()%101+1
if randomNumber == 1:
#then we have a 1 in 100 chance of being picked
#do stuff here
elif powerup == "growBat":
#make growBat a 1 in 40 chance of being drawn
var randomNumber = randi()%41+1
if randomNumber == 1:
#then we have a 1 in 40 chance of being picked
#do stuff here
``````

I'm happy to progress with this idea but wanted to check in, see if anyone can see any issues etc or some better suggestion?

in Engine

``````var randomNumber = randi()%101+1
if randomNumber == 1:
#do stuff here
``````

randomNumber will have 1~101.
it's actually 1 of 101 chance.

``````var randomNumber = randi()%100 # will have 0~99
if randomNumber == 0:
#do stuff here
``````

There is another way.

``````var randomNumber = rand_range(0,1)
if randomNumber <= 0.01: # takes 1%
#do stuff here
``````

this can be more convenient cause you can adjust like `if randomNumber <= 0.015` which is 1.5%

by (9,509 points)
``````var randomNumber = rand_range(0,1)
if randomNumber <= 0.01: # takes 1%
#do stuff here
``````

I really like that. Makes a lot of logical sense. Combined with the idea from eons it will make for a really nice random powerup manager.

+1 vote

Give each powerup a chance, the level or difficulty manager may need to modify it too according some rules.

The powerup picker gets one up from the sum of chances (and possible modifiers), the manager decides which powerup fall in that number.

Expanding a bit the idea:

Chances.
PowerupA: 40
PowerupB: 60
PowerupC: 20

``````|.......... ................. .......
|          |                 |      |
|   A      |         B       |   C  |
|          |                 |      |
+-------------------------------------->
``````

The picker gets a number from 120, the manager decides which one falls there, this could be dynamic so if you keep adding powerups, the system won't need to be touched.

This could allow to set fuzzy logic rules, near of where the vertical lines are, you could add overlapped chances for one or another, is something common for AI but useful for subtle difficulty settings and scaling too (is just a bit harder to design).

by (7,796 points)

Yes, that's a good solution.

I implemented something similar a couple years ago, by assign frequencies of drop to each type of powerup: https://github.com/akien-mga/dynadungeons/blob/master/scripts/global.gd#L64-L67

Then storing the sum of frequencies: https://github.com/akien-mga/dynadungeons/blob/master/scripts/global.gd#L85-L89

And then pick a random number in that sum of frequencies and check which chunk/powerup it corresponds to: https://github.com/akien-mga/dynadungeons/blob/master/scripts/bomb.gd#L280-L295