+1 vote

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 ! :)
Thanks !

in Engine by (245 points)
edited by

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

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] 

any error on debug output when run it?

no errors when I play the scene, but I get

" Invalid call. Nonexistent function 'set_disabled' in base 'Nil'."

when clicking the button

1 Answer

+3 votes
Best answer

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) 
by (9,688 points)
selected by

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"

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.
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 webmaster@godotengine.org with your username.