Keeping external/internal lists in sync, or iterating through a sharedList?

mostlyhuman

Member
I have a list in an external script that I need to initially copy to a behaviorTree shared list variable on Start, and then keep them in sync if something from the list changes during runtime in the external script. It would be one directional from the external list to the BT shared list. I know normally you set values on BT shared variables like this behaviorTree.SetVariable("AttackEvents", (SharedAttackEvents)attackEventNew); but how would I do basically like a foreach or iterate through it to populate it with values from a corresponding list in an external script or is there another way to achieve keeping these lists in sync as the list in the external script changes over time? Thank you for any help!
 

abc123

Member
Why copy list contents? You can copy a reference to the external list (replace Vector3 with the type you need)

C#:
using System.Collections.Generic;
using UnityEngine;

namespace BehaviorDesigner.Runtime.Tasks
{
    [System.Serializable]
    public class SharedVector3List : SharedVariable<List<Vector3>>
    {
        public static implicit operator SharedVector3List(List<Vector3> value) { return value; }
    }
}

Now you can add a Vector3List variable into behavior tree, and assign it's Value on Start. It will store a reference, so you don't need to sync anything.

Edit: I tested this method and it works...


...but reverse synchronization is not full. If you add/remove elements from the list inside the tree, it will lose the link to external list. I don't know why, let's wait for Justin's verdict)
 
Last edited:

mostlyhuman

Member
Why copy list contents? You can copy a link to the external list (replace Vector3 with the type you need)

C#:
using System.Collections.Generic;
using UnityEngine;

namespace BehaviorDesigner.Runtime.Tasks
{
    [System.Serializable]
    public class SharedVector3List : SharedVariable<List<Vector3>>
    {
        public static implicit operator SharedVector3List(List<Vector3> value) { return value; }
    }
}

Now you can add a Vector3List variable into behavior tree, and assign it's Value on Start. It will store a link to your external list, so you don't need to sync anything.

Edit: I tested this method and it works...


...but reverse synchronization is not full. If you add/remove elements from the list inside the tree, it will lose the link to external list. I don't know why, let's wait for Justin's verdict)

Thank you so much, that worked perfectly and easily!! :)
 

abc123

Member
Thank you so much, that worked perfectly and easily!! :)
You're welcome, be careful though - don't change external list while it is being processed somewhere inside the tree.. And if you are to destroy an object with external list, don't forget to nullify the reference.
 
Top