Save load serialization

From nlelouche/Unity-SkillForge · View interactive version

Save load serialization is an AI agent skill from the GitHub repository nlelouche/Unity-SkillForge. Complete save/load system with ISaveable interface, JSON serialization, save slots, and encryption support.

How to use this skill

Skill file

Save Load Serialization

Overview

Complete save/load system using ISaveable interface pattern. Automatically discovers saveable objects, serializes to JSON, supports multiple save slots, and optional encryption.

When to Use

Architecture

┌─────────────────────────────────────────────────────────────┐
│                     SaveManager                             │
│              (Central Controller)                           │
├─────────────────────────────────────────────────────────────┤
│  Save() / Load() / SaveToSlot(n) / LoadFromSlot(n)          │
│  FindAllSaveables() → Collect ISaveable objects             │
│  Serialize to JSON → Write to persistentDataPath            │
└─────────────────────────────────────────────────────────────┘
                              ↓
           ┌──────────────────┼──────────────────┐
           ↓                  ↓                  ↓
   ┌───────────────┐  ┌───────────────┐  ┌───────────────┐
   │ PlayerStats   │  │   Inventory   │  │   QuestLog    │
   │ : ISaveable   │  │  : ISaveable  │  │  : ISaveable  │
   └───────────────┘  └───────────────┘  └───────────────┘
         ↓                    ↓                    ↓
   CaptureState()      CaptureState()      CaptureState()
   RestoreState()      RestoreState()      RestoreState()

Components

| Component | Purpose |

|-----------|---------|

| ISaveable | Interface for saveable objects |

| SaveManager | Central save/load controller |

| SaveData | Container for all save states |

ISaveable Implementation

public class PlayerStats : MonoBehaviour, ISaveable
{
    public string SaveId => "player_stats";
    public int LoadPriority => 0; // Load first
    
    [Serializable]
    private struct SaveState
    {
        public int Level;
        public float Experience;
        public int Gold;
    }
    
    public object CaptureState()
    {
        return new SaveState 
        { 
            Level = _level, 
            Experience = _experience,
            Gold = _gold
        };
    }
    
    public void RestoreState(object state)
    {
        var data = (SaveState)state;
        _level = data.Level;
        _experience = data.Experience;
        _gold = data.Gold;
    }
}

Best Practices

Few-Shot Examples

Example 1: Save Game

User: "Save the game when player presses F5."

Agent:

if (Input.GetKeyDown(KeyCode.F5))
{
    SaveManager.Instance.Save();
    ShowNotification("Game Saved!");
}

Example 2: Multiple Slots

User: "Implement 3 save slots."

Agent:

public void ShowSaveSlots()
{
    for (int i = 0; i < 3; i++)
    {
        bool hasSave = SaveManager.Instance.HasSave(i);
        // Show slot UI with save info
    }
}

Example 3: Autosave

User: "Autosave every 5 minutes."

Agent:

IEnumerator AutosaveLoop()
{
    while (true)
    {
        yield return new WaitForSeconds(300f);
        SaveManager.Instance.SaveToSlot(99); // Autosave slot
    }
}

TDD Contract

⚠️ Legacy Skill — Refactor Pending

Este skill NO tiene tests automatizados aún. El siguiente boilerplate es un punto de partida.

// Escribe estos tests ANTES de implementar:

// Test 1: should [expected behavior] when [condition]
[Test]
public void SaveLoadSerialization_Should{ExpectedBehavior}_When{Condition}()
{{
    // Arrange
    // TODO: Setup test fixtures
    
    // Act
    // TODO: Execute system under test
    
    // Assert
    Assert.Fail("Not implemented — write test first");
}}

// Test 2: should handle [edge case]
[Test]
public void SaveLoadSerialization_ShouldHandle{EdgeCase}()
{{
    // Arrange
    // TODO: Setup edge case scenario
    
    // Act
    // TODO: Execute
    
    // Assert
    Assert.Fail("Not implemented");
}}

// Test 3: should throw when [invalid input]
[Test]
public void SaveLoadSerialization_ShouldThrow_When{InvalidInput}()
{{
    // Arrange
    var invalidInput = default;
    
    // Act & Assert
    Assert.Throws<Exception>(() => {{ /* execute */ }});
}}

Pasos para completar el TDD:

  1. Descomenta los tests above
  2. Implementa la funcionalidad mínima para que compile
  3. Ejecuta los tests — deben fallar (RED)
  4. Implementa la funcionalidad real
  5. Verifica que los tests pasen (GREEN)
  6. Refactorea manteniendo los tests verdes

Nota: Este skill fue marcado como tdd_first: false durante la auditoría v2.0.1. La sección TDD fue agregada automáticamente pero requiere customización manual para reflejar el comportamiento real del skill.

Related Skills

Template Files

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.