GDScript: get resource path of current scene (*.tscn)

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

Hi!


TL;DR:

In a tool script, I need to get the resource path of the tscn file of the current scene. Something like this (mockup code):

var res_path = get_tree().edited_scene_root.get_resource_path()

#returns "res://scenes/world1/townA.tscn"

A more in depth explanation:

I want to build a workflow like this:

  • Creating/editing some images in a drawing program
  • Saving thoses images in a folder within the game project hierarchy
  • Creating a json file at the same location to add some data about each image (size, position, etc.)
  • In Godot: creating/saving a scene at the same location
  • Adding a node with my tool script attached
  • From the tool script, loading the json file and creating some sprites using the data read from the json file and the images present in the folder
  • Later, after changing the images and the json file, using the tool script to update the created sprites

The idea here is to work relatively to the scene file because I will need to create a lot of setup like that in my game so I can’t rely on a fix absolute path for the images and json files. And I need to automate this task because I will have a lot of scene to produce and update.

The problem I’m facing is to access the json file: I haven’t found a way to get/build its path.

Remark:

get_tree().edited_scene_root.get_script().get_path()

doesn’t fit my needs because the script of the root node is not guaranteed to be in the correct folder.


Thank you for your help!

Kevar

:bust_in_silhouette: Reply From: eska

The filename member of the root node of an instantiated scene retrieves the path to the scene file:

get_tree().edited_scene_root.filename

Thank you, it’s exactly what I needed :slight_smile:

Kevar | 2018-08-25 12:16

I tried this but I get an error. What gives?

KramchayDig | 2020-11-21 04:36

Try changing edited_scene_root to current_scene.
e.g. get_tree.current_scene.filename instead of get_tree().edited_scene_root.filename

AshCam | 2021-01-12 23:25

get_tree().current_scene.filename works, thanks

Ingeniou5 | 2022-12-11 02:30

:bust_in_silhouette: Reply From: Ninfur

In Godot 4.0:

get_tree().current_scene.scene_file_path
2 Likes