A few questions about Variables

ShadowZie

Member
Hey, I am designing a new tree that multiple agents will use

I was planning to use the external behavior tree
and then creating the main AI script that will store health death etc and then the behavior tree will update those variables/properties
and to run the behavior tree not on every frame but like every 0.4 - 0.2 seconds

now
I see there are 3 ways to access the behavior tree

GetVariable, variable mapping, Variable Synchronizer

what is the use case for each one of them? (Like how should I decide which one I want to use)

And what is the optimized way to do it? (with non-behavior tree components) If I have a lot of agents in the scene like 10 - 20 or more
.
If I am correct the best-optimized way is variable mapping with scripts that are existing on the same agent and GetVariable on other scripts that exist somewhere else


Thank you :)
 
Last edited:
For your own scripts you should use GetVariable and cache the result.

If you want to sync values within the behavior tree you should use variable mappings. The variable synchronizer will be deprecated in favor of variable mappings.
 
For your own scripts you should use GetVariable and cache the result.

If you want to sync values within the behavior tree you should use variable mappings. The variable synchronizer will be deprecated in favor of variable mappings.
Thank you,

About the movement pack, I saw that there is a search action but how can I choose what target I want to search for? I don't see any option to do a tag or something like that

And is there any performance difference between the external behavior tree and vs regular tree?
And If I want to switch from external to the regular tree how can I import the tree into it?

And the last thing I see that variable mapping won't work with external behavior what should I do to replace it? if i want to get info from the script on the ai

thank you
 
Last edited:
If you can create a new thread for each topic that would make searching easier for others.

About the movement pack, I saw that there is a search action but how can I choose what target I want to search for? I don't see any option to do a tag or something like that
The Search task uses a layer mask to determine the search targets.

And is there any performance difference between the external behavior tree and vs regular tree?
And If I want to switch from external to the regular tree how can I import the tree into it?
There isn't a performance difference. On the top of the editor you can save a tree as an external tree.

And the last thing I see that variable mapping won't work with external behavior what should I do to replace it? if i want to get info from the script on the ai
You will need to do something like this:

 
If you can create a new thread for each topic that would make searching easier for others.


The Search task uses a layer mask to determine the search targets.


There isn't a performance difference. On the top of the editor you can save a tree as an external tree.


You will need to do something like this:

Thank you, I will check it out,
If I want how can I convert the external tree to just regular tree that is on the player? so I can use the valuable mapping? (with there valuables inside it)
 
If you can create a new thread for each topic that would make searching easier for others.


The Search task uses a layer mask to determine the search targets.


There isn't a performance difference. On the top of the editor you can save a tree as an external tree.


You will need to do something like this:

I checked the post
But there I am unsure what to use

there is the one solution

Code:
public static void SetPropertyMapping(this BehaviorTree tree, string treeVariableName, string mapping, GameObject owner)
{
var sharedVariable = tree.GetVariable(treeVariableName);
sharedVariable.PropertyMapping = mapping;
sharedVariable.PropertyMappingOwner = owner;
sharedVariable.InitializePropertyMapping(tree.GetBehaviorSource());
}

and then he edited it and changed it to

Code:
public static void SetPropertyMapping(this BehaviorTree tree, string containingClassName, GameObject propertyMappingOwner, string propertyName, object property)
{
var sharedVariable = tree.GetVariable(propertyName) ?? AddProperty(tree, propertyName, property);
sharedVariable.PropertyMapping = containingClassName+"/"+propertyName;
sharedVariable.PropertyMappingOwner = propertyMappingOwner;
sharedVariable.InitializePropertyMapping(tree.GetBehaviorSource());
}

seems that the AddProperty is a custom method that he did, will the first one work?

Edit - I tried with the first one, I am getting ArgumentException: Type cannot be null, I set it to health property and it does exist and is not null check the photo

download.png
 
Last edited:
If I want how can I convert the external tree to just regular tree that is on the player? so I can use the valuable mapping? (with there valuables inside it)
You can assign the external tree and then create new variables on your player GameObject which are mapped to the local properties.
Edit - I tried with the first one, I am getting ArgumentException: Type cannot be null, I set it to health property and it does exist and is not null check the photo
That first edit should work, it's just getting the variable. What line has the exception?
 
You can assign the external tree and then create new variables on your player GameObject which are mapped to the local properties.

That first edit should work, it's just getting the variable. What line has the exception?
Screenshot_515.png
Thank you, this is the error
 
You can assign the external tree and then create new variables on your player GameObject which are mapped to the local properties.

That first edit should work, it's just getting the variable. What line has the exception?
Hey, I did some tests, and its working now but he won't get values from it and still use the behavior tree last value
I should get 50, not 0
Screenshot_526.pngScreenshot_527.png
 
Another thing if you can help me with sometimes (sometimes it will work fine) the search will return true even if he doesn't see it

I am running it under a parallel task with abort type both
Thanks for helping :)
Screenshot_530.png
 
I will upload an example of the property mappings being set at runtime.

Another thing if you can help me with sometimes (sometimes it will work fine) the search will return true even if he doesn't see it
The best way to debug this is to place a breakpoint and see why it is returning a status of success when you expect failure.
 
I will upload an example of the property mappings being set at runtime.


The best way to debug this is to place a breakpoint and see why it is returning a status of success when you expect failure.
Thank you for helping me, I will test it out
And please send me a link when you upload an example.
 
I ended up not needing to do any scripting at all to make this work. In the attached Unity Package I am using property mappings with an external behavior tree by overriding the variable on the MainCamera behavior tree component. Notice the binding on this component is then used by the external behavior tree. Hopefully that helps.
 

Attachments

  • BoolMapping.unitypackage
    3.3 KB · Views: 9
I ended up not needing to do any scripting at all to make this work. In the attached Unity Package I am using property mappings with an external behavior tree by overriding the variable on the MainCamera behavior tree component. Notice the binding on this component is then used by the external behavior tree. Hopefully that helps.
Hey Thank you for your reply, Its still not working he getting the wrong value,
Screenshot_542.png
Here ^ before I map it to property it has the value of 100

Screenshot_543.png
After I map it ^
Screenshot_544.png
After I click play you can see that in the script the health value is 0 but the Behaviour designer gets 100 (the old value)
And I just checked with debug on update and the property does return 0 but the Behaviour designer won't update its own value

Edit - after some more testing, It works, and that the run time code is not working, seems that If I use the
SetPropertyMapping the Behaviour designer won't update its own value

Thanks for helping :)
 
Hey, allow me to bump this thread, I'm having the same error thrown when calling the SetPropertyMapping function, how did you solve it ?
 
Hey, allow me to bump this thread, I'm having the same error thrown when calling the SetPropertyMapping function, how did you solve it ?
Hey,
I mapped it via the inspector and its working perfect now
the code never worked for me
 
I've been using the manual mapping method as well but I'd really like to try and automate the whole process, it would save a lot of time for trees with many properties ! Allow me to ping you @Justin , is it possible to get the SetPropertyMapping functions ShadowZie posted working ? Thanks in advance !
 
SetPropertyMapping should work since that is what the editor is doing. I'll see if I can come with an example in one of the next updates.
 
Well it doesn't work on my end, the last line "sharedVariable.InitializePropertyMapping(tree.GetBehaviorSource());" throws the same error as ShadowZie posted - could you please try to paste the SetPropertyMapping extension and confirm whether if it works on your end ?
 
Top