NetworkDictionary bug when calling OnCallback

Hi guys, so I have this:

[Networked(OnChanged = nameof(ProgressTriggersUpdated)), Capacity(10)]
private NetworkDictionary<NetworkId, NetworkString<_16>> progressTriggers { get; }

When my ProgressTriggersUpdated callback is called I get on both old and new behavior the same values (when I add 1 element, count it is 1 on both when I add a second, the count is 2 on both). Here is the code of my callback function.

private static void ProgressTriggersUpdated(Changed<ObjectiveTriggerMediator> changed)
{
    changed.LoadOld();
    var old = changed.Behaviour;
    changed.LoadNew();
    var nw = changed.Behaviour;
    if (old.progressTriggers.Count > nw.progressTriggers.Count)
    {
        // logic
    }
    else if (old.progressTriggers.Count < nw.progressTriggers.Count)
    {
        // logic
    }
    // logs
    Debug.LogError(old.progressTriggers.Count);
    foreach (var pair in old.progressTriggers)
    {
        Debug.Log(pair.Key);
        Debug.Log(pair.Value);
    }
    Debug.LogError(nw.progressTriggers.Count);
    foreach (var pair in nw.progressTriggers)
    {
        Debug.Log(pair.Key);
        Debug.Log(pair.Value);
    }
}

Basically, I can not get the diff between the 2 states.

I can always write a workaround with some boilerplate code to get things going, but my concern is - if this is happening here, it might happen elsewhere where it will be harder to detect.

Is this a bug or am I doing something wrong? Can you help me with this?

Best Answer

  • Solution was to save dictionary variable like this;

    var nw = changed.Behaviour;
    changed.LoadOld();
    var old = changed.Behaviour;
    

    and then check nw and old variable's count.

    Credits to JCPereira from the Photon Engine discord channel.

Answers

  • Solution was to save dictionary variable like this;

    var nw = changed.Behaviour;
    changed.LoadOld();
    var old = changed.Behaviour;
    

    and then check nw and old variable's count.

    Credits to JCPereira from the Photon Engine discord channel.