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?
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?
5 Replies
Hi @cryptomax. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the provided code, it seems like you're correctly subscribing to the
int1DidChange
event in the OnRealtimeModelReplaced
method. However, there are a few things you might want to check:
1. RealtimeComponent: Make sure your custom component has been added to a game object after it was converted to a RealtimeComponent subclass. If you added your custom component to a game object before it was converted to a RealtimeComponent subclass, OnRealtimeModelReplaced
might not get called. You can verify this by looking at the RealtimeView's list of components and checking if your custom component is in the list. If you don't see it, or a RealtimeView has not been created for you automatically, try removing the component and re-adding it to your game object. This will tell Unity to automatically create a RealtimeView and add your RealtimeComponent subclass to its component list (source (https://normcore.io/documentation/realtime/synchronizing-custom-data#my-custom-realtimecomponent-isnt-syncing-and-onrealtimemodelreplaced-doesnt-get-called)).
2. Change Event: Ensure that the change event has been added to the model. When this is set to true, a C# event is added; it will fire when a property is changed locally or remotely. This is a useful signal to update your scene to match the model (source (https://normcore.io/documentation/realtime/synchronizing-custom-data#change-event)).
3. Network Updates: Normcore serializes all updates once per network frame (~20hz) and groups them into the same packet. When model updates are applied, they’re applied one at a time, and a change event fires for that value. However, it’s possible the other values from that packet hadn’t yet been applied. In order to get an event that fires once all updates have been applied to a model, use RealtimeCallback (source (https://normcore.io/documentation/room/common-questions#sometimes-when-i-log-the-values-on-my-model-from-inside-of-valuedidchange-not-all-the-values-that-i-set-on-another-client-update-when-are-updates-sent-out-and-how-are-they-applied)).
If you've checked all these points and the issue persists, it might be helpful to look into any potential network issues or errors in other parts of your code.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@401986039964631060> kapa.ai is still learning and improving, please let us know how it did by reacting below
wierd. change event fires fine if I just set the model after the fact, but it's not firing on self from setting it from fresh. This was an early bug but i'm running normcore 2.7.1 and that was fixed in this version I think
it's unity 2020.3.48
The
currentModel.int1DidChange += Int1DidChange;
line should be at the top of the block because it's subscribing after the change atm
This fixed it in my test projectoh my gosh how did i manage that i've made tons of these, somehow I got my circuits flipped here. thank you lol ! Onse of thoe late nights wondering why it wasn't working, sometimes your better off just quiting for the night XD