when breaking bricks how to make a flower or mushroom etc..

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rodell2020

I am trying to create a 2D Super Mario platform like game
I managed to create my own bricks and make them bust and do random coins
but Now what I like to do is

[A] at random if he breaks a brick it grows a flower on top so that the Player walks into the flower he grows…
[B] at random if he breaks a brick it sends a mushroom out (moving) the player has to chase it. if he catches it he gets (I do not know yet but I wanted to get ahead start here!

look
Random number generation — Godot Engine (stable) documentation in English

var random = RandomNumberGenerator.new()
var number : int
func _ready():
 random.randomize()
 number = random.randi()% 2
 print(number)
 pass

func example():
     if number == 0:
            print("flower")
     else:
            print("mushroom")

ramazan | 2022-07-12 07:45

:bust_in_silhouette: Reply From: USBashka

You already implement coins, so you probably understand how to make other collectables. If the question is about random, most common way to make it is randi() function. randi() generates random intager, so how you can use it?

Firstly, you should define probability of spawning object. Let’s say that bricks drops nothing in 80% of chances, flover in 15% and mushroom in 5%. Take the modulo 100 and you’ll get random int from 0 to 99. Then you can use if statement to spawn appropriate object. Like this:

var spawn_number = randi() % 100
if spawn_number < 15:
    spawn_flower() #Make this function
elif spawn_number < 20:
    spawn_mushroom() #And this too