How to target an array's nodes with a 'for' loop ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nonomiyo
:warning: Old Version Published before Godot 3 was released.

Hello, I’m new in Godot and in programming so still trying to figure out how everything works. I’m trying to set all my buttons to disabled. (I simplified my issue here, by using only 2 buttons) :

#getting the nodes' paths for all the buttons :

export (NodePath) var button1_path
onready var button1 = get_node(button1_path)

export (NodePath) var button2_path
onready var button2 = get_node(button2_path)
    
#setting the array

var buttonsarray = [button1, button2]
            
#and that's where I have no idea what to do.. I don't get how 'for loops' work  :/
     		for entry in buttonsarray:
                		entry.set_disabled(true) 

Any help would be appreciated ! :slight_smile:
Thanks !

where the var buttonslist is declared?
out side of functions?

volzhs | 2017-09-17 17:04

no, the only things outside this script are the ‘button1 path’ and ‘button2 path’ which are in the inspector.
Maybe I should have called it Array instead of List… I just edited the original post changing “list” with “array”. Sorry for the confusion

Isn’t this how you declare an array ? :

 var buttonsarray = [button1, button2] 

nonomiyo | 2017-09-17 18:07

any error on debug output when run it?

volzhs | 2017-09-17 18:54

no errors when I play the scene, but I get

" Invalid call. Nonexistent function ‘set_disabled’ in base ‘Nil’."

when clicking the button

nonomiyo | 2017-09-17 19:44

:bust_in_silhouette: Reply From: volzhs

so you got null error.
it’s not about for loop, it’s about null reference.

(i’m not native english speaker so it’s a little hard to explain but… anyway)
I asked on my first comment is that the var buttonsarray is inside of func or not.
Now I guess it’s outside of function.

let me explain when the code is executed actually.

export (NodePath) var button1_path # value is set at loading scene
onready var button1 = get_node(button1_path) # it's null at loading but set just right before _ready called

export (NodePath) var button2_path # value is set at loading scene
onready var button2 = get_node(button2_path) # it's null at loading but set just right before _ready called

var buttonsarray = [button1, button2] # this executed at loading. so it's actually [null, null]

I think there might be several options to solve it.

onready var buttonsarray = [button1, button2] # instead of just "var"

or

var buttonsarray
func _ready():
    buttonsarray = [button1, button2]

or

func disable_it():
    var buttonsarray = [button1, button2]
    for entry in buttonsarray:
        entry.set_disabled(true) 

wow, thanks very much volzhs for your crazy fast answers and helping me solve my problem even with my not-so-good indications !

I’ll meditate on your notes for a while to get the logic.

PS: and you were right, my “var buttonsarray” was outside any “func”

nonomiyo | 2017-09-17 20:54