This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 by (838 points)

2 Answers

0 votes
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,800 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.

Thank you for your help.

+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,954 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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.