0 votes

I have a script that takes all children of a container node and arranges them in a circle at a given radius from center. This functions correctly.

The problem I have is that the parent scene has multiple children that can be used, and in the inherited scenes I want to choose which of these children nodes to show. To that end, I have tried setting up a for loop which checks if the child has been set to visible in the inspector, and if NOT, it is removed from the array of children generated at the beginning of the method.

Here's the code:

func set_radial_position():
var buttons = get_children()
print(buttons.size())
for button in buttons:
    if !button.is_visible():
        print(button.name)
        buttons.erase(button)
if buttons.size() == 0:
    return
print(buttons.size())

The odd thing is that it doesn't get the correct visible value for every child. Some return as visible when they are not visible, and vice versa. In game, the correct children show, but the calculation that follows the above code (and arranges the children in a circle) is based on the array size, which is incorrect after running through the for loop.

Here's a cap from the editor:

enter image description here

From that image, you can see that Button 6, 8 and 10 are set to visible:false in the inspector (and they are not visible in game, as expected), but they pass the visible check in the for loop. All the buttons are the same as, they are all duplicated from Button 1.

Any ideas what might be causing this issue? I know there are other ways I can do this, but it's bugging me. The radial arrangement works, but the for loop registers 7 visible children and spaces them out based on that number.

asked Jul 2 in Engine by alwaysusa (69 points)
edited Jul 3 by alwaysusa

1 Answer

+1 vote
Best answer

Figured it out.

I was iterating through the initial array size, and during that iteration removing items - which then throws off the iteration count. I solved it by moving visible children to a new array, then using that new array for further calculations.

answered Jul 3 by alwaysusa (69 points)
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.