Duplicate Dictionary Key Exception in External Behavior Tree

red_halo

New member
I'm using Behavior Trees v1.7.7

I seem to trigger this exception when using dynamic variables, but reproducing it is difficult. It occurs during the course of normal usage, and makes the external tree this is happening to unusable. Good thing it happens rarely, but when it does I end up losing work on that particular tree since there's no straightforward way to resolve it. I usually reset the asset to a previous version using git, and sometimes that means losing hours of careful tuning.

1709815418872.png


It happened again today, so I decided to take a look at the internal contents of the behavior tree's .asset file, and formatted the JSON I found within to see if I could track down the problem.

1709815989079.png

I searched for the key specified in the exception and found an element toward the end of the JSON block that seemed to be cause. Notice the last two identical elements defining waitDistance. Decided to remove the last entry from the original non-formatted JSON block pictured above, and I was able to resolve the issue and recover the tree without losing work.

JSON:
"Variables": [
    {
      "Type": "SharedNPC",
      "Name": "sharedNPC",
      "IsShared": true
    },
    {
      "Type": "SharedSightSensor",
      "Name": "sharedSightSensor",
      "IsShared": true
    },
    {
      "Type": "BehaviorDesigner.Runtime.SharedVector3",
      "Name": "coverPoint",
      "IsShared": true,
      "IsDynamic": true,
      "Vector3mValue": "(0,0,0)"
    },
    {
      "Type": "BehaviorDesigner.Runtime.SharedFloat",
      "Name": "waitDistance",
      "IsShared": true,
      "IsDynamic": true,
      "SinglemValue": 0
    },
    {
      "Type": "BehaviorDesigner.Runtime.SharedFloat",
      "Name": "waitDistance",
      "IsShared": true,
      "IsDynamic": true,
      "SinglemValue": 0
    }
  ]

I may be off-base since I don't know how the internals work, but perhaps detecting duplicate entries and removing them would be a good solution. Below is the full stack trace of the issue in case that's helpful.

Code:
ArgumentException: An item with the same key has already been added. Key: waitDistance
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
BehaviorDesigner.Runtime.BehaviorSource.UpdateVariablesIndex () (at <4a24131e73ec49f7805f3dfb6a69fa78>:0)
BehaviorDesigner.Runtime.BehaviorSource.SetAllVariables (System.Collections.Generic.List`1[T] variables) (at <4a24131e73ec49f7805f3dfb6a69fa78>:0)
BehaviorDesigner.Runtime.JSONDeserialization.DeserializeVariables (BehaviorDesigner.Runtime.IVariableSource variableSource, System.Collections.Generic.Dictionary`2[TKey,TValue] dict, System.Collections.Generic.List`1[T] unityObjects) (at <4a24131e73ec49f7805f3dfb6a69fa78>:0)
BehaviorDesigner.Runtime.JSONDeserialization.Load (BehaviorDesigner.Runtime.TaskSerializationData taskData, BehaviorDesigner.Runtime.BehaviorSource behaviorSource, System.Boolean loadTasks) (at <4a24131e73ec49f7805f3dfb6a69fa78>:0)
BehaviorDesigner.Runtime.BehaviorSource.CheckForSerialization (System.Boolean force, BehaviorDesigner.Runtime.BehaviorSource behaviorSource, System.Boolean isPlaying) (at <4a24131e73ec49f7805f3dfb6a69fa78>:0)
BehaviorDesigner.Editor.GraphDesigner.Load (BehaviorDesigner.Runtime.BehaviorSource behaviorSource, System.Boolean loadPrevBehavior, UnityEngine.Vector2 nodePosition) (at <d6203e70841f4bfeb69e5def378a2bc0>:0)
BehaviorDesigner.Editor.BehaviorDesignerWindow.LoadBehavior (BehaviorDesigner.Runtime.BehaviorSource behaviorSource, System.Boolean loadPrevBehavior, System.Boolean inspectorLoad) (at <d6203e70841f4bfeb69e5def378a2bc0>:0)
BehaviorDesigner.Editor.BehaviorDesignerWindow.ReloadPreviousBehavior () (at <d6203e70841f4bfeb69e5def378a2bc0>:0)
BehaviorDesigner.Editor.BehaviorDesignerWindow.OnFocus () (at <d6203e70841f4bfeb69e5def378a2bc0>:0)
UnityEditor.HostView.OnFocus () (at <1f0be198f5164d2489de92f22c998266>:0)
 
Thanks, I'd love to figure out why the duplicate entry is being added but I'll do a check to ignore any duplicates when loading the tree.
 
Noticed that this happened right after an undo command (Ctrl+Z). The whole BD editor window looked like it crashed. Manually setting the ID to the next increment recovered the file. Maybe it would be worth pushing an update that reports this issue in the console and recovers by incrementing the ID or skipping importing duplicates. Not the cleanest, but saves the scare of losing "all progress".
 
Are you running version 1.7.9 of Behavior Designer? It can no longer insert duplicate keys as indicated by the stack trace from the original post. Or do you have a different stack trace?
 
Back
Top