How to fetch the FILEPATH for an object? [Godot 3.0]

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

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.

:bust_in_silhouette: Reply From: hilfazer

For Nodes you can use filename property:

myNode.filename

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

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!

Corruptinator | 2018-03-24 22:08