+3 votes

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.

Help would be appreciated.

in Engine by (496 points)

3 Answers

+5 votes
Best answer

The only thing I could find is Recource.get_path() which works for everything that is inherited by Resource. (e.g. Script, see 1 for a full list).

by (488 points)
selected by

After learning about the Resource inheritance, I managed to find the path by using the running scripts path. (Since Script inherits Resource)

var dir = self.get_script().get_path()
dir = str(dir).replace("command_list.gd", "commands/") # replacing command_list.gd with the folder path

command_script.gd is where this is run.
commands/ is the folder I was trying to get a path to.


I found this useful, and I will add a remark to this for anyone else coming along.

You don't have to parse the string, there is a String.get_base_dir() method that does this.

+3 votes

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 :

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

by (177 points)

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.

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.

+2 votes

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.

by (44 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.