How do I get a node with editor in Resource?

Godot Version



I have a RPG system, where the event are resources, called by a Node.
I’m writing a functionality, where upon triggering, the Node that holds the event resource will be teleported to a specific spot in the node tree. (It will require reparent() so simply export Vector2 wouldn’t work.)

Now, I can make a new Node class for it, and just let it receive a signal from the event resource, but for the spirit of the consistency, I want the logic to be within the Event Resource.
However, in order to reparent it, I’ll have to set the node to reparent to in the editor, which is what I haven’t figure out.

As shown, neither can I export a node from a Resource class, nor export a path and use Node.get_node(path) to get the node.

What do I do here? Thanks for reading.

Use a node to run get_node I believe your player will work. The path will be evaluated relative to the player though, so it may help to try passing in a parent of map or world parameter and use that.

func trigger(_triggered_with: EventTriggeror, _triggered_by: Player):
    var destination: Marker2D = _triggered_by.get_node(move_to)
1 Like

What is the move_to path relative to? If it’s relative to SceneTree.current_scene, the following may work:

var scene := (Engine.get_main_loop() as SceneTree).current_scene
var node := scene.get_node(move_to) as Marker2D

Otherwise, I’d go with @gertkeno solution.

1 Like

Can’t check for now since I’m going through major refactor, but this looks promising, I’ll check solution once I confirm it working

extends Event
class_name MoveTriggeror

@export_node_path("Marker2D") var move_to: NodePath

func trigger(_triggered_with: EventTriggeror, _triggered_by: Player):
	var destination: Marker2D = _triggered_with.get_node_or_null(move_to)
	if destination != null:
		_triggered_with.position = Vector2.ZERO
		_triggered_with.reparent(destination, false)

This is what I settled with. Since the path is broke once the reparent is done, I added a null check just to avoid the red warning.
This would break if I want the node to move multiple time, but it’s good enough for the project, thanks for helping.