0 votes

Hello! I am trying to utilize GDScript to tell the engine to find the directory location of an object's tscn file or res file.

This is what I thought might work when you place it in an autoload script as a function:

func path_fetch(idx):
    print(idx)
    var fetcher = EditorFileSystemDirectory.new()
    print(fetcher.get_file_path(idx.get_instance_id())) 
    pass

It was a theory in thought that "idx"is the node or object you want to fetch the id off from (i.e. [Node:####]) using get_instance_id(), then after starting the EditorFileSystemDirectory as new I could use use get_file_path() command to get the directory path (ex. res://path/to/file.tscn)

I did get a print when using get file path, and it didn't come out right when the file I wanted is in a folder "res://", also I ended up with this error:

0:00:02:0130 - Index p_idx=1289 out of size (files.size()=0)
----------
Type:Error
Description: 
Time: 0:00:02:0130
C Error: Index p_idx=1289 out of size (files.size()=0)
C Source: editor\editor_file_system.cpp:87
C Function: EditorFileSystemDirectory::get_file

1289 is one of many instance ids for an object (####), and for some reason it says it's out of size.

I wouldn't be surprised that this is not the way to fetch the filepath of an object but unless there is a better way to do it I'm open for ideas.

asked Mar 24, 2018 in Engine by Corruptinator (198 points)

1 Answer

+3 votes
Best answer

For Nodes you can use filename property:

myNode.filename
answered Mar 24, 2018 by hilfazer (2,154 points)
selected Mar 24, 2018 by Corruptinator

Oh wow, I was not aware of this function. Definitely saves me a lot of trouble despite I put up too much effort. ":D

But that's just the joy of attempting to dig into the GODOT API and discover what is there to figure out what works. Thanks!

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.