MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

Notified when a specific SyncPrimitive changes

Open kyleWillson opened this issue 9 years ago • 5 comments

I am trying to understand how to setup sharing, while setting up an exploratory test I was wondering something. From what I can tell, if a SyncPrimitive child changes in a SyncObejct, the ObjectChanged flag will be fired. But does that ObjectChanged flag tell you which particular SyncPrimitive Child changed? Or does it just say, "hey, there was a change in the children"? Which is the way I think it works. Is that correct?

So based on above, if I want to be notified of which particular SyncPrimitive Changed, I wrap SyncBool in a SyncObject wrapper. Like this:

[SyncDataClass]
public class SyncNotifyingBool : SyncObject
{
    [SyncData] public SyncBool IsActive;
}

Now I can be notified when individual SyncPrimitives Change in my SyncObejct class like:

[SyncDataClass]
public class SyncSpawnedTestContainer : SyncSpawnedObject
{
    [SyncData] public SyncNotifyingBool IsAActive;

    [SyncData] public SyncNotifyingBool IsBActive;

Is that generally the right idea overall?

kyleWillson avatar Oct 16 '17 15:10 kyleWillson

Okay solved this issue by altering SyncSpawnedObject to inlcude a Syncbool.

/// <summary>
/// A SpawnedObject contains all the information needed for another device to spawn an object in the same location
/// as where it was originally created on this device.
/// </summary>
[SyncDataClass]
public class SyncSpawnedObject : SyncObject
{
    [SyncData] public SyncTransform Transform;

    [SyncData] public SyncString Name;

    [SyncData] public SyncString ParentPath;

    [SyncData] public SyncString ObjectPath;

    [SyncData] public SyncBool NetworkBoolean00; // new SyncBool

This Syncbool (the datamodel) is then initialized before instantiation in the PrefabSpawnmanager like so:

         // Add the data model object to the networked array, for networking and history purposes
        dataModel.Initialize(instanceName, parent.transform.GetFullPath("/"));
        dataModel.Transform.Position.Value = localPosition;
        dataModel.Transform.Rotation.Value = localRotation;
        dataModel.NetworkBoolean00.Value = false; // the new bool on SyncSpawnedObject

and at the same time the TransformSynchronizer is being added I also find my script on my prefab called BoolSynchronizer using the GetComponentInChildren AND set its boolDataModel to the same we created earlier on Spawn

        // Setup the transform synchronization
        TransformSynchronizer transformSynchronizer = instance.EnsureComponent<TransformSynchronizer>();
        transformSynchronizer.TransformDataModel = dataModel.Transform;
        BoolSynchronizer boolSynchronizer = instance.GetComponentInChildren<BoolSynchronizer>();
        if(boolSynchronizer != null)
        {
            boolSynchronizer.boolDataModel = dataModel.NetworkBoolean00;
        }

This is 90% the way there. The last 10% is writing your own BoolSynchronizer to do what you want with it and what happens when the variable changes. Took a few days of reading over the code again and again.

Ristophonics avatar Feb 07 '18 21:02 Ristophonics

@Ristophonics Could you share your BoolSynchronizer. Having trouble implementing it like the way transformSynchronizer works.

Firifire avatar Apr 23 '18 14:04 Firifire

@Firifire here is the most stripped down version. Please post any improvement. Regards

namespace HoloToolkit.Sharing
{

    public class BoolSynchronizer : MonoBehaviour
    {
        [SerializeField] public bool _thisToggle00;
        [SerializeField] public GameObject _GameobjectToToggle00;

        public SyncBool boolDataModel00;

        private void Start()
        {
            _thisToggle00 = false;
        }

        public void ToggleOffSwitch00()
        {
            if (_thisToggle00 == true)
            {
                Debug.Log("Toggling OFF");
                _thisToggle00 = false;
                boolDataModel00.Value = _thisToggle00;
                Debug.Log("Syncbool Value is NOW" + boolDataModel00.Value.ToString());
            }
        }

        public void ToggleOnSwitch00()
        {
            if (_thisToggle00 == false)
            {
                Debug.Log("Toggling ON");
                _thisToggle00 = true;
                boolDataModel00.Value = _thisToggle00;
                Debug.Log("Syncbool Value is NOW" + boolDataModel00.Value.ToString());
            }
        }

        private void CheckBoolStatus()
        {
            if (_thisToggle00 != boolDataModel00.Value)
            {
                _thisToggle00 = boolDataModel00.Value;
            }
        }

        private void FixedUpdate()
        {
            if (_thisToggle00)
            {
                _GameobjectToToggle00.SetActive(true);
            }
            if (!_thisToggle00)
            {
                _GameobjectToToggle00.SetActive(false);
            }
            CheckBoolStatus();
        }
    }
}

Ristophonics avatar Apr 24 '18 04:04 Ristophonics

@Ristophonics Thanks. Seems like ObjectChanged was not working for me.

Well, with your help. I am currently progressing like this.

I have altered SyncSpawnedObject to include two additional function

 ...      
        public virtual void SecondaryInitilize() //To initialize variables
        {

        }

        public virtual void AttachSync(GameObject instance) //To attach Synchronizer
        {

        }

Then in PrefabSpawnManager I add two lines

...
            dataModel.Initialize(instanceName, parent.transform.GetFullPath("/"));
            dataModel.Transform.Position.Value = localPosition;
            dataModel.Transform.Rotation.Value = localRotation;
            dataModel.SecondaryInitilize(); //Initialize Variables

...
            // Setup the transform synchronization
            TransformSynchronizer transformSynchronizer = instance.EnsureComponent<TransformSynchronizer>();
            transformSynchronizer.TransformDataModel = dataModel.Transform;
            dataModel.AttachSync(instance); //Attach Sync

instead of adding variables to SyncSpawnedObject. I create another SyncDataClass inheriting SyncSpawnedObject, and overriding the functions

/// <summary>
/// A SpawnedObject contains all the information needed for another device to spawn an object in the same location
/// as where it was originally created on this device.
/// </summary>
[SyncDataClass]
public class SyncToggle: SyncSpawnedObject 
{
    [SyncData] public SyncBool NetworkBoolean00; // new SyncBool`

    public override void SecondaryInitilize(GameObject instance)
    {
        NetworkBoolean00 = false;
    }
    public override void AttachSync(GameObject instance)
    {
        BoolSynchronizer boolSynchronizer = instance.EnsureComponent<BoolSynchronizer >();
        boolSynchronizer.boolDataModel = NetworkBoolean00;
    }

Firifire avatar Apr 25 '18 11:04 Firifire

Closing issues older than 180 days. If this is still an issue, please reactivate with recent information.

wiwei avatar Apr 11 '19 20:04 wiwei