data_paths.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. .. _doc_data_paths:
  2. File paths in Godot projects
  3. ============================
  4. This page explains how file paths work inside Godot projects. You will learn how
  5. to access paths in your projects using the ``res://`` and ``user://`` notations,
  6. and where Godot stores project and editor files on your and your users' systems.
  7. Path separators
  8. ---------------
  9. To make supporting multiple platforms easier, Godot uses **UNIX-style path
  10. separators** (forward slash ``/``). These work on all platforms, **including
  11. Windows**.
  12. Instead of writing paths like ``C:\Projects\Game``, in Godot, you should write
  13. ``C:/Projects/Game``.
  14. Windows-style path separators (backward slash ``\``) are also supported in some
  15. path-related methods, but they need to be doubled (``\\``), as ``\`` is normally
  16. used as an escape for characters with a special meaning.
  17. This makes it possible to work with paths returned by other Windows
  18. applications. We still recommend using only forward slashes in your own code to
  19. guarantee that everything will work as intended.
  20. .. tip::
  21. The String class offers over a dozen methods to work with strings that represent file paths:
  22. - :ref:`String.filecasecmp_to() <class_String_method_filecasecmp_to>`
  23. - :ref:`String.filenocasecmp_to() <class_String_method_filenocasecmp_to>`
  24. - :ref:`String.get_base_dir() <class_String_method_get_base_dir>`
  25. - :ref:`String.get_basename() <class_String_method_get_basename>`
  26. - :ref:`String.get_extension() <class_String_method_get_extension>`
  27. - :ref:`String.get_file() <class_String_method_get_file>`
  28. - :ref:`String.is_absolute_path() <class_String_method_is_absolute_path>`
  29. - :ref:`String.is_relative_path() <class_String_method_is_relative_path>`
  30. - :ref:`String.is_valid_filename() <class_String_method_is_valid_filename>`
  31. - :ref:`String.path_join() <class_String_method_path_join>`
  32. - :ref:`String.simplify_path() <class_String_method_simplify_path>`
  33. - :ref:`String.validate_filename() <class_String_method_validate_filename>`
  34. Accessing files in the project folder (``res://``)
  35. --------------------------------------------------
  36. Godot considers that a project exists in any folder that contains a
  37. ``project.godot`` text file, even if the file is empty. The folder that contains
  38. this file is your project's root folder.
  39. You can access any file relative to it by writing paths starting with
  40. ``res://``, which stands for resources. For example, you can access an image
  41. file ``character.png`` located in the project's root folder in code with the
  42. following path: ``res://character.png``.
  43. Accessing persistent user data (``user://``)
  44. --------------------------------------------
  45. To store persistent data files, like the player's save or settings, you want to
  46. use ``user://`` instead of ``res://`` as your path's prefix. This is because
  47. when the game is running, the project's file system will likely be read-only.
  48. The ``user://`` prefix points to a different directory on the user's device.
  49. Unlike ``res://``, the directory pointed at by ``user://`` is created
  50. automatically and *guaranteed* to be writable to, even in an exported project.
  51. The location of the ``user://`` folder depends on what is configured in the
  52. Project Settings:
  53. - By default, the ``user://`` folder is created within Godot's
  54. :ref:`editor data path <doc_data_paths_editor_data_paths>` in the
  55. ``app_userdata/[project_name]`` folder. This is the default so that prototypes
  56. and test projects stay self-contained within Godot's data folder.
  57. - If :ref:`application/config/use_custom_user_dir <class_ProjectSettings_property_application/config/use_custom_user_dir>`
  58. is enabled in the Project Settings, the ``user://`` folder is created **next
  59. to** Godot's editor data path, i.e. in the standard location for applications
  60. data.
  61. * By default, the folder name will be inferred from the project name, but it
  62. can be further customized with
  63. :ref:`application/config/custom_user_dir_name <class_ProjectSettings_property_application/config/custom_user_dir_name>`.
  64. This path can contain path separators, so you can use it e.g. to group
  65. projects of a given studio with a ``Studio Name/Game Name`` structure.
  66. On desktop platforms, the actual directory paths for ``user://`` are:
  67. +---------------------+------------------------------------------------------------------------------+
  68. | Type | Location |
  69. +=====================+==============================================================================+
  70. | Default | | Windows: ``%APPDATA%\Godot\app_userdata\[project_name]`` |
  71. | | | macOS: ``~/Library/Application Support/Godot/app_userdata/[project_name]`` |
  72. | | | Linux: ``~/.local/share/godot/app_userdata/[project_name]`` |
  73. +---------------------+------------------------------------------------------------------------------+
  74. | Custom dir | | Windows: ``%APPDATA%\[project_name]`` |
  75. | | | macOS: ``~/Library/Application Support/[project_name]`` |
  76. | | | Linux: ``~/.local/share/[project_name]`` |
  77. +---------------------+------------------------------------------------------------------------------+
  78. | Custom dir and name | | Windows: ``%APPDATA%\[custom_user_dir_name]`` |
  79. | | | macOS: ``~/Library/Application Support/[custom_user_dir_name]`` |
  80. | | | Linux: ``~/.local/share/[custom_user_dir_name]`` |
  81. +---------------------+------------------------------------------------------------------------------+
  82. ``[project_name]`` is based on the application name defined in the Project Settings, but
  83. you can override it on a per-platform basis using :ref:`feature tags <doc_feature_tags>`.
  84. On mobile platforms, this path is unique to the project and is not accessible
  85. by other applications for security reasons.
  86. On HTML5 exports, ``user://`` will refer to a virtual filesystem stored on the
  87. device via IndexedDB. (Interaction with the main filesystem can still be performed
  88. through the :ref:`JavaScriptBridge <class_JavaScriptBridge>` singleton.)
  89. File logging
  90. ------------
  91. By default, Godot writes log files in ``user://logs/godot.log`` on desktop
  92. platforms. You can change this location by modifying the
  93. ``debug/file_logging/log_path`` project setting. Logs are rotated to keep older
  94. files available for inspection. Each session creates a new log file, with the
  95. old file renamed to contain the date at which it was rotated. Up to 5 log files
  96. are kept by default, which can be adjusted using the
  97. ``debug/file_logging/max_log_files`` project setting.
  98. File logging can also be disabled completely using the
  99. ``debug/file_logging/enable_file_logging`` project setting.
  100. When the project crashes, crash logs are written to the same file as the log
  101. file. The crash log will only contain a usable backtrace if the binary that was
  102. run contains debugging symbols, or if it can find a debug symbols file that
  103. matches the binary. Official binaries don't provide debugging symbols, so this
  104. requires a custom build to work. See
  105. :ref:`Debugging symbols <doc_introduction_to_the_buildsystem_debugging_symbols>`.
  106. for guidance on compiling binaries with debugging symbols enabled.
  107. .. note::
  108. Log files for :ref:`print<class_@GlobalScope_method_print>`
  109. statements are updated when standard output is *flushed* by the engine.
  110. Standard output is flushed on every print in debug builds only. In projects that
  111. are exported in release mode, standard output is only flushed when the project exits
  112. or crashes to improve performance, especially if the project is often printing
  113. text to standard output.
  114. On the other hand, the standard error stream
  115. (used by :ref:`printerr<class_@GlobalScope_method_printerr>`,
  116. :ref:`push_error<class_@GlobalScope_method_push_error>` and
  117. :ref:`push_warning<class_@GlobalScope_method_push_warning>`) is always
  118. flushed on every print, even in projects exported in release mode.
  119. For some use cases like dedicated servers, it can be preferred to have release
  120. builds always flush stdout on print, so that logging services like journald can
  121. collect logs while the process is running. This can be done by enabling
  122. ``application/run/flush_stdout_on_print`` in the Project Settings.
  123. Converting paths to absolute paths or "local" paths
  124. ---------------------------------------------------
  125. You can use :ref:`ProjectSettings.globalize_path() <class_ProjectSettings_method_globalize_path>`
  126. to convert a "local" path like ``res://path/to/file.txt`` to an absolute OS path.
  127. For example, :ref:`ProjectSettings.globalize_path() <class_ProjectSettings_method_globalize_path>`
  128. can be used to open "local" paths in the OS file manager
  129. using :ref:`OS.shell_open() <class_OS_method_shell_open>` since it only accepts
  130. native OS paths.
  131. To convert an absolute OS path to a "local" path starting with ``res://``
  132. or ``user://``, use :ref:`ProjectSettings.localize_path() <class_ProjectSettings_method_localize_path>`.
  133. This only works for absolute paths that point to files or folders in your
  134. project's root or ``user://`` folders.
  135. .. _doc_data_paths_editor_data_paths:
  136. Editor data paths
  137. -----------------
  138. The editor uses different paths for editor data, editor settings, and cache,
  139. depending on the platform. By default, these paths are:
  140. +-----------------+---------------------------------------------------+
  141. | Type | Location |
  142. +=================+===================================================+
  143. | Editor data | | Windows: ``%APPDATA%\Godot\`` |
  144. | | | macOS: ``~/Library/Application Support/Godot/`` |
  145. | | | Linux: ``~/.local/share/godot/`` |
  146. +-----------------+---------------------------------------------------+
  147. | Editor settings | | Windows: ``%APPDATA%\Godot\`` |
  148. | | | macOS: ``~/Library/Application Support/Godot/`` |
  149. | | | Linux: ``~/.config/godot/`` |
  150. +-----------------+---------------------------------------------------+
  151. | Cache | | Windows: ``%TEMP%\Godot\`` |
  152. | | | macOS: ``~/Library/Caches/Godot/`` |
  153. | | | Linux: ``~/.cache/godot/`` |
  154. +-----------------+---------------------------------------------------+
  155. - **Editor data** contains export templates and project-specific data.
  156. - **Editor settings** contains the main editor settings configuration file as
  157. well as various other user-specific customizations (editor layouts, feature
  158. profiles, script templates, etc.).
  159. - **Cache** contains data generated by the editor, or stored temporarily.
  160. It can safely be removed when Godot is closed.
  161. Godot complies with the `XDG Base Directory Specification
  162. <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>`__
  163. on Linux/\*BSD. You can override the ``XDG_DATA_HOME``, ``XDG_CONFIG_HOME`` and
  164. ``XDG_CACHE_HOME`` environment variables to change the editor and project data
  165. paths.
  166. .. note:: If you use `Godot packaged as a Flatpak
  167. <https://flathub.org/apps/details/org.godotengine.Godot>`__, the
  168. editor data paths will be located in subfolders in
  169. ``~/.var/app/org.godotengine.Godot/``.
  170. .. _doc_data_paths_self_contained_mode:
  171. Self-contained mode
  172. ~~~~~~~~~~~~~~~~~~~
  173. If you create a file called ``._sc_`` or ``_sc_`` in the same directory as the
  174. editor binary (or in `MacOS/Contents/` for a macOS editor .app bundle), Godot
  175. will enable *self-contained mode*.
  176. This mode makes Godot write all editor data, settings, and cache to a directory
  177. named ``editor_data/`` in the same directory as the editor binary.
  178. You can use it to create a portable installation of the editor.
  179. The `Steam release of Godot <https://store.steampowered.com/app/404790/>`__ uses
  180. self-contained mode by default.
  181. .. UPDATE: Not supported yet. When self-contained mode is supported in exported
  182. .. projects, remove or update this note.
  183. .. note::
  184. Self-contained mode is not supported in exported projects yet.
  185. To read and write files relative to the executable path, use
  186. :ref:`OS.get_executable_path() <class_OS_method_get_executable_path>`.
  187. Note that writing files in the executable path only works if the executable
  188. is placed in a writable location (i.e. **not** Program Files or another
  189. directory that is read-only for regular users).