Godot Resources

作者 gamedev-skillsd4b0e35550c5無授權條款1.3K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫11 天前更新

Design data-driven Godot 4.7 games with custom Resource classes: define typed data with class_name + @export, save/load .tres/.res files, instance and duplicate resources, and load on demand with ResourceLoader (incl. threaded loading). Use when modeling items/stats/configs as data in a Godot project, creating .tres resources, or working with custom Resource subclasses and ResourceLoader/ResourceSaver.

AI 產生的概覽

指導使用自訂 Resource 類別、.tres/.res 檔案與 ResourceLoader 設計資料驅動的 Godot 4.7 遊戲。

功能
說明如何用 class_name 與 @export 欄位將遊戲資料建模為自訂 Resource 子類別,並把實例撰寫成可在檢視器中編輯的 .tres 或 .res 檔案。涵蓋從節點參照資源、用 preload、load 或 ResourceLoader 載入、修改前複製共用資源,以及用 ResourceSaver 儲存。也列出常見陷阱,例如共用參照、res:// 唯讀路徑與循環參照。
適用情境
適用於在 Godot 專案中將物品、屬性、敵人設定、對話行或關卡中介資料表示為資料。也適合在檢視器中撰寫 .tres 資源,或處理自訂 Resource 子類別與 ResourceLoader/ResourceSaver。
執行需求
需要 Godot 4.7 專案環境;僅為說明文件,不附帶指令碼。配套參考檔案涵蓋執行緒載入、@tool 資源以及自訂 ResourceFormatLoader/Saver。

Godot Resources (4.x)

Model game data as reusable, Inspector-editable Resource objects instead of hard-coded values, and load/save them as .tres/.res. Targets Godot 4.7.

When to use

  • Use when representing items, stats, enemy configs, dialogue lines, or level metadata as data; authoring .tres files in the Inspector; or loading/saving custom resources.

When not to use: nodes/scene structure → godot-nodes-scenes; saving the player's runtime progress (engine-agnostic save format/slots) → save-systems; the C# variant of this pattern → godot-csharp.

Core workflow

  1. Subclass Resource with class_name and @export fields. It now appears in the "New Resource" dialog and as an @export type, editable in the Inspector.
  2. Create instances as .tres files in the FileSystem dock (text, diff-friendly) or .res (binary, smaller/faster). Edit their fields in the Inspector — no code needed.
  3. Reference resources from nodes via @export var data: ItemResource, or arrays @export var loot: Array[ItemResource].
  4. Load at runtime with preload (constant path) or load/ResourceLoader.load (variable path). Use threaded loading for large assets.
  5. Duplicate before mutating a shared resource at runtime, or every node using it changes too (resources are shared references).
  6. Save generated/edited resources with ResourceSaver.save.

Patterns

1. A custom data resource

gdscript
# item.gdextends Resourceclass_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.

2. Consume resources from a node

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])

3. Duplicate before mutating shared data

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 copy

4. Save and load a resource at runtime

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 null

Pitfalls

  • Resources are shared by reference. A single .tres assigned to many nodes is the same object — mutating it changes all of them (and re-saving the file). Call duplicate(true) for per-instance state.
  • class_name required to instance from the editor. Without it the class won't appear in the "New Resource" dialog or as an @export type.
  • res:// is read-only in exported games. Write runtime data to user://, never res://. ResourceSaver.save to res:// only works in the editor.
  • Storing nodes in a Resource doesn't serialize them. Resources hold data, not live scene nodes. Reference scenes via PackedScene, not node instances.
  • Cyclic resource references (A holds B holds A) can fail to save/load cleanly — keep data graphs acyclic or use IDs/lookups.
  • preload vs load. preload needs a constant path and loads with the script; load accepts a variable path at runtime. Use ResourceLoader.exists() before load to avoid errors on missing files.
  • Don't put secrets in .tres — they ship in plain text inside the export.

References

  • For threaded/background loading (load_threaded_request), @tool resources with custom setup, custom ResourceFormatLoader/Saver, resource-local-to-scene, and resource UIDs, read references/resource-patterns.md.

Related skills

  • godot-gdscript — @export annotations used to define resource fields.
  • godot-nodes-scenes — instancing scenes vs. sharing resource data.
  • save-systems — persisting runtime progress (separate from static data).
  • unity-scriptableobjects — the equivalent data-asset pattern in Unity.

來源與署名

來源:gamedev-skills/awesome-gamedev-agent-skills位於skills/godot/godot-resources提交d4b0e35

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架