The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I want a clean way of getting specific index members in an array during a loop. I want to provide an array of values and have the for loop retrieve the indexes specified by the array. I don't want to write a massive if statement with a bunch of ORs. Basically like this:

for i in (1,2,3,5,7,9, 18, 19) in get_children():

or

for i in get_children():
    if i == (1,2,3,5,7,9, 18, 19):

How can I do this?

Godot version 3.2.3
in Engine by (44 points)

1 Answer

+1 vote
Best answer
if i in [1,2,3,5,7,9, 18, 19]:
by (8,550 points)
selected by

Thanks. For my use case it doesn't seem to be working.

for i in get_children():
    if i in [0,1,3,5,7,9, 17, 18]: 
        i.visible = true
        i.mouse_filter = 1 

For some reason, nothing becomes visible. Without the if statement, the whole VBoxcontainer becomes visible.

get_index() fixes this.

Oh I see, sorry

i is not an index but rather the child node. You should do this or this

for i in get_child_count():
    if i in array:
        var child = get_child(i)

for child in get_children():
    if child.get_index() in array:
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 Frequently asked questions and 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 [email protected] with your username.