How do you use @export_node_path in Godot 4

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

New to programming so I followed a tutorial a while back on Godot 3.X and after downloading and upgrading the project to beta. Exporting NodePaths changed and gives errors when trying to use them the same way. in 3.X it looked like this:

export(NodePath) onready var some_node 

and after assigning a node to the variable in the instector. I could use the shorthand variable and . to change properties and whatever. But in 4, it looks like this:

@export_node_path(Node2D) @onready var some_node 

But whenever I try to use the variable in any way, Godot gives me errors like this:

Cannot find property “modulate” on base “NodePath”.

I’ve looked at the docs and tried to find information but can’t find straight answers.

To follow up, if you export a NodePath, you have to use get_node() on that path to actually reference the node. This was true in 3.x and is still true in 4.

kidscancode | 2023-01-21 21:22

:bust_in_silhouette: Reply From: ipdramon

In Godot4 you can export a complete node instead.

So you can use

@export var some_node: Node2D

and then not only can you choose the node with an easy to use popup window but use the node directly as well without any @onready and so on.
I hope this part of the docs will help you with this: GDScript exported properties — Godot Engine (latest) documentation in English

Good luck with your project!

That’s exactly what I needed. Thank you so much!

Fun_Protection | 2023-01-21 20:14