0 votes

My code:

onready var planet = preload("res://Planet.tscn")
    var planet_sizes = [0.15, 0.25, 0.35]
    var created_planets = []
    var goodtoadd = true

func _ready():
    randomize()
    draw_planets()

func draw_planets():
    while created_planets.size() < 2:
        var planet_coords = Vector2(rand_range(50, 1870), rand_range(50, 1030))
        if not planet_coords in created_planets:   #if there is not already a planet at those coordinates
            if len(created_planets) == 0:           #if there are no planets
                goodtoadd = true
            else:
                for existing in created_planets:                   # if planet is further than 35 pixels from all other planets
                    if planet_coords.distance_to(existing) < 35:
                        goodtoadd = false
        if goodtoadd:
            var s = planet.instance()
            add_child(s)
            s.position = planet_coords
            var size = planet_sizes[int(rand_range(0, 3))]
            s.set_scale(Vector2(size, size))
            created_planets.append(planet_coords)

The while loop is completing, and when created_planets.size() equals 2, it deletes everything in there and runs again. And when the size equals 2 again, it stops. And for some reason, changing the 2 to anything higher than 3, causes the test window to crash. I need the number to be 40 or so.

in Engine by (39 points)

Maybe you should not add_child within a _ready function. Is Godot printing errors?

1 Answer

0 votes

From what I can see, you are setting goodtoadd = true only at start and if there are no planets so making it false will stop it. However this may not fix your problem/s I recommend you to print in the IFs to see where your problem starts.
Note that while 2 < 2: will stop

by (330 points)

So I fixed the issue with changing goodtoadd and added print statements. Here it is: https://pastebin.com/GiVT00dU but it still loops twice no matter what.

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.