Multiple duplicated key exceptions when changing external behavior tree with asynchronous load

Kichang Kim

New member
When I set external behavior trees in runtime (about ~100 objects in single frame), multiple exceptions are occurred like this:

ArgumentException: An item with the same key has already been added. Key: 1908045651
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <695d1cc93cca45069c528c15c9fdd749>:0)
BehaviorDesigner.Runtime.JSONDeserialization.Load (BehaviorDesigner.Runtime.TaskSerializationData taskData, BehaviorDesigner.Runtime.BehaviorSource behaviorSource) (at Assets/Behavior Designer/Runtime/JSONDeserialization.cs:36)
BehaviorDesigner.Runtime.BehaviorSource.CheckForSerialization (System.Boolean force, BehaviorDesigner.Runtime.BehaviorSource behaviorSource) (at Assets/Behavior Designer/Runtime/BehaviorSource.cs:74)
BehaviorDesigner.Runtime.Behavior.CheckForSerialization (System.Boolean forceSerialization) (at Assets/Behavior Designer/Runtime/Behavior.cs:288)
BehaviorDesigner.Runtime.Behavior.CheckForSerialization () (at Assets/Behavior Designer/Runtime/Behavior.cs:274)
BehaviorDesigner.Runtime.Behavior.GetVariable (System.String name) (at Assets/Behavior Designer/Runtime/Behavior.cs:218)

ArgumentException: An item with the same key has already been added. Key: AAAAAAA - Behavior
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <695d1cc93cca45069c528c15c9fdd749>:0)
BehaviorDesigner.Runtime.BehaviorManager.LoadBehaviorComplete (BehaviorDesigner.Runtime.Behavior behavior, BehaviorDesigner.Runtime.BehaviorManager+BehaviorTree behaviorTree) (at Assets/Behavior Designer/Runtime/BehaviorManager.cs:473)
BehaviorDesigner.Runtime.BehaviorManager+<CheckThreadLoaders>d__75.MoveNext () (at Assets/Behavior Designer/Runtime/BehaviorManager.cs:364)

It seems that the internal caching system is not thread safe.
 
How are you setting the external trees? Can you send me a repro scene so I can take a look at it?
 
It is hard to create repro project, but here is a part of my code: (removed redundant part)

C#:
for (int i = 0; i < this.Count; i++)
{
    var instance = PrefabSystem.I.Spawn(this.PrefabName);
    var behaviorTree = instance.GetComponent<BehaviorDesigner.Runtime.BehaviorTree>();

    if (behaviorTree != null)
    {
        if (AI.AIInterfaceSystem.Exists && AI.AIInterfaceSystem.I.TryGetExternalBehaviorTree(this.BehaviorTreeName, out var externalBehaviorTree))
        {
            behaviorTree.ExternalBehavior = externalBehaviorTree;
        }

        behaviorTree.EnableBehavior();
    }
}

Prefab and ExternalBehaviorTree are pre-loaded from assetbundles.
 
If you are able to send a repro scene that would be great. I just tried to reproduce this error but wasn't able to.
 
Hmm, strange. When I tried to create repro, it does not occurred.. Maybe some part of my script makes issue. If I found more precise condition, I'll report you.
 
Top