0 votes

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

asked Aug 24, 2018 in Engine by Kevar (19 points)

1 Answer

+1 vote
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
answered Aug 25, 2018 by eska (1,034 points)
selected Aug 25, 2018 by Kevar

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

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 How to use this Q&A? before posting your first questions.