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

  • repro.unitypackage
    3.7 KB · Views: 0
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.
 
Top