文件系统

简介

文件系统管理资源的存储方式和访问方式。精心设计的文件系统还允许多个开发人员在协作时编辑相同的源文件和资源。Godot将所有资源作为文件存储在其文件系统中。

实现

文件系统将资源存储在磁盘上。从脚本到场景或PNG图像的任何内容都是引擎的资源。如果一个资源包含引用磁盘上其他资源的属性,则它还将包括这些资源的路径。如果一个资源具有内置的子资源,则该资源与所有捆绑的子资源一起保存在单个文件中。例如,字体资源通常与字体纹理捆绑在一起。

Godot文件系统避免使用元数据文件。现有的资源管理器和VCS比我们能够实现的任何东西都要好,因此Godot会尽力与SVN、Git、Mercurial、Perforce等一起使用。

文件系统内容示例:

  1. /project.godot
  2. /enemy/enemy.tscn
  3. /enemy/enemy.gd
  4. /enemy/enemysprite.png
  5. /player/player.gd

project.godot

The project.godot file is the project description file, and it is always found at the root of the project. In fact, its location defines where the root is. This is the first file that Godot looks for when opening a project.

This file contains the project configuration in plain text, using the win.ini format. Even an empty project.godot can function as a basic definition of a blank project.

路径分隔符

Godot only supports / as a path delimiter. This is done for portability reasons. All operating systems support this, even Windows, so a path such as C:\project\project.godot needs to be typed as C:/project/project.godot.

资源路径

访问资源时,使用主机OS文件系统布局可能很麻烦且不可移植。为了解决这个问题,创建了特殊路径 res://

The path res:// will always point at the project root (where project.godot is located, so res://project.godot is always valid).

仅当从编辑器本地运行项目时,此文件系统才是读写的。导出时或在其他设备(例如电话或控制台,或从DVD运行)上运行时,文件系统将变为只读状态,并且将不再允许写入。

用户路径

Writing to disk is still needed for tasks such as saving game state or downloading content packs. To this end, the engine ensures that there is a special path user:// that is always writable. This path resolves differently depending on the OS the project is running on. Local path resolution is further explained in 数据路径.

主机文件系统

另外,也可以使用主机文件系统路径,但是不建议将其用于已发布的产品,因为不能保证这些路径在所有平台上都可以使用。但是,在Godot中编写开发工具时,使用主机文件系统路径可能会很有用。

缺点

这种简单的文件系统设计有一些缺点。第一个问题是,资源四处移动(重新命名资源或将其从项目中的一条路径移动到另一条路径)将破坏现有对这些资源的引用。这些引用将必须重新定义以指向新资源的位置。

为避免这种情况,请在Godot中的文件系统停靠面板上,进行所有移动、删除和重命名操作。切勿从Godot外部移动资源,否则必须手动修复依赖关系(Godot会检测到此问题并帮助您修复它们,但是为什么要走这条艰难的路线呢?)。

The second is that, under Windows and macOS, file and path names are case insensitive. If a developer working in a case insensitive host file system saves an asset as myfile.PNG, but then references it as myfile.png, it will work fine on their platform, but not on other platforms, such as Linux, Android, etc. This may also apply to exported binaries, which use a compressed package to store all files.

建议您的团队在使用Godot一起工作时明确定义文件的命名约定。一种简单的万无一失的约定是仅允许使用小写的文件名和路径名。