godot-resources
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGodot Resources (4.x)
Godot Resources (4.x)
Model game data as reusable, Inspector-editable objects instead of hard-coded
values, and load/save them as /. Targets Godot 4.3+.
Resource.tres.res将游戏数据建模为可复用、可在Inspector中编辑的对象,而非硬编码值,并以/格式加载/保存。适用于**Godot 4.3+**版本。
Resource.tres.resWhen to use
适用场景
- Use when representing items, stats, enemy configs, dialogue lines, or level metadata as
data; authoring files in the Inspector; or loading/saving custom resources.
.tres
When not to use: nodes/scene structure → ; saving the player's
runtime progress (engine-agnostic save format/slots) → ; the C# variant of
this pattern → .
godot-nodes-scenessave-systemsgodot-csharp- 适用于将物品、属性、敌人配置、对话内容或关卡元数据表示为数据的场景;在Inspector中编辑文件;或加载/保存自定义资源时使用。
.tres
不适用场景:节点/场景结构 → ;保存玩家的运行时进度(与引擎无关的保存格式/存档槽)→ ;此模式的C#变体 → 。
godot-nodes-scenessave-systemsgodot-csharpCore workflow
核心工作流程
- Subclass with
Resourceandclass_namefields. It now appears in the "New Resource" dialog and as an@exporttype, editable in the Inspector.@export - Create instances as files in the FileSystem dock (text, diff-friendly) or
.tres(binary, smaller/faster). Edit their fields in the Inspector — no code needed..res - Reference resources from nodes via , or arrays
@export var data: ItemResource.@export var loot: Array[ItemResource] - Load at runtime with (constant path) or
preload/load(variable path). Use threaded loading for large assets.ResourceLoader.load - Duplicate before mutating a shared resource at runtime, or every node using it changes too (resources are shared references).
- Save generated/edited resources with .
ResourceSaver.save
- 继承类:使用
Resource和class_name字段定义子类。该类现在会出现在“新建资源”对话框中,且作为@export类型可在Inspector中编辑。@export - 创建实例:在文件系统面板中创建
.tres文件(文本格式,支持差异对比)或.tres文件(二进制格式,体积更小、加载更快)。无需编写代码,直接在Inspector中编辑其字段。.res - 从节点引用资源:通过或数组
@export var data: ItemResource引用资源。@export var loot: Array[ItemResource] - 运行时加载:使用(固定路径)或
preload/load(可变路径)加载资源。大型资源可使用线程加载。ResourceLoader.load - 修改前复制资源:在运行时修改共享资源前需调用,否则所有使用该资源的节点都会受到影响(资源为共享引用)。
duplicate - 保存生成/编辑的资源:使用保存资源。
ResourceSaver.save
Patterns
模式示例
1. A custom data resource
1. 自定义数据资源
gdscript
undefinedgdscript
undefineditem.gd
item.gd
extends Resource
class_name ItemResource
@export var id: StringName = &""
@export var display_name: String = "Item"
@export_multiline var description: String = ""
@export var icon: Texture2D
@export var max_stack: int = 99
@export var value: int = 0
Create `sword.tres` from this class in the FileSystem dock and fill it in the Inspector.extends Resource
class_name ItemResource
@export var id: StringName = &""
@export var display_name: String = "Item"
@export_multiline var description: String = ""
@export var icon: Texture2D
@export var max_stack: int = 99
@export var value: int = 0
在此类基础上,在文件系统面板中创建`sword.tres`文件,并在Inspector中填写内容。2. Consume resources from a node
2. 从节点中使用资源
gdscript
extends Node
@export var starting_items: Array[ItemResource] = [] # drag .tres files in the Inspector
func _ready() -> void:
for item in starting_items:
print("Have: %s (x%d max)" % [item.display_name, item.max_stack])gdscript
extends Node
@export var starting_items: Array[ItemResource] = [] # 在Inspector中拖拽.tres文件
func _ready() -> void:
for item in starting_items:
print("拥有:%s(最大堆叠数:%d)" % [item.display_name, item.max_stack])3. Duplicate before mutating shared data
3. 修改共享数据前复制资源
gdscript
func give_unique_copy(template: ItemResource) -> ItemResource:
# true = deep copy sub-resources too; false = shallow (shares sub-resources).
var copy: ItemResource = template.duplicate(true)
copy.value += 5 # mutating the copy won't touch the template .tres
return copygdscript
func give_unique_copy(template: ItemResource) -> ItemResource:
# true = 深度复制子资源;false = 浅复制(共享子资源)。
var copy: ItemResource = template.duplicate(true)
copy.value += 5 # 修改副本不会影响模板.tres文件
return copy4. Save and load a resource at runtime
4. 运行时保存和加载资源
gdscript
func save_config(cfg: Resource) -> void:
ResourceSaver.save(cfg, "user://config.tres") # user:// = writable app data dir
func load_config() -> Resource:
if ResourceLoader.exists("user://config.tres"):
return ResourceLoader.load("user://config.tres")
return nullgdscript
func save_config(cfg: Resource) -> void:
ResourceSaver.save(cfg, "user://config.tres") # user:// = 可写入的应用数据目录
func load_config() -> Resource:
if ResourceLoader.exists("user://config.tres"):
return ResourceLoader.load("user://config.tres")
return nullPitfalls
注意事项
- Resources are shared by reference. A single assigned to many nodes is the same object — mutating it changes all of them (and re-saving the file). Call
.tresfor per-instance state.duplicate(true) - required to instance from the editor. Without it the class won't appear in the "New Resource" dialog or as an
class_nametype.@export - is read-only in exported games. Write runtime data to
res://, neveruser://.res://toResourceSaver.saveonly works in the editor.res:// - Storing nodes in a Resource doesn't serialize them. Resources hold data, not live
scene nodes. Reference scenes via , not node instances.
PackedScene - Cyclic resource references (A holds B holds A) can fail to save/load cleanly — keep data graphs acyclic or use IDs/lookups.
- vs
preload.loadneeds a constant path and loads with the script;preloadaccepts a variable path at runtime. UseloadbeforeResourceLoader.exists()to avoid errors on missing files.load - Don't put secrets in — they ship in plain text inside the export.
.tres
- 资源为共享引用:单个文件被多个节点引用时,它们指向的是同一个对象——修改该对象会影响所有引用它的节点(并会重新保存文件)。如需每个实例独立状态,请调用
.tres。duplicate(true) - 必须添加才能在编辑器中实例化:没有
class_name的话,该类不会出现在“新建资源”对话框中,也无法作为class_name类型使用。@export - 导出游戏后为只读:运行时数据需写入
res://,绝不能写入user://。仅在编辑器中才能使用res://将资源保存到ResourceSaver.save。res:// - 资源中存储节点无法序列化:资源用于存储数据,而非实时场景节点。需通过引用场景,而非节点实例。
PackedScene - 循环资源引用(A包含B,B包含A)可能导致保存/加载失败——请保持数据图无环,或使用ID/查找方式。
- 与
preload的区别:load需要固定路径,会随脚本一起加载;preload支持运行时可变路径。加载前使用load可避免文件缺失导致的错误。ResourceLoader.exists() - 不要在中存储敏感信息——它们会以明文形式随导出包发布。
.tres
References
参考资料
- For threaded/background loading (),
load_threaded_requestresources with custom setup, custom@tool/ResourceFormatLoader, resource-local-to-scene, and resource UIDs, readSaver.references/resource-patterns.md
- 如需了解线程/后台加载()、带自定义设置的
load_threaded_request资源、自定义@tool/ResourceFormatLoader、场景本地资源及资源UID,请阅读Saver。references/resource-patterns.md
Related skills
相关技能
- —
godot-gdscriptannotations used to define resource fields.@export - — instancing scenes vs. sharing resource data.
godot-nodes-scenes - — persisting runtime progress (separate from static data).
save-systems - — the equivalent data-asset pattern in Unity.
unity-scriptableobjects
- —— 用于定义资源字段的
godot-gdscript注解。@export - —— 实例化场景与共享资源数据的区别。
godot-nodes-scenes - —— 持久化运行时进度(与静态数据分离)。
save-systems - —— Unity中对应的资源数据模式。
unity-scriptableobjects