Hello there,
I use Godot 4.0.2-stable on Debian Testing, installed via Flatpak. I'm trying to copy a directory from another directory relative to the executable file into the user:// at runtime. Let's say a have such a structure "local/inner/" right next to the .x86_64/.exe file. What I tried:

To open the outer ("local") folder, I tried:
var dir = DirAccess.open("local/"), also without the slash, with "./", and even with absolute path.

Then I tried copying the inner folder using:
dir.copy("inner/", "user://inner/"); again, I tried without the slashes at the end, with "./" at the beginning, and even tried both absolute paths starting with "/home" or "file:///home".

Also, I made an attempt using:
DirAccess.copy_absolute(...), of course with absolute paths, but also with "user://".

It seems like neither of these works for me. In order to make sure the Godot editor (and the exported game) actually has access to these directories, I enabled Flatpak full filesystem access and checked permissions for the folder I'm trying to use. Also, I tried standalone (non-Flatpak) Godot from the official website. I have read the Godot Docs on DirAccess, File system, and File paths in Godot projects several times, yet I can't figure out what I am doing wrong.
Wrapping these commands in print(), I got either 1, 7, or 12 Error codes. Some other operations like make_dir(<path>) work; dir_exists(<path>) returns true.

Can anybody help me with this problem? Thank you a lot for your time!

  • DaveTheCoder replied to this.
  • DaveTheCoder Thank you a lot for trying to help! Weirdly, but even with relative paths, I can list files and folders using get_directories() and get_files(), though I also did some experiments with different files, and here's what I managed to find out:

    Considering such file structure,

    I executed this code in _ready() alone:

    The pre-last line (dir.copy(...) ) doesn't work (even though user.get_current_dir() returns the absolute path), however, in Godot Docs it is said that "Both arguments should be paths to files, either relative or absolute."
    What seems strange about DirAccess (which I think is supposed to imply that it works with Directories) is that the last line (DirAccess.copy_absolute(...)) returns Error 12 ("Failed to open /home/michael/.../outer/test" in Debugger), while this code:

    works for some reason, copying a single file instead.
    I clicked "Open C++ source on GitHub" in Debugger, and it looks like DirAccess::copy tries to open its arguments as files.

    Is it a bug apparently or is it intended to be implemented like that? UPD: well, "it's not a bug, it's a feature", I see...
    Sorry for such a huge thread with a lot of text here. Thank you again for your time!

    ya_Bob_Jonez changed the title to Cannot / How do I copy a local folder (relative to the executable) into user://? .

    DaveTheCoder Thank you for replying, but I mentioned that I had read Godot Docs on "File paths in Godot projects", "File system", and "DirAccess". I tried many ways to write paths, but neither worked for me.

    Sorry, I read your post too quickly and missed that.

    ya_Bob_Jonez To open the outer ("local") folder, I tried:
    var dir = DirAccess.open("local/"), also without the slash, with "./", and even with absolute path.

    Do the DirAccess.open() calls result in errors? If not, after the open(), what's the output of DirAccess.get_current_dir()?

      DaveTheCoder Do the DirAccess.open() calls result in errors? If not, after the open(), what's the output of DirAccess.get_current_dir()?

      No, it opens the folder correctly; get_current_dir() shows the correct (resolves to absolute) path. Seems like the problem is with copying.

      ya_Bob_Jonez relative to the executable file

      I did some experimentation.

      Non-absolute paths seem to be relative to the project folder ("res://").

      To access the folder containing the Godot executable, I had to use either an absolute file system path that begins with "/", or OS.get_executable_path(), e.g.:

      var path: String =  OS.get_executable_path().get_base_dir()
      var dir: DirAccess = DirAccess.open(path)

      I was able to list the contents of that directory, but I didn't try copying any directories or files. I don't think DirAccess can be used to copy a file between different directories. That should be possible with FileAccess or OS.execute().

      (I'm also using Linux, Pop!_OS, which is Ubuntu-based. I installed Godot by downloading the executable, rather than using a package manager such as Flatpak.)

        DaveTheCoder Thank you a lot for trying to help! Weirdly, but even with relative paths, I can list files and folders using get_directories() and get_files(), though I also did some experiments with different files, and here's what I managed to find out:

        Considering such file structure,

        I executed this code in _ready() alone:

        The pre-last line (dir.copy(...) ) doesn't work (even though user.get_current_dir() returns the absolute path), however, in Godot Docs it is said that "Both arguments should be paths to files, either relative or absolute."
        What seems strange about DirAccess (which I think is supposed to imply that it works with Directories) is that the last line (DirAccess.copy_absolute(...)) returns Error 12 ("Failed to open /home/michael/.../outer/test" in Debugger), while this code:

        works for some reason, copying a single file instead.
        I clicked "Open C++ source on GitHub" in Debugger, and it looks like DirAccess::copy tries to open its arguments as files.

        Is it a bug apparently or is it intended to be implemented like that? UPD: well, "it's not a bug, it's a feature", I see...
        Sorry for such a huge thread with a lot of text here. Thank you again for your time!