How to access a node within another scene

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

Hello community,

im pretty new to Godot and playing around with Godot to get used to it. I’m a programmer for about 15 years but i’ve never used an editor like Godot before. I guess i’m thinking to complicated and have to get used to Godot and the way things are made with Godot.

My question is: how can i access a node and it’s methods inside another scene?

I’m playing around with plugins and wanted to add a simple button to the 2D canvas toolbar. That’s working fine. But i wanted to show a window dialog when i click the button. So i created another scene with the window dialog in it. Now i need to call the “popup” method of the window dialog when i click the button.
But somehow it can’t find the method popup().

Here is the code i’ve written so far:
General editor script:

extends EditorPlugin

var button # A class member to hold the button during the plugin lifecycle

func _enter_tree():
	button = preload("res://addons/my_custom_button/my_button.tscn").instance()
	add_control_to_container( CONTAINER_CANVAS_EDITOR_MENU   , button )

func _exit_tree():
    remove_control_from_container( CONTAINER_CANVAS_EDITOR_MENU, button ) # Remove the button # Erase the control from the memory
    enter code here

Code of the button itself:

extends Button

var window

func _enter_tree():
	connect("pressed", self, "clicked")
	window = preload("res://addons/my_custom_button/WindowDialog.tscn").instance()

func clicked():
	print("You clicked me!")

And here the error message:

res:// - Invalid call. Nonexistent function 'popup' in base 'Nil'

It basically says that i don’t have a reference to the window dialog and thats why it can’t find the popup() method.

:bust_in_silhouette: Reply From: Rednib

Ok, i misunderstood a few things.

I,ve changed a few things so now it works fine.

Editor script:

extends EditorPlugin

var editor_button
var popup_window_tscn = preload("res://addons/WindowDialog.tscn")
var popup_window

func _init():
	editor_button =
func _ready():
	editor_button.text = "Popup"
	editor_button.connect("pressed", self, "showDialog")
func _enter_tree():
	add_control_to_container( CONTAINER_CANVAS_EDITOR_MENU   , editor_button )
func _exit_tree():
    remove_control_from_container( CONTAINER_CANVAS_EDITOR_MENU, editor_button ) # Remove the button # Erase the control from the memory
func showDialog():
	if (popup_window == null):
		popup_window = popup_window_tscn.instance()

And within the popup window you can create and script the ui elements as usual.
Easier as i thought.
However, i still would like to know how i can access a node of another scene. That might be usefull to know.

Thanks :slight_smile:

For all who want to know how to access a node of another scene: var other_scene = get_node("other_scene") and then you can access all the functions of the scene’s attached script. other_scene.function_of_other_scene().

Rednib | 2018-06-21 17:57

Can I access the variables?

R9_dev | 2023-03-08 19:06