Skip to content

Commit

Permalink
Serializations of tree nodes now update automatically when new attrib…
Browse files Browse the repository at this point in the history
…utes are added
  • Loading branch information
David624634 committed May 13, 2024
1 parent c4b3280 commit 41f63e4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public SerializationEntry(string key, T value)
}

/// <summary>
/// Pseudo dictonary, that in contrast to actual dictonaries is serializable, but only offers search in linear time.
/// Pseudo dictionary, that in contrast to actual dictionaries is serializable, but only offers search in linear time.
/// </summary>
/// <typeparam name="T"></typeparam>
[Serializable]
Expand All @@ -56,6 +56,7 @@ public T Get(string key)
return entry.Value;
}
}

throw new KeyNotFoundException(key + " has not been deserialize before.");
}

Expand All @@ -64,9 +65,18 @@ public SerializationEntry<T> Get(int index)
return data[index];
}

public void Add(string key, T value)
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns>Returns true if key was added, returns false when key was already presents and not added again</returns>
public bool Add(string key, T value)
{
if (KeyExists(key))
return false;
data.Add(new SerializationEntry<T>(key, value));
return true;
}

public void Clear()
Expand Down Expand Up @@ -120,60 +130,60 @@ public class SerializationDataContainer
[SerializeField] public SerializedListFloats serializedListFloats = new SerializedListFloats();
[SerializeField] public SerializedTrees serializedTrees = new SerializedTrees();

//Saves the order in which the data was serialized. Allows coustom inspectors to replicate that order.
//Saves the order in which the data was serialized. Allows custom inspectors to replicate that order.
[SerializeField] public List<SerializableType> serializationOrder = new List<SerializableType>();

#region Overloads for adding data to the serialization
public void AddSerializedData(string key, Vector3 value)
{
serializationOrder.Add(SerializableType.VECTOR3);
serializedVectors.Add(key, value);
if(serializedVectors.Add(key, value))
serializationOrder.Add(SerializableType.VECTOR3);
}

public void AddSerializedData(string key, float value)
{
serializationOrder.Add(SerializableType.FLOAT);
serializedFloats.Add(key, value);
if(serializedFloats.Add(key, value))
serializationOrder.Add(SerializableType.FLOAT);
}

public void AddSerializedData(string key, string value)
{
serializationOrder.Add(SerializableType.STRING);
serializedStrings.Add(key, value);
if(serializedStrings.Add(key, value))
serializationOrder.Add(SerializableType.STRING);
}

public void AddSerializedData(string key, int value)
{
serializationOrder.Add(SerializableType.INT);
serializedInts.Add(key, value);
if(serializedInts.Add(key, value))
serializationOrder.Add(SerializableType.INT);
}

public void AddSerializedData(string key, GameObject value)
{
serializationOrder.Add(SerializableType.GAMEOBJECT);
serializedGameobjects.Add(key,value);
if(serializedGameobjects.Add(key, value))
serializationOrder.Add(SerializableType.GAMEOBJECT);
}

public void AddSerializedData(string key, bool value)
{
serializationOrder.Add(SerializableType.BOOL);
serializedBools.Add(key, value);
if(serializedBools.Add(key, value))
serializationOrder.Add(SerializableType.BOOL);
}

public void AddSerializedData(string key, List<float> value)
{
serializationOrder.Add(SerializableType.LIST_FLOAT);
serializedListFloats.Add(key, value);
if(serializedListFloats.Add(key, value))
serializationOrder.Add(SerializableType.LIST_FLOAT);
}

public void AddSerializedData(string key, BehaviourTreeAsset value)
{
serializationOrder.Add(SerializableType.TREE);
serializedTrees.Add(key, value);
if(serializedTrees.Add(key, value))
serializationOrder.Add(SerializableType.TREE);
}
#endregion

#region Overloads for retriving serialized data
#region Overloads for retrieving serialized data
public Vector3 GetSerializedVector(string key)
{
return serializedVectors.Get(key);
Expand Down Expand Up @@ -233,34 +243,25 @@ public void Clear()
}

/// <summary>
/// Retrives the key of the item at position index.
/// Retrieves the key of the item at position index.
/// </summary>
/// <param name="index"></param>
/// <param name="type"></param>
/// <returns></returns>
public string GetKeyByIndex(int index, SerializableType type)
{
switch (type)
return type switch
{
case SerializableType.VECTOR3:
return serializedVectors.Get(index).Key;
case SerializableType.FLOAT:
return serializedFloats.Get(index).Key;
case SerializableType.STRING:
return serializedStrings.Get(index).Key;
case SerializableType.INT:
return serializedInts.Get(index).Key;
case SerializableType.GAMEOBJECT:
return serializedGameobjects.Get(index).Key;
case SerializableType.BOOL:
return serializedBools.Get(index).Key;
case SerializableType.LIST_FLOAT:
return serializedListFloats.Get(index).Key;
case SerializableType.TREE:
return serializedTrees.Get(index).Key;
default:
throw new NotImplementedException();
}
SerializableType.VECTOR3 => serializedVectors.Get(index).Key,
SerializableType.FLOAT => serializedFloats.Get(index).Key,
SerializableType.STRING => serializedStrings.Get(index).Key,
SerializableType.INT => serializedInts.Get(index).Key,
SerializableType.GAMEOBJECT => serializedGameobjects.Get(index).Key,
SerializableType.BOOL => serializedBools.Get(index).Key,
SerializableType.LIST_FLOAT => serializedListFloats.Get(index).Key,
SerializableType.TREE => serializedTrees.Get(index).Key,
_ => throw new NotImplementedException(),
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

Expand Down Expand Up @@ -57,8 +58,7 @@ private ISerializable DeserializeType()
}
}
#endif

return null;
throw new Exception("Type " + serializedObjectType + " could not be found. It is likely that the name of a task in the tree was changed. Please update the name accordingly in the asset file of that tree.");
}

/// <summary>
Expand All @@ -68,7 +68,17 @@ private ISerializable DeserializeType()
public ISerializable GetCopyOfSerializedInterface(SerializationDataContainer overwriteData = null)
{
ISerializable copy = DeserializeType();
copy.Deserialize(overwriteData != null ? overwriteData : Data);
try
{
copy.Deserialize(overwriteData != null ? overwriteData : Data);
}
catch (KeyNotFoundException e)
{
Debug.LogWarning("One node seems to have added new attributes since the tree got saved last. Node will now be updated. Please save the tree again. Error: " + e + " ");
copy.Serialize(overwriteData != null ? overwriteData : Data);
copy.Deserialize(overwriteData != null ? overwriteData : Data);
}

return copy;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ MonoBehaviour:
serializedGameobjects:
data: []
serializedBools:
data: []
data:
- Key: fail
Value: 0
serializedListFloats:
data: []
serializedTrees:
data: []
serializationOrder: 02000000
serializationOrder: 0200000005000000
serializedObjectType: i5.VirtualAgents.AgentTasks.DebugTask
Guid: e948eb5f3ab23e241baeaf266f9f1620
Position: {x: 345.00012, y: 333.00003}
Expand Down Expand Up @@ -174,12 +176,14 @@ MonoBehaviour:
serializedGameobjects:
data: []
serializedBools:
data: []
data:
- Key: fail
Value: 0
serializedListFloats:
data: []
serializedTrees:
data: []
serializationOrder: 02000000
serializationOrder: 0200000005000000
serializedObjectType: i5.VirtualAgents.AgentTasks.DebugTask
Guid: cc0944ccf4ddeb048a4f0f801e8c585f
Position: {x: 755, y: 333}
Expand Down Expand Up @@ -294,12 +298,14 @@ MonoBehaviour:
serializedGameobjects:
data: []
serializedBools:
data: []
data:
- Key: fail
Value: 0
serializedListFloats:
data: []
serializedTrees:
data: []
serializationOrder: 02000000
serializationOrder: 0200000005000000
serializedObjectType: i5.VirtualAgents.AgentTasks.DebugTask
Guid: 186e31cf253db4045843f701482d8c67
Position: {x: -916.2627, y: 412}
Expand Down Expand Up @@ -441,12 +447,14 @@ MonoBehaviour:
serializedGameobjects:
data: []
serializedBools:
data: []
data:
- Key: fail
Value: 0
serializedListFloats:
data: []
serializedTrees:
data: []
serializationOrder: 02000000
serializationOrder: 0200000005000000
serializedObjectType: i5.VirtualAgents.AgentTasks.DebugTask
Guid: 29c5b279cb1e1d145b8da8e835ce1aea
Position: {x: 549.7092, y: 333}
Expand Down Expand Up @@ -640,12 +648,14 @@ MonoBehaviour:
serializedGameobjects:
data: []
serializedBools:
data: []
data:
- Key: fail
Value: 0
serializedListFloats:
data: []
serializedTrees:
data: []
serializationOrder: 02000000
serializationOrder: 0200000005000000
serializedObjectType: i5.VirtualAgents.AgentTasks.DebugTask
Guid: a2a78e0b347482344babd741d49447cf
Position: {x: -443.49994, y: 411.99994}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ MonoBehaviour:
serializedGameobjects:
data: []
serializedBools:
data: []
data:
- Key: fail
Value: 0
serializedListFloats:
data: []
serializedTrees:
data: []
serializationOrder: 02000000
serializationOrder: 0200000005000000
serializedObjectType: i5.VirtualAgents.AgentTasks.DebugTask
Guid: f9548b7358c63d746b570bc9059a65be
Position: {x: 389.4791, y: 556}
Expand Down Expand Up @@ -400,12 +402,14 @@ MonoBehaviour:
serializedGameobjects:
data: []
serializedBools:
data: []
data:
- Key: fail
Value: 0
serializedListFloats:
data: []
serializedTrees:
data: []
serializationOrder: 02000000
serializationOrder: 0200000005000000
serializedObjectType: i5.VirtualAgents.AgentTasks.DebugTask
Guid: b86e5564b7d0ce441966c77db5303500
Position: {x: -24.00001, y: 261}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ MonoBehaviour:
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 273b94479bfe09f49a9b751d7f675d69, type: 3}
m_Name: BehaviorTreeTask
m_Name: BehaviourTreeTask
m_EditorClassIdentifier:
Data:
serializedVectors:
Expand All @@ -68,7 +68,7 @@ MonoBehaviour:
- Key: Tree
Value: {fileID: 0}
serializationOrder: 07000000
serializedObjectType: i5.VirtualAgents.AgentTasks.BehaviorTreeTask
serializedObjectType: i5.VirtualAgents.AgentTasks.BehaviourTreeTask
Guid: ba68094b98afa5d4aa50f61eabba2df7
Position: {x: 157, y: 767}
Description:
Expand Down

0 comments on commit 41f63e4

Please sign in to comment.