godotgdscriptgodot4

How to obtain the absolute path of "res://"


Is there a way to use gdscript to obtain the absolute path of "res://" on the current device? I have encountered some issues that require the use of some code from C #, but the paths "res://" and "users://" cannot be used in C # (non godot API). I want to know if there is a gdscript method that can obtain the absolute paths of these two paths.

like :

    var dir = DirAccess.open("res://assets/")
    var path = dir.get_current_dir_absolute() # there is no function named that
    print_debug(path) # out put: D://.../assets

or something else.

I tried all the methods of the DirAccess class, but none of them worked. If someone could tell me how to do this, I would be very grateful!


Solution

  • use ProjectSettings.globalize_path() as such:

    var dir = DirAccess.open("res://assets/")
    var path = ProjectSettings.globalize_path("res://assets/")
    print_debug(path)
    

    However you might want to take a look at the documentation since:

    globalize_path with res:// will not work in an exported project.

    In such a case you'd want to use get_executable_path() & get_base_dir():

    path = OS.get_executable_path().get_base_dir().path_join("assets/")