+1 vote

In the script of Main scene (main.gd) I want to create instance of another scene (player scene), but player also have a player.gd script attached to player scene.

Problem:
In player scene script (player.gd), there are so many paths ($this, $that, $those, etc.), if I run player scene alone (by pressing F6) it runs correctly with all of those paths, but if Run the main scene (by pressing F5), those paths that are working in player scene are all NULL/nil.

Why? and how can I have some separated scenes with their own scripts and paths, and I just want to load that player inside my main scene.

Godot version Godot_v4.0-alpha1_linux.64
in Engine by (26 points)

1 Answer

+1 vote
Best answer

Try using the magic of NodePaths. When the game runs, those variables marked with the "$" operator are turned into NodePaths. Here's a trick that I use which makes keeping track of those nodes easy.

Export a variable to the editor, and then assign that to a variable which is used by the script to stand in for the node.

# This places a widget in the Inspector of the editor which allows you to select the node in the scene tree.
export (NodePath) var some_node_path
onready var some_node = get_node(some_node_path)

func _ready():
    # Now use the variable in any part of the script.
    some_node.position = Vector2()

Hope this helps.

by (2,462 points)
selected by
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.