+1 vote

Hi

Can any of you give me a link to something that can help me generate planets (circles) at random positions? It's in 2D.
I have tried googling but for some reason i'm not finding anything.
I want to create like 20 circles in an area, then some distance from that area i want to do the same again.
I'm not sure how to create the nodes dynamically or if the circles at all should have some special node-type attached.

Any one who can give me a link for a video or written documentation guidance.

Thanks.

in Engine by (16 points)

1 Answer

+3 votes
Best answer

You would first create a scene representing one planet. For simplicity, let's assume it consists of only one node: a Sprite with the texture of a circle (like this).

Then you would create another scene. I'll assume a Node2D here. It will represent your "universe". Now attach the following script to it:

# load your planet scene
var planet = preload("res://Planet.tscn")

func _ready():
    # do this 20 times
    for i in range(20):
        # create a new instance of the planet scene
        var new_planet = planet.instance()

        # set its global_position to two random (float)
        # values lying somewhere between 0 and 400
        new_planet.global_position.x = rand_range(0, 400)
        new_planet.global_position.y = rand_range(0, 400)

        # add it as a child of this "universe"-node
        add_child(new_planet)

Depending on where your planet-scene is located, how many planets you want to create and how big the considered area should be, you will have to adapt the code. In this form it's probably still not working precisely as you imagined it, but it should give you a rough idea on how to tackle a problem like this.

by (10,153 points)
selected by

Awesome!
Thanks.

That is just what i need to get an idea about where to start.

Thanks, I was making a similar game as well.

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 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 webmaster@godotengine.org with your username.

Categories