The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Right now I'm doing this:

onready var planet = preload("res://Planet.tscn")

func _ready():
    randomize()
    var s = planet.instance()
    for i in range(5):
        add_child(s)
        s.position.x = rand_range(0, 1920)
        s.position.y = rand_range(0, 1080)

But every sprite is in the same position. Or it's just loading in once because I only see one.

in Engine by (39 points)

1 Answer

+2 votes

Look at your function, you are only instancing one planet, the for is working over the same instance all the time (and there may be an error saying that the node is already in the tree after the first add_child).
Create, set position and add a new instance on every loop.

by (7,890 points)

I'm not sure I follow along because it seems like I am doing what you suggested. Am I not creating a new instance on each loop?

var s = planet.instance() is outside of the loop, so your code attempts to add one instance to the tree multiple times.

Like this

for i in range(5):
    var s = planet.instance()
    add_child(s)
    s.position.x = rand_range(0, 1920)
    s.position.y = rand_range(0, 1080)
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.