-
Notifications
You must be signed in to change notification settings - Fork 0
SlashSave
Roll4d4 edited this page Feb 23, 2025
·
1 revision
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.
-
Save & Load KLEP Keys:
- Supports two formats: XML and Binary.
- Provides serialization and deserialization of KLEP keys into persistent files.
-
Serialization of Complex Data:
- Converts KLEPKey objects into serializable formats.
- Supports simple types (e.g., int, bool) and complex data (e.g., transforms, lists).
-
Persistence Across Sessions:
- Allows AI agents to retain state between game sessions, levels, or application restarts.
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.
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
KLEPKeyinto 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.
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.
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.
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.
-
Save AI State:
- Preserve agent state, allowing complex behaviors to persist between sessions.
-
Dynamic Memory Systems:
- Enable AI to "remember" information over time, creating more lifelike behaviors.
-
Game Persistence:
- Store inventory items, environmental changes, or player choices using KLEP keys.
-
Modular Development:
- Pre-build key configurations that can be loaded dynamically, allowing designers to modify behavior without recompiling code.
- 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.
-
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? ๐