Claude
Skills
Sign in
Back

godot-resource-data-patterns

Included with Lifetime
$97 forever

Expert blueprint for data-oriented design using Resource/RefCounted classes (item databases, character stats, reusable data structures). Covers typed arrays, serialization, nested resources, and resource caching. Use when implementing data systems OR inventory/stats/dialogue databases. Keywords Resource, RefCounted, ItemData, CharacterStats, database, serialization, @export, typed arrays.

Designscripts

What this skill does


# Resource & Data Patterns

Resource-based design, typed arrays, and serialization define reusable, inspector-friendly data structures.

## Available Scripts

### [custom_data_resource.gd](scripts/custom_data_resource.gd)
Pattern for defining serialized data containers (Items, Spells, Stats) for the Inspector.

### [resource_flyweight_caching.gd](scripts/resource_flyweight_caching.gd)
Expert example of the Flyweight pattern for memory-efficient resource sharing.

### [resource_local_to_scene.gd](scripts/resource_local_to_scene.gd)
Handling "Local to Scene" resources and `duplicate()` to prevent cross-contamination.

### [character_stats_resource.gd](scripts/character_stats_resource.gd)
Reactive data containers that emit signals when internal properties are modified.

### [resource_save_system.gd](scripts/resource_save_system.gd)
Pattern for serializing complex game state directly into `.tres` files on disk.

### [resource_based_inventory.gd](scripts/resource_based_inventory.gd)
Managing item collections and inventory logic using serialized Resource arrays.

### [flyweight_enemy_config.gd](scripts/flyweight_enemy_config.gd)
Using shared Resources to configure many entities efficiently (HP, Skins, Speed).

### [dynamic_resource_generation.gd](scripts/dynamic_resource_generation.gd)
Creating and modifying Resource instances programmatically at runtime (Loot, Procedural).

### [resource_preloading_strategy.gd](scripts/resource_preloading_strategy.gd)
Preventing frame drops by caching resources in a dictionary before gameplay starts.

### [nested_resource_serialization.gd](scripts/nested_resource_serialization.gd)
Building and saving complex data hierarchies using nested Resource properties.

## NEVER Do in Resource Design

- **NEVER modify resource instances directly** — Without `.duplicate()`, changing a value (like HP) modifies the `.tres` file on disk for everyone [26].
- **NEVER use untyped arrays in Resources** — `@export var items: Array` allows logic errors. Always use `Array[ResourceClass]` for type safety [27].
- **NEVER store Node references in Resources** — Objects that only exist in a specific SceneTree (like Players/Projectiles) cannot be serialized. Store `NodePath` or `UID` [30].
- **NEVER perform heavy calculations in Resource getters/setters** — Resources should be data containers. Offload logic to Nodes or specialized RefCounted classes.
- **NEVER skip `ResourceSaver.save()` error checks** — Saving can fail due to permissions, disk space, or path issues. Always check the return code [31].
- **NEVER use Resources for high-frequency runtime data** — If a value changes 60 times a second (like velocity), a standard variable is faster than a Resource property.
- **NEVER allow circular Resource references** — If A.tres references B.tres and B.tres references A.tres, the engine may crash on load.
- **NEVER forget the `_init` defaults** — Resources created via `new()` or in the Inspector need default values in their constructor to be editable [15].
- **NEVER share a Resource between entities if they need unique state** — Use `resource_local_to_scene = true` in the Inspector for components [26].
- **NEVER use `.tres` for massive datasets** — If you have 10,000 items, a JSON or custom binary format might be more efficient than individualized Resource files.

---

| Type | Use Case | Serializable | Can Save to Disk | Inspector Support |
|------|----------|-------------|-----------------|-------------------|
| `Resource` | Data that needs saving/loading | ✅ | ✅ | ✅ |
| `RefCounted` | Temporary runtime data | ❌ | ❌ | ❌ |
| `Node` | Scene hierarchy entities | ✅ (scene files) | ✅ | ✅ |

## When to Use Resources

**Use Resources For:**
- Item definitions (weapons, consumables, equipment)
- Character stats/progression systems
- Skill/ability data
- Configuration files
- Dialogue databases
- Enemy/NPC templates

**Use RefCounted For:**
- Temporary calculations
- Runtime-only state machines
- Utility classes without data persistence

## Implementation Patterns

### Pattern 1: Custom Resource Class

```gdscript
# item_data.gd
extends Resource
class_name ItemData

@export var item_name: String = ""
@export var description: String = ""
@export_enum("Weapon", "Consumable", "Armor") var item_type: int = 0
@export var icon: Texture2D
@export var value: int = 0
@export var stackable: bool = false
@export var max_stack: int = 1

func use() -> void:
    match item_type:
        0:  # Weapon
            print("Equipped weapon: ", item_name)
        1:  # Consumable
            print("Consumed: ", item_name)
        2:  # Armor
            print("Equipped armor: ", item_name)
```

**Create Resource Instances:**
1. In Inspector: **Right-click → New Resource → ItemData**
2. Fill in properties, **Save** as `res://items/health_potion.tres`

### Pattern 2: Character Stats Resource

```gdscript
# character_stats.gd
extends Resource
class_name CharacterStats

@export var max_health: int = 100
@export var max_mana: int = 50
@export var strength: int = 10
@export var defense: int = 5
@export var speed: float = 100.0

var current_health: int = max_health:
    set(value):
        current_health = clampi(value, 0, max_health)

var current_mana: int = max_mana:
    set(value):
        current_mana = clampi(value, 0, max_mana)

func take_damage(amount: int) -> int:
    var actual_damage := maxi(amount - defense, 0)
    current_health -= actual_damage
    return actual_damage

func heal(amount: int) -> void:
    current_health += amount

func duplicate_stats() -> CharacterStats:
    var stats := CharacterStats.new()
    stats.max_health = max_health
    stats.max_mana = max_mana
    stats.strength = strength
    stats.defense = defense
    stats.speed = speed
    stats.current_health = current_health
    stats.current_mana = current_mana
    return stats
```

**Usage:**
```gdscript
# player.gd
extends CharacterBody2D

@export var stats: CharacterStats

func _ready() -> void:
    if stats:
        # Create runtime copy to avoid modifying the original resource
        stats = stats.duplicate_stats()
```

### Pattern 3: Database Pattern (Array of Resources)

```gdscript
# item_database.gd
extends Resource
class_name ItemDatabase

@export var items: Array[ItemData] = []

func get_item_by_name(item_name: String) -> ItemData:
    for item in items:
        if item.item_name == item_name:
            return item
    return null

func get_items_by_type(item_type: int) -> Array[ItemData]:
    var filtered: Array[ItemData] = []
    for item in items:
        if item.item_type == item_type:
            filtered.append(item)
    return filtered
```

**Create Database:**
1. Create `ItemDatabase` resource
2. Expand `items` array in Inspector
3. Add `ItemData` resources to array
4. Save as `res://data/item_database.tres`

**Usage:**
```gdscript
# Global autoload
const ITEM_DB := preload("res://data/item_database.tres")

func get_item(name: String) -> ItemData:
    return ITEM_DB.get_item_by_name(name)
```

### Pattern 4: Runtime-Only Data (RefCounted)

For data that doesn't need persistence:

```gdscript
# damage_calculation.gd
extends RefCounted
class_name DamageCalculation

var base_damage: int
var critical_hit: bool
var damage_type: String

func calculate_final_damage(target_defense: int) -> int:
    var final_damage := base_damage - target_defense
    if critical_hit:
        final_damage *= 2
    return maxi(final_damage, 1)
```

**Usage:**
```gdscript
var calc := DamageCalculation.new()
calc.base_damage = 50
calc.critical_hit = randf() > 0.8
calc.damage_type = "physical"
var damage := calc.calculate_final_damage(enemy.defense)
```

## Advanced Patterns

### Pattern 5: Nested Resources

```gdscript
# weapon_data.gd
extends ItemData
class_name WeaponData

@export var damage: int = 10
@export var attack_speed: float = 1.0
@export var special_effects: Array[StatusEffect] = []

@export var effect_name: String
@export var duration: float
@export var damage_per_second: int

### Pattern 5b: Bin

Related in Design