Accessing existing shared variables in a task

mostlyhuman

Member
I saw the docs on how to access them from non task objects but wasn't sure of what is probably a very simple way to access the variables through code within a task. Both regular and global. For instance I have a shared gameobject variable called Target, how do I check its value from a new task i'm creating? Is the syntax different for reading vs writing?

also is it possible to add a new variable of an existing shared type to the shared variable list from a task? For instance, I thought about creating an initialization task to put at the top of each tree that sets up common shared variables and their values.

Thanks in advance!
 
Last edited:

Justin

Administrator
Staff member
This page shows how to access variables within a script:


also is it possible to add a new variable of an existing shared type to the shared variable list from a task?
Yes, you can use shared variables within parent/child classes.
 

mostlyhuman

Member
Hmm, just to clarify, I am asking from within a Task, how to add, delete, read and write variables that will show up under the shared variable tab, in either the local shared or global shared areas, was that your understanding? Because in the example you sent it doesnt look like thats what the code is doing but I may be confused. I have a variable in the variable tab that is a gameObject called Target, but I cant just type Target in a new task code and it recognizes that it is from the variable tab list. Thanks, Justin.
 
Last edited:

Justin

Administrator
Staff member
Hmm, just to clarify, I am asking from within a Task, how to add, delete, read and write variables that will show up under the shared variable tab, in either the local shared or global shared areas, was that your understanding? Because in the example you sent it doesnt look like thats what the code is doing but I may be confused.
Yes, that page is an example of accessing the variable through a task. Here's also a video:


You'll end up with target.Value = yourValue;
 

mostlyhuman

Member
sorry never got this sorted, I dont think I explained it correctly. In the Variables tab area say I have a variable called Target that is a sharedGameObject. If you have a task that takes a sharedGameObject called objectToUse, you can click in that task's Inspector window, and to the right of the variable you can click that dot which turns the field red and in the dropdown I can tell it to reference the Target variable from the Variables tab list. My question is, inside the task code itself, how do I do this same thing, what is the syntax to have the task OnStart() assign objectToUse a reference to Target from the custom Variables tab? Is there a more direct syntax I can use since I am inside a task, or do I just still have to get a reference to the tree itself and do GetComponent<BehaviorTree>().GetVariable() like I do with an external script? Thanks as always!
 

abc123

Member
My question is, inside the task code itself, how do I do this same thing, what is the syntax to have the task OnStart() assign objectToUse a reference to Target from the custom Variables tab? Is there a more direct syntax I can use since I am inside a task, or do I just still have to get a reference to the tree itself and do GetComponent<BehaviorTree>().GetVariable() like I do with an external script? Thanks as always!
From task you can access the tree via Owner, like this

C#:
using UnityEngine;

namespace BehaviorDesigner.Runtime.Tasks
{
    public class ExampleTask : Action
    {
        private Transform objectToUse;
 
        public override void OnStart()
        {
            objectToUse = ((SharedTransform)Owner.GetVariable("Target")).Value;
        }
    }
}

However, it will not update If during execution Target starts to reference another transform. So you need to do this:

C#:
using UnityEngine;

namespace BehaviorDesigner.Runtime.Tasks
{
    public class ExampleTask : Action
    {
        private SharedTransform objectToUse;
 
        public override void OnAwake()
        {
            objectToUse = (SharedTransform)Owner.GetVariable("Target");
        }
    }
}

This way they will always reference the same transform, which you'll get with objectToUse.Value. But why do this inside the class, hard coding a variable name? Its better to make objectToUse public* and link variables in the inspector, just as intended.

*If you want to keep it private, but still visible in the inspector:
C#:
[SerializeField]
private SharedTransform objectToUse;
 
Last edited:

mostlyhuman

Member
abc123 you are killing it with support! thank you, this was exactly what I needed and so glad to have you around to help!
 

mostlyhuman

Member
One more follow-up question, is there a way to create new variables in the main variables tab from code within a task? I am wanting to create an initialization task that sets up common variables that would be placed at the root of each tree. and if so what would the syntax difference be to create global variables in the variables tab versus local variables in the variables tab? Thank you again!
 

Justin

Administrator
Staff member
is there a way to create new variables in the main variables tab from code within a task?
No, not at runtime. The reason is because you need to assign the variables to the tasks, and that is done within the editor. In your case you should create all of the variables that you need, and then assign the values at runtime.
 
Top