AgentSkillsCN

new-resource

为游戏数据(如道具、技能、角色属性、任务、敌人设定等)创建自定义 Godot 资源类。当您需要一种可复用的数据容器时,此功能将大显身手。

SKILL.md
--- frontmatter
name: new-resource
description: Create a new custom Godot Resource class for game data (items, skills, character stats, quests, enemy definitions, etc.). Use when you need a reusable data container.
argument-hint: <resource-name> [properties...]
disable-model-invocation: true

Create New Custom Resource

Create a custom Resource class for: $ARGUMENTS

Background

Custom Resources in Godot are the standard way to define reusable data containers. They can be:

  • Created as .tres files in the editor inspector
  • Loaded with preload() or load()
  • Used as @export types on other scripts
  • Saved and loaded as part of the game state

MANDATORY — before writing any code, complete BOTH of these:

  1. Call the godot-docs skill:
    code
    activate_skill("godot-docs") # Look up Resource class and custom Resources. I need the Resource API, @export patterns, and how to create .tres files for a [RESOURCE_TYPE] data type.
    
  2. Read the resources best practices:
    code
    Read("docsbest-practices/04-resources-and-data.md")
    

Step 1 — Determine Resource Properties

Parse the arguments to identify what properties this resource needs. Common JRPG resource patterns:

Resource TypeTypical Properties
Itemname, description, icon, item_type, value, stack_size, effect
Skill / Abilityname, description, icon, mp_cost, damage, element, target_type, animation
CharacterStatsmax_hp, max_mp, attack, defense, speed, luck, element_resist
EnemyDefinitionname, stats, sprite_frames, loot_table, ai_type, exp_reward, gold_reward
Questid, title, description, objectives, rewards, prerequisites
QuestObjectivedescription, type, target, required_count, current_count
DialogueLinespeaker, text, portrait, choices, next_line_id
StatusEffectname, type, duration, strength, tick_damage, stat_modifiers
LootEntryitem, drop_chance, min_count, max_count
Equipmentname, slot, stats_bonus, required_level, sprite

Step 2 — Create the Resource Script

Create at game/systems/<system>resources/<name>.gd or game/data/<category>/<name>.gd.

Template

gdscript
@icon("res:/assetsicons/<resource_type>.svg")
class_name <PascalCaseName>
extends Resource
## <Brief description of what this resource represents.>
##
## <Extended description. How to create instances, what they're used for.>


## <Property description.>
@export var name: String = ""

## <Property description.>
@export var description: String = ""

## <Property description.>
@export var icon: Texture2D

## <Property description — use enum for categorical types.>
@export var <property>: <Type> = <default>

Resource Design Rules

  • Use @export on ALL properties so they're editable in the inspector
  • Use ## doc comments above every property
  • Use enums for categorical types (item type, element, target type, etc.)
  • Use other Resources as property types for composition (e.g., @export var stats: CharacterStats)
  • Use Array[<Type>] for typed arrays (e.g., @export var loot_table: Array[LootEntry])
  • Provide sensible defaults for all properties
  • Add @icon if you have a relevant icon

Enum Definitions

If the resource needs enums, define them in the resource script:

gdscript
class_name Item
extends Resource

enum Type {
	CONSUMABLE,
	EQUIPMENT,
	KEY_ITEM,
	MATERIAL,
}

enum Rarity {
	COMMON,
	UNCOMMON,
	RARE,
	LEGENDARY,
}

@export var item_type: Type = Type.CONSUMABLE
@export var rarity: Rarity = Rarity.COMMON

Step 3 — Create Example Data (optional)

If the user wants sample data, create .tres files:

code
[gd_resource type="Resource" script_class="<ClassName>" load_steps=2 format=3]

[ext_resource type="Script" path="res:/data/<category>/<name>.gd" id="1"]

[resource]
script = ExtResource("1")
name = "Example"
description = "An example resource."

Step 4 — Report

After creating files, report:

  1. Files created (with full paths)
  2. All properties with types and defaults
  3. Enums defined
  4. How to create instances in the editor (right-click in FileSystem > New Resource > select class)
  5. How to use from scripts (@export var item: Item, preload("res:/dataitemspotion.tres"))
  6. Suggested next steps