Topic was automatically imported from the old Question2Answer platform.
Asked By
Tybobobo
Old Version
Published before Godot 3 was released.
I am attempting to find the current folder path to a specific script (or node).
I am not looking for Node’s get_path(), as that return a NodePath; path to a node. I am looking for the file path. ex: “res://path/to/folder/”.
Anyone know how I may get this path without having to hardcode the res://path/to ?
The reason I want this is because I am working on creating a scene that can be easily imported into other game projects; so the res://path may change depending on project.
I do not know how to get the path but keep in mind that the script knows his path.
For example, let’s say that you have a script in "res://instances/player.gd" and a bullet.scn in "res://instances/bullet.scn"
You can call bullet.scn from the player.gd like this : preload("res://instances/bullet.scn")
or preload("bullet.scn")
The second way is the what you want.
You can use all the known ways to navigate where you want. If bullet.scn was in "res://bullets/bullet.scn" you can call it like that preload("../bullets/bullet.scn")
This only works for me if I use load. With preload it gives me “Parser Error: Can’t preload resource at path: …” even if the path it spits out is correct. I only tried this with scenes, not other resources. Not sure why it happens.
Edit: Nevermind, loading didn’t work either, the scene had some broken dependencies. fixed those and now both work as described.
markopolo | 2017-07-04 18:43
This just works, pick this as the best answer, you don’t need any parsing.
I’ve tested it on, Windows Debug and Windows Release.
Just to clarify, if you want the resource path of a instanced scene, you can simply use Node.filename. It works if the node actually ‘represents’ a scene loaded from a file - see Node.filename. This (part of the) question was also recently answered here.