Possible to get Scene/Resource path from GDScript?

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

I would like to get the resource path (ie “res://src/Node.tscn”) from GDScript (ie var my_path = get_scene_path()). Is this possible?

:bust_in_silhouette: Reply From: markopolo

It is possible to get the resource path of a script attached to a node. See this question.

self.get_script().get_path()

or

self.get_script().get_path().get_base_dir() # if you just want the directory

This doesn’t necessarily give you the path to the scene (i.e. if the scene is inherited or if the script is shared among multiple scenes). I do not know of a way to pick a node and ask it to tell you the scene that was loaded and caused it to be added to the tree.

:bust_in_silhouette: Reply From: Ninfur

In Godot 3.x:

node.filename

In Godot 4.0:

node.scene_file_path