open-behavior-trees icon indicating copy to clipboard operation
open-behavior-trees copied to clipboard

NullReferenceException whenever trying to save a blackboard in Unity 2023

Open MishterKirby opened this issue 1 year ago • 6 comments

When using Open Behavior Trees under Unity 2023.3.0b2, the following output gets generated when clicking save on a behavior tree blackboard

NullReferenceException: Object reference not set to an instance of an object
OpenBehaviorTrees.BehaviorTreeEditorUtilities.GenerateAssetCopy (OpenBehaviorTrees.BehaviorTreeNode currentNode, OpenBehaviorTrees.BehaviorTreeNode parent, System.Collections.Generic.Queue`1[T] order) (at ./Library/PackageCache/com.sterberino.open-behavior-trees/Editor/BehaviorTreeEditorUtilities.cs:231)
OpenBehaviorTrees.BehaviorTreeEditorUtilities.GenerateAssetCopy (OpenBehaviorTrees.BehaviorTreeNode currentNode, OpenBehaviorTrees.BehaviorTreeNode parent, System.Collections.Generic.Queue`1[T] order) (at ./Library/PackageCache/com.sterberino.open-behavior-trees/Editor/BehaviorTreeEditorUtilities.cs:215)
OpenBehaviorTrees.BehaviorTreeEditorUtilities.GenerateAssetCopy (OpenBehaviorTrees.BehaviorTreeNode currentNode, OpenBehaviorTrees.BehaviorTreeNode parent, System.Collections.Generic.Queue`1[T] order) (at ./Library/PackageCache/com.sterberino.open-behavior-trees/Editor/BehaviorTreeEditorUtilities.cs:224)
OpenBehaviorTrees.BehaviorTreeEditorUtilities.GenerateTreeFromNode (OpenBehaviorTrees.BehaviorTreeNode node) (at ./Library/PackageCache/com.sterberino.open-behavior-trees/Editor/BehaviorTreeEditorUtilities.cs:110)
OpenBehaviorTrees.BehaviorTreeEditorWindow.Save () (at ./Library/PackageCache/com.sterberino.open-behavior-trees/Editor/BehaviorTreeEditorWindow.cs:1071)
OpenBehaviorTrees.BehaviorTreeWindowHeader.Render (UnityEngine.Rect position, System.Single sidebarWidth) (at ./Library/PackageCache/com.sterberino.open-behavior-trees/Editor/BehaviorTreeWindowHeader.cs:63)
OpenBehaviorTrees.BehaviorTreeEditorWindow.OnGUI () (at ./Library/PackageCache/com.sterberino.open-behavior-trees/Editor/BehaviorTreeEditorWindow.cs:235)

MishterKirby avatar Mar 03 '24 22:03 MishterKirby

I think that this issue is due to renaming the root node. It tries to get a reference to the existing tree using the name of the currently edited tree (which is a copy) via the root node and throws a null reference exception, I will try to fix it as soon as possible (probably tomorrow).

Sterberino avatar Mar 04 '24 03:03 Sterberino

Just did some further testing in an isolated environment and discovered that this is caused by having unattached nodes on the blackboard.

I personally feel those should be preserved even if they're not being used at the moment, but I'm not sure if what I'm currently experiencing is intended functionality or not.

MishterKirby avatar Mar 13 '24 18:03 MishterKirby

Just did some further testing in an isolated environment and discovered that this is caused by having unattached nodes on the blackboard.

I personally feel those should be preserved even if they're not being used at the moment, but I'm not sure if what I'm currently experiencing is intended functionality or not.

Sorry for the inconvenience, and that I haven't gotten to this yet. I'm in the middle of finals in my last quarter, actually.

When you say unattached nodes, are you referring to instances of BehaviorTreeNode? Because the tree is comprised of runtime copies of the existing ScriptableObject asset nodes. If you are trying to put nodes in the blackboard tree in the inspector, that would not work. You should instead insert the node using the OnInit() or OnStart() methods in BehaviorTreeNode.

Sterberino avatar Mar 14 '24 00:03 Sterberino

That's okay, please don't rush. Hope your finals go well.

As for unattached nodes, I mean nodes in the Node Editor. If you don't connect nodes together, as shown in this screenshot, you get the NullReferenceException error. image

My expectation coming in was that I should be able to save the blackboard with such nodes still not hooked up.

MishterKirby avatar Mar 14 '24 03:03 MishterKirby

That's okay, please don't rush. Hope your finals go well.

As for unattached nodes, I mean nodes in the Node Editor. If you don't connect nodes together, as shown in this screenshot, you get the NullReferenceException error. image

My expectation coming in was that I should be able to save the blackboard with such nodes still not hooked up.

Oh yes, this is because the behavior tree you are working on in the node editor is a copy of the behavior tree asset (the root node). Those nodes can't have null parents, because they wouldn't be part of the tree. I can add a notification when trying to save a tree with unconnected nodes, but saving the tree without connecting the nodes wouldn't work as it currently stands. I would have to rework the implementation to something that uses explicit edges, I think.

Sterberino avatar Mar 20 '24 18:03 Sterberino

Oh, I see, that makes sense now. Thank you for the explanation. I agree adding a notification would make the editor a bit more user friendly to use, and also would most likely be easier to implement, too.

On another note, should I leave this issue open for future reference, or can it be closed?

MishterKirby avatar Mar 21 '24 19:03 MishterKirby