Null Variables in Code

Lukas

New member
Hello all,
correct me if Im doing something wrong, but I am debugging this for hours and cannot get this working.

I want to set external behavior tree asset to BehaviorTree via code.
Then after set, I want to retrieve the variables from the tree via GetVariable() APIThe issue us, after I set it, all variables are still null, why?

I also fored to enable the behavior via EnableBehavior() API, but same issue.

1714221821195.png
 
You do not need to enable the behavior tree in order to get the variables. Does the external behavior tree have any variables? Where is newTree coming from? Has it been initialized?
 
You do not need to enable the behavior tree in order to get the variables. Does the external behavior tree have any variables? Where is newTree coming from? Has it been initialized?
Hmm this is something I don't really understand :)
As far as I understand the behavior designer system, ExternalBehaviorTree is an asset, that is shared amongst many behavior tree components that can use it. So I presume if this external behavior tree asset is assigned to a behavior tree component, it internally creates its own instance, with its own variable states for that instance. Otherwise one health variable would be used and overwritten by all the AI using that tree :)

I need to access a variable from that behavior tree instance for that particular component, as we can have like 50 other units running with the same tree. If I accessed variable from external behavior tree asset, I would access the shared variable from asset, not for my instance.


Does the external behavior tree have any variables?
Yes, it has many :) I am retrieving them by their exact name (which is working later in the next frame it seems).

Where is newTree coming from?
It is just a serialized field that has assigned the external behavior asset from the project. This asset has all the variables and nodes for the AI.

Has it been initialized?
Do you mean the external behavior tree? No, I was not aware it should be, I always presumed its just enough to assign the asset to BehaviorTree component (either via code or inspector) and it gets automatically initialized, but I presume I was wrong here.

Should I specifically call something on this asset? I see there is some API, but I thought that is handled internally for ease of use.
 
I just tried to reproduce this and it's even easier than I initially thought. Here's some code and a package that worked:

Code:
        BehaviorTree tree = GetComponent<BehaviorTree>();
        tree.ExternalBehavior = external;
        var value = tree.GetVariable("VariableA");

When I output the value variable it correctly outputs the value of the SharedFloat. Maybe you can compare against this package to see what is different with your setup?
 

Attachments

I just tried to reproduce this and it's even easier than I initially thought. Here's some code and a package that worked:

Code:
        BehaviorTree tree = GetComponent<BehaviorTree>();
        tree.ExternalBehavior = external;
        var value = tree.GetVariable("VariableA");

When I output the value variable it correctly outputs the value of the SharedFloat. Maybe you can compare against this package to see what is different with your setup?

Hmm, it seems something else must have been wrong, when I try it with clean script and behavior, its working. However after some changes in my AI code, its working there as well, no idea however why it wasnt getting those variables before.

For now, we can consider this issue solved, thank you for testing this.
 
Hello @Justin, I finally managed to find the issue where variables are not present, but not sure if this is by design or not, however it caused a lot of troubles investigate that this was the issue :D

When setting a ExternalBehaviorTree from code into BehaviorTree component (StartWhenEnabled is disabled) and then requesting a variable that is found ExternalBehaviorTree subtree via BehaviorTree.GetVariable() API, the variable is not found and is null.

Because it seems like only variables from the main tree asset are present, but not from subtrees yet.

is this a bug? I know trees are not deserialized into this behavior source before they are enabled.started, however I would expect at least variable data be present.

For now, the only way is to create all the variables present in any subtree we have for all our AI, which is quite error prone, as we would need to declare those variables twice (in main BT and subtree BT).

1717422749216.png1717422769749.png
1717422757428.png1717422802595.png
 
This is expected - the variables aren't actually added until the external tree is loaded onto the main tree. The variables will automatically be added to the main tree if you assign the ExternalBehavior reference on the Behavior Tree component, though I know that this is manual work as well.
 
Is there any way we can force load (but not start and execute) the entire tree and subtree after assigning it via code? So all the variables are present.
 
You can enable the behavior tree and then disable the behavior tree immediately. This should consume the variables from the external tree without actually running the logic (though I do think that the very first task will execute).
 
Back
Top