Code doesn't work to hide the specified nodes in the array?

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

func _ready():
    pass

export(bool) var ShowFutureDesignObjects = false #change to true to show future design objects

var FutureObjects = ["Node1", "Node2", "Node3", "Node4"]


func ShowFutureDesign():
        for Obj in FutureObjects:
            if ShowFutureDesignObjects == false:
                get_node(Obj).visible.hide
            else:
                get_node(Obj).show
:bust_in_silhouette: Reply From: johnygames

OK, I found what the problem is.

Basically, three things:

  1. You never call the function ShowFutureDesign(). Try calling it from the func _ready() function

  2. It’s .hide() and .show(), actually, not .hide and .show

  3. Make sure the get_node() method gets called on the right node. If you try to access the nodes from a subnode, you might have to use the get_parent().get_node(Obj) method or something equivalent.

Hello, thanks for the comment.

You write :

get_node(Obj).visible.hide

but shouldn’t it be:

get_node(Obj).hide

Yes, I actually had them both get_node(Obj).hide, but I was trying to illustrate it’s a visible property.

Also, I don’t know why this array of yours has strings as members.
That’s a good point, get_node looks for the name of the node by string.
ie. get_node("LabelName")

GodotUser | 2019-08-11 21:02

OK, I found what the problem is.

Basically, three things:

  1. You never call the function ShowFutureDesign(). Try calling it from the func _ready() function

  2. It’s .hide() and .show(), actually, not .hide and .show

  3. Make sure the get_node() method gats called on the right node. If you try to access the nodes from a subnode, you might have to use the get_parent().get_node(Obj) method. or something equivalent.

I have updated the answer to this. If this is the right answer, please mark it as best so that others can find it too.

johnygames | 2019-08-11 22:12

JohnnyGames Wow, It works!

Thank you so much I had been struggling with this.

I didn’t need the get_parent; however I am glad you said that because I had been trying get_root().get_node()
get_children
etc. etc. etc.

Final working code is:

func _ready():
	ShowFutureDesign()

export(bool) var ShowFutureDesignObjects = false #change to true to show future design objects

var FutureObjects = ["StockName", "StockSymbol", "Search", "ExtCostBasisLabel"]


func ShowFutureDesign():
		for Obj in FutureObjects:
			if ShowFutureDesignObjects == false:
				get_node(Obj).hide()
			else:
				get_node(Obj).show()

GodotUser | 2019-08-12 01:42

Can you up vote my question also so people find it?

thanks

GodotUser | 2019-08-13 04:56

Done! Glad it solved your issue.

johnygames | 2019-08-13 12:53