while loop is resetting itself when condition is met and running a second time

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

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():

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
				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()
			s.position = planet_coords
			var size = planet_sizes[int(rand_range(0, 3))]
			s.set_scale(Vector2(size, size))

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.

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

Zylann | 2018-02-20 20:41

:bust_in_silhouette: Reply From: Sprowk

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

So I fixed the issue with changing goodtoadd and added print statements. Here it is: extends Nodeonready var planet = preload("res://Planet.tscn")var planet_si - Pastebin.com but it still loops twice no matter what.

LockManipulator | 2018-02-20 19:32