diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/BehaviourTree/Visual Behaviour Tree/SerializationDataContainer.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/BehaviourTree/Visual Behaviour Tree/SerializationDataContainer.cs index 6d3ae9f..8844f5a 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/BehaviourTree/Visual Behaviour Tree/SerializationDataContainer.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/BehaviourTree/Visual Behaviour Tree/SerializationDataContainer.cs @@ -39,7 +39,7 @@ public SerializationEntry(string key, T value) } /// - /// 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. /// /// [Serializable] @@ -56,6 +56,7 @@ public T Get(string key) return entry.Value; } } + throw new KeyNotFoundException(key + " has not been deserialize before."); } @@ -64,9 +65,18 @@ public SerializationEntry Get(int index) return data[index]; } - public void Add(string key, T value) + /// + /// + /// + /// + /// + /// Returns true if key was added, returns false when key was already presents and not added again + public bool Add(string key, T value) { + if (KeyExists(key)) + return false; data.Add(new SerializationEntry(key, value)); + return true; } public void Clear() @@ -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 serializationOrder = new List(); #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 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); @@ -233,34 +243,25 @@ public void Clear() } /// - /// Retrives the key of the item at position index. + /// Retrieves the key of the item at position index. /// /// /// /// 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(), + }; } } } diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/BehaviourTree/Visual Behaviour Tree/TaskSerializer.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/BehaviourTree/Visual Behaviour Tree/TaskSerializer.cs index 988ef36..14c3c6e 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/BehaviourTree/Visual Behaviour Tree/TaskSerializer.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/BehaviourTree/Visual Behaviour Tree/TaskSerializer.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using UnityEditor; using UnityEngine; @@ -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."); } /// @@ -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; } } diff --git a/Assets/Virtual Agents Framework/Samples/BehaviourTree/2. CompositeNodeSample/CompositeBehaviourTreeSample.asset b/Assets/Virtual Agents Framework/Samples/BehaviourTree/2. CompositeNodeSample/CompositeBehaviourTreeSample.asset index 1e12a20..1a08658 100644 --- a/Assets/Virtual Agents Framework/Samples/BehaviourTree/2. CompositeNodeSample/CompositeBehaviourTreeSample.asset +++ b/Assets/Virtual Agents Framework/Samples/BehaviourTree/2. CompositeNodeSample/CompositeBehaviourTreeSample.asset @@ -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} @@ -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} @@ -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} @@ -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} @@ -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} diff --git a/Assets/Virtual Agents Framework/Samples/BehaviourTree/3. DecoratorNode/DecoratorNodeBehaviour Tree Asset.asset b/Assets/Virtual Agents Framework/Samples/BehaviourTree/3. DecoratorNode/DecoratorNodeBehaviour Tree Asset.asset index 11c3fad..e0793ff 100644 --- a/Assets/Virtual Agents Framework/Samples/BehaviourTree/3. DecoratorNode/DecoratorNodeBehaviour Tree Asset.asset +++ b/Assets/Virtual Agents Framework/Samples/BehaviourTree/3. DecoratorNode/DecoratorNodeBehaviour Tree Asset.asset @@ -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} @@ -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} diff --git a/Assets/Virtual Agents Framework/Samples/BehaviourTree/Test.asset b/Assets/Virtual Agents Framework/Samples/BehaviourTree/Test.asset index 0340407..b6e4eb5 100644 --- a/Assets/Virtual Agents Framework/Samples/BehaviourTree/Test.asset +++ b/Assets/Virtual Agents Framework/Samples/BehaviourTree/Test.asset @@ -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: @@ -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: