checking if a node is visible returns erratic results in for loop

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

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.

:bust_in_silhouette: Reply From: alwaysusa

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.