0 votes

I have so far scoured the internet and have failed to find a solution to this problem, so I was hoping that someone might know.
I think it's a problem with the path. It keeps returning null.
Trees:
In World1:
-World1
--Player
---player <-- Where the script is

In Mainmenu:
-Mainmenu
--Camera2D
---play <-- What I'm trying to reference

Script:

onready var m = get_node("/Mainmenu/Mainmenu/Camera2D/play")

func _ready():
    print(m)
    pass

If anyone knows, please say.

Godot version 3.2.3
in Engine by (15 points)

2 Answers

+1 vote
Best answer

It looks like you have two scenes: World1 and Mainmenu. If only one of those is active in the Scene Tree, the other won't be accessible through the method you are using. I'm unsure how you go about the scene transitions (if you are transitioning between them), but if you call get_tree().change_scene() and pass it the World1 Packed Scene (*.tscn, *.scn files), then the current scene, Mainmenu will be unloaded and removed from the Scene Tree, being replaced with World1. This would be why get_node("/Mainmenu/Mainmenu/Camera2D/play") returns null -- the node named Mainmenu doesn't exist.


One way to pass the play node from Mainmenu to World1 is to use a global singleton -- an autoload script that is always accessible during runtime.

You would need to create a new script and call it whatever you would like; I will use "global" for simplicity. Then, add the script as an autoload.

If you don't know how to add a singleton, see below:
Click "Project" in the top-left, then "Project Settings...". In the pop-up, find the tab at the top labeled "Autoload", finally clicking the folder button; navigate to the script file, then click the "Add" button at the far-right. Do note, Godot will most likely replace any underscores with spaces and capitalize all the words.
In the autoload script, make a variable called player. This is where you will transfer the node play from Mainmenu to World1.
Skip here if you already know how to add a singleton.

In the script for the player node in World1, change:

onready var m = get_node("/Mainmenu/Mainmenu/Camera2D/play")

to:

onready var m = Globals.player

(Note: Globals is just the name I am using; you can use whatever name you set for your autoload.)

(I am looking at this after writing it and realizing it might be a tad bit confusing. I hope this is easy enough for you to understand, and hopefully it helps.)

by (329 points)
selected by

Thank you so much! I knew I was missing something obvious.

+1 vote

If I'm reading your trees correctly, it seems like you included an extra "Mainmenu" in your get_node call's path.

by (84 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.
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.