0 votes

Both scene1 and scene2 are inside a main scene3.
Why won't the "Button" node from scene1 script hide (interact with) scene2?

-scene3 node
--scene1 node(contains "Button")
--scene2 node(contains "Node2d/Sprite")

extends Panel

func _on_button_pressed():
    get_node("Sprite").set_hidden(!get_node("Sprite").is_hidden())
    get_tree().get_root().get_node("scene2").set_hidden(!get_node("scene2").is_hidden())

func _ready():
    get_node("Button").connect("pressed",self,"_on_button_pressed")

Edit: Okay I've since realized that the scenes weren't instanced properly. The following worked:

-scene3 main node
--scene1 node
---scene2 node

extends Panel

var block

func _on_button_pressed():
    block = load("res://scene2node.tscn").instance()
    get_node("Sprite").set_hidden(!get_node("Sprite").is_hidden())
    get_node("scene2").set_hidden(!get_node("scene2").is_hidden())

func _ready():
    get_node("Button").connect("pressed",self,"_on_button_pressed")
asked Sep 13 in Engine by friendlyapprentice (22 points)
edited Sep 15 by friendlyapprentice

2 Answers

+1 vote
Best answer

where is the "block" node?
you don't have to start with get_tree()

you can get a reference in various ways.
one of way is using get_parent()
if "block" node is located at parent of your current Panel,
you can get it with get_parent().get_node("block")

or just simply do get_node("../block") with relative path.

answered Sep 13 by volzhs (5,370 points)
selected Sep 15 by friendlyapprentice
+1 vote

At a glance, it could be because you're using get_tree().get_root().get_node("block") in one case and get_node("block") in another. Those aren't the same path.

Other than that, you should probably provide more information. What does your node tree look like?

answered Sep 13 by kidscancode (1,208 points)
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.