cryptomaxC
Normal2y ago
9 replies
cryptomax

DidChange Event not firing

The did change event is not firing on the client that is setting it from fresh.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Normal.Realtime;

public class ChangeEventTester : RealtimeComponent<IntSyncFreshModel>
{
public bool chooseRandomAtStart;
public Vector2 randomRange = new Vector2(1, 5); // Default random range

private IntSyncFreshModel _model;

protected override void OnRealtimeModelReplaced(IntSyncFreshModel previousModel, IntSyncFreshModel currentModel)
{
_model = currentModel;

if (previousModel != null)
{
previousModel.int1DidChange -= Int1DidChange;
}

if (currentModel != null)
{
if (currentModel.isFreshModel)
{
InitializeFreshModel(currentModel);
}
else
{
Int1DidChange(currentModel, currentModel.int1);
}

currentModel.int1DidChange += Int1DidChange;
}
}

private void InitializeFreshModel(IntSyncFreshModel model)
{
Debug.Log("Model is fresh.");
if (chooseRandomAtStart)
{
model.int1 = Random.Range((int)randomRange.x, (int)randomRange.y + 1);
Debug.Log($"Random value picked: {model.int1}");
}
else
{
model.int1 = 0; // Initialize with your desired default value
Debug.Log("Set int1 to default value 0.");
}
}

private void Int1DidChange(IntSyncFreshModel model, int value)
{
Debug.Log($"int1 changed to: {value}");
}

public int GetCurrentInt()
{
return _model.int1;
}
}

Any idea why that would be?
Was this page helpful?