0 votes

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!

Godot version 3.4.4
in Engine by (52 points)

look
https://docs.godotengine.org/en/stable/tutorials/math/random_number_generation.html

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")

1 Answer

0 votes

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