Search code examples
godotgdscript

How to directly get Node instead of NodePath in export variable?


Instead of maintaining 2 separate variables for my exported node path, like this:

extends Node2D

export(NodePath) var target_path
var target_node

func _ready():
    target_node=get_node(target_path)

is there any way to export only 1 variable and directly get the node?


Solution

  • There is, and it's a one liner:

    extends Node2D
    export (NodePath) onready var target_node=get_node(target_node)
    

    Turns out someone did it 2 years ago.