Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

While loops are resetting themselves after completing condition #16868

Closed
LockManipulator opened this issue Feb 20, 2018 · 7 comments
Closed
Labels

Comments

@LockManipulator
Copy link

LockManipulator commented Feb 20, 2018

Godot 3.0 using GDscript.

Windows 10.

After a while loops completes, it resets and loops a second time.

To reproduce:

extends Node
 
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() < 3:
        print("looping")
        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
            print("not in")
            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
                    else:
                        goodtoadd = true
        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)
            print(created_planets.size())

From the print statements you can see that the size of created_planets reaches 3, then somehow everything in it gets deleted and it loops through again.

@Zylann
Copy link
Contributor

Zylann commented Feb 20, 2018

Isn't it deprecated to do add_child whithin a _ready() function? Do you get an error about this?

@LockManipulator
Copy link
Author

No error and I have no idea if it's deprecated or not. I didn't know how to do it so that's what I found online. Is there another way to spawn an instance of that sprite?

@vnen
Copy link
Member

vnen commented Feb 20, 2018

Do you have a small project that reproduce this issue? I can't reproduce with the code attached.

@LockManipulator
Copy link
Author

https://www.dropbox.com/s/gzg966nv543beiy/Civ.zip?dl=0 Here is the project. It seems like the problem is actually being caused by func _ready(): being called twice.

@Zylann
Copy link
Contributor

Zylann commented Feb 20, 2018

I created a new project in which I made a dummy Planet.tscn scene, so I could copy paste your code with no modifications. Here is the output:

looping
not in
1
looping
not in
2
looping
not in
3

I would say there is a problem in your project.
Are you sure your Node isn't present twice? (which would explain why _ready is called twice).

Also, another thing that miiiight cause this is adding a child node from within a _ready() function, which means the new child's _ready will be called once when you add it, and a second time because Godot was already ready-ing the parent (so its children are following, including the one you just added!).

Edit:
I tried your game, and I get the same output by launching the MainMenu:

looping
not in
1
looping
not in
2
looping
not in
3

@LockManipulator
Copy link
Author

Oh. Seems like my node is present twice. It's a child of MainMenu and then it gets called later. I just removed it as a child of MainMenu and calling it as before worked fine.

@reduz
Copy link
Member

reduz commented Feb 20, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants