+1 vote

Hey, i've readed a lot of topics but none came to an solution. I am making a menu for the game and i need to have access to other nodes that are in other scenes.

The code i am making:

Player.gd

`
onready var globals := GlobalWorld.title_label

...

func _on_StartButton_pressed() -> void:
on_menu = false
$Head/Camera.current = true
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
globals.text = str("Hello")


pass`

this is the script i want to pick the nodes from the main scene, that have the following code:

World.gd

extends Spatial

onready var start_button: Button = $Control/Buttons/StartButton
onready var options_button: Button = $Control/Buttons/OptionsButton
onready var controls_button: Button = $Control/Buttons/ControlsButton
onready var quit_game_button: Button = $Control/Buttons/QuitGameButton
onready var title_label: RichTextLabel = $Control/Title/RichTextLabel

I'm using the Autoload if you may wonder, and i tried using the "getnode", simply what happens here is that globals.text simply returns a nullinstance (apparently).

details of the error: E 0:00:04.332 getnode: (Node not found: "Control/Buttons/StartButton" (relative to "/root/GlobalWorld").)
Method failed. Returning: nullptr
scene/main/node.cpp:1465 @ get
node()
World.gd:3 @ _ready()

im not really sure of whats happening, but keep in mind i want a node that is in another scene

Godot version 3.5.1
in Engine by (13 points)

1 Answer

0 votes

None of these lines will get nodes

onready var start_button: Button = $Control/Buttons/StartButton
onready var options_button: Button = $Control/Buttons/OptionsButton
onready var controls_button: Button = $Control/Buttons/ControlsButton
onready var quit_game_button: Button = $Control/Buttons/QuitGameButton
onready var title_label: RichTextLabel = $Control/Title/RichTextLabel

This is because "Control" in not a child of "GlobalWorld." Instead, delete this lines and write this:

var control: Control

Notice that we didn't set the variable yet. Next, add a script to the "Control" node (if you haven't already) and add this to the _ready function:

func _ready():
    GlobalWorld.control = self

Now you can reference the children of "Control" from wherever you'd like. So this line:

onready var globals := GlobalWorld.title_label

Would become:

onready var globals := GlobalWorld.control.get_node("Buttons/TitleLable")

And then everything should work as expected.

by (731 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 Frequently asked questions and 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 [email protected] with your username.