The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

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

in Engine by (20 points)

2 Answers

+11 votes
Best answer

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

get_tree().edited_scene_root.filename
by (1,096 points)
selected by

Thank you, it's exactly what I needed :)

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

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

get_tree().current_scene.filename works, thanks

+2 votes

In Godot 4.0:

get_tree().current_scene.scene_file_path
by (1,122 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.