Skip to content

SlashSave

Roll4d4 edited this page Feb 23, 2025 · 1 revision

๐Ÿ’พ SLASHsavekey: Experimental Key Saving & Loading for KLEP

The SLASHsavekey is an experimental feature in the KLEP system that allows keys to be saved and loaded to/from persistent storage. This functionality is not required for KLEP to function, but it demonstrates the flexibility of KLEP keys and their potential use cases, such as persistent game states, memory systems, or dynamic reconfiguration.


๐Ÿšฆ What Does SLASHsavekey Do?

  1. Save & Load KLEP Keys:

    • Supports two formats: XML and Binary.
    • Provides serialization and deserialization of KLEP keys into persistent files.
  2. Serialization of Complex Data:

    • Converts KLEPKey objects into serializable formats.
    • Supports simple types (e.g., int, bool) and complex data (e.g., transforms, lists).
  3. Persistence Across Sessions:

    • Allows AI agents to retain state between game sessions, levels, or application restarts.

๐Ÿ” Core Components of SLASHsavekey:


๐Ÿ’พ 1. Initialization (Awake)

private void Awake()
{
    if (Instance == null)
    {
        Instance = this;
        DontDestroyOnLoad(gameObject); // Keep this manager persistent across scenes
        saveDirectory = Path.Combine(Application.persistentDataPath, "KLEPKeys");
        if (!Directory.Exists(saveDirectory))
        {
            Directory.CreateDirectory(saveDirectory);
        }
    }
    else
    {
        Destroy(gameObject);
    }
}
  • Singleton Pattern: Ensures only one instance of SLASHsavekey exists.
  • Persistent Data Path: Creates a "KLEPKeys" directory under Unity's persistent data path, which is safe for all platforms.
  • Optional Persistence: DontDestroyOnLoad keeps this manager active across scene loads, making saved keys available globally.

๐Ÿง  2. Saving a Key (SaveKey)

public void SaveKey(string keyName, KLEPKey key)
{
    SerializableKLEPKey serializableKey = key.ToSerializable();
    string filePath = Path.Combine(saveDirectory, keyName + (saveFormat == SaveFormat.XML ? ".xml" : ".bin"));

    switch (saveFormat)
    {
        case SaveFormat.XML:
            SaveToXML(serializableKey, filePath);
            break;
        case SaveFormat.Binary:
            SaveToBinary(serializableKey, filePath);
            break;
    }

    Debug.Log("Key saved to " + filePath);
}
  • Converts a KLEPKey into a serializable format (SerializableKLEPKey).
  • Saves the key using either XML or Binary format, based on selected setting.
  • Supports flexibility by offering different save formats:
    • XML: Human-readable, good for debugging and manual editing.
    • Binary: More compact and potentially faster, but not human-readable.

๐Ÿ”„ 3. Loading a Key (LoadKey)

public KLEPKey LoadKey(string keyName, SLASHkeyLoader keyLoader)
{
    string filePath = Path.Combine(saveDirectory, keyName + (saveFormat == SaveFormat.XML ? ".xml" : ".bin"));
    if (!File.Exists(filePath))
    {
        Debug.LogError("Save file not found for key: " + keyName);
        return null;
    }

    SerializableKLEPKey serializableKey = null;
    switch (saveFormat)
    {
        case SaveFormat.XML:
            serializableKey = LoadFromXML(filePath);
            break;
        case SaveFormat.Binary:
            serializableKey = LoadFromBinary(filePath);
            break;
    }

    if (serializableKey != null)
    {
        return serializableKey.ToKLEPKey(keyLoader);
    }

    return null;
}
  • Checks if a save file exists for the specified key name.
  • Loads the key data using the appropriate format (XML or Binary).
  • Converts the serialized data back into a KLEPKey, ready for use in the neuron.
  • Use Case:
    • Reloading stateful data, such as inventory keys, environmental states, or persistent AI memory.

๐Ÿ’พ 4. XML & Binary Serialization:

๐Ÿง  XML Serialization:

private void SaveToXML(SerializableKLEPKey keyData, string filePath)
{
    XmlSerializer serializer = new XmlSerializer(typeof(SerializableKLEPKey));
    using (StreamWriter writer = new StreamWriter(filePath))
    {
        serializer.Serialize(writer, keyData);
    }
}

private SerializableKLEPKey LoadFromXML(string filePath)
{
    XmlSerializer serializer = new XmlSerializer(typeof(SerializableKLEPKey));
    using (StreamReader reader = new StreamReader(filePath))
    {
        return (SerializableKLEPKey)serializer.Deserialize(reader);
    }
}
  • XML is human-readable, allowing manual inspection and editing of saved key data.
  • Useful for debugging or modifying values outside the application.

๐Ÿšฆ Binary Serialization:

private void SaveToBinary(SerializableKLEPKey keyData, string filePath)
{
    using (FileStream fs = new FileStream(filePath, FileMode.Create))
    {
        BinaryWriter writer = new BinaryWriter(fs);
        writer.Write(JsonUtility.ToJson(keyData));
    }
}

private SerializableKLEPKey LoadFromBinary(string filePath)
{
    using (FileStream fs = new FileStream(filePath, FileMode.Open))
    {
        BinaryReader reader = new BinaryReader(fs);
        return JsonUtility.FromJson<SerializableKLEPKey>(reader.ReadString());
    }
}
  • Binary format is more compact and suitable for performance-critical applications.
  • Not human-readable, but faster to load and save.

๐Ÿ’ก Use Cases for Key Persistence:

  1. Save AI State:

    • Preserve agent state, allowing complex behaviors to persist between sessions.
  2. Dynamic Memory Systems:

    • Enable AI to "remember" information over time, creating more lifelike behaviors.
  3. Game Persistence:

    • Store inventory items, environmental changes, or player choices using KLEP keys.
  4. Modular Development:

    • Pre-build key configurations that can be loaded dynamically, allowing designers to modify behavior without recompiling code.

โœ… Key Takeaways:

  • SLASHsavekey is an optional tool for enhancing KLEP's flexibility, not a required component.
  • Demonstrates the modularity of KLEP keys, showing that they can exist beyond the immediate runtime.
  • Supports both XML and Binary formats, offering flexibility in data management.
  • Allows for innovative use cases, such as persistent AI memory and game state management.

๐Ÿง  How This Fits Into KLEP:

  • Works alongside the SLASHkeyLoader, allowing keys to be loaded with predefined properties.
  • Supports AI agents that can "learn" over time, maintaining memories across sessions.
  • Experimental status: While functional, this feature is not yet fully integrated into the core KLEP system. Contributors are welcome to explore and expand this concept!

Would you like help integrating this feature into a specific use case, such as saving AI goals or restoring environmental states in KLEP? ๐Ÿ˜Š

KLEP Wiki Navigation

๐Ÿš€ Getting Started

๐Ÿง  Core Concepts

๐ŸŽฎ Examples and Tutorials

๐Ÿ› ๏ธ Scripts and Components

๐Ÿ’ก Experimental/Project Projections

๐Ÿงฌ Contributing

Clone this wiki locally