+1 vote

The problem:

I am attempting to make a Godot project that is self-contained, meaning files are both saved and written to the res:// directory rather than the user:// directory. I feel it is more simplistic to keep the files all in the same place rather than saving to the user:// directory. Despite researching it quite a bit I'm still not sure whether this is possible.

In the Godot docs on the File System, it says this about the res:// path: This file system is read-write only when running the project locally from the editor. When exported or when running on different devices (such as phones or consoles, or running from DVD), the file system will become read-only and writing will no longer be permitted.

This behavior is exactly what I am seeing, as my project can both read and write to the res:// directory within the editor but can only read and not write to the res:// directory upon exporting the project to a .exe and .pck file.

What I have now:

I ran the following code from both the editor and the exported versions.

var saveFile = File.new()
print(saveFile.file_exists("res://save_file.res"))
var err = saveFile.open("res://save_file.res", File.READ_WRITE)
print(saveFile.is_open())
print(err)

Editor output:

True
True
0 (indicating no error)

And the save file was able to be read from and written to without any issues.

Exported project output:

True
False
7 (indicating file does not exist, even when I just checked if it did exist and the output was True)

And the save file was unable to be read from or written to. If I use File.READ I can read from the file just fine. Attempting to read or write to this file when using the File.READ_WRITE method results in these errors in the debugger window:

ERROR: File must be opened before use.
   At: core/bind/core_bind.cpp:1939
ERROR: File must be opened before use.
   At: core/bind/core_bind.cpp:2152

Is there any way keep my project contained in the res:// directory? Also, can someone explain why it was designed to work fine in the editor but not once exported? My thanks in advance!

in Engine by (80 points)
edited by

1 Answer

+3 votes
Best answer

It is designed this way, because writing to res:// will not work everywhere. iOS and Android won't allow it at all and windows sometimes is picky, too. Note that you could define the prefix in a global variable and then use res:// for testing and user:// when exporting.

You can work around this, but it's a bit involved, my list is windows specific.

  1. You can't use paths that exist within your project and get exported,
    because they will be packaged into the pck which isn't writable.
    Godot will search the pck first and if it finds a directory with the name
    you requested it will use that one, so you can't write to it or its files.
  2. You need to use ./foo instead of res://foo again to not point to
    the pck file.
  3. Directories can't be created using Directory.dir_create(). You'll
    need to use the system shell for this using OS.execute("cmd.exe", ["/C mkdir foo"])

Using those workarounds you should be able to stick to your project directory.

by (1,147 points)
selected by

Ah okay, I see what you're saying. You have answered my question. I think rather than doing such an involved work around I will stick to the user:// directory. Thank you too for your excellent explanation of the res:// directory and how it is directly related to the .pck file, I feel I have a better understanding of Godot now.

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 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 webmaster@godotengine.org with your username.