Network Variable OnValueChanged not working

Hi there,
we are currently testing the MLAPI and we ran pretty fast in a problem with the NetworkVariables. We have an enum to store the player states(Alive, Dead, …). The Starting value is None. We subscribe to the OnValueChanged in NetworkStart and also change the value from None to Alive and this is working for the host, the problem is the client there the OnValueChanged event does not suplly the right value for the old Player State says Alive instead of None. This is kinda annoying. Does anyone has some advice or is more used to MLAPI and knows how to fix the Problem?

Thanks!

Unity Version: 2020.2.2f1
MLAPI Version: 0.1
Code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using MLAPI;
using MLAPI.Messaging;
using MLAPI.NetworkVariable;
using UnityEngine;

public enum PlayerState
{
    None,
    Alive,
    Dead
}

public class Player : NetworkBehaviour
{
    private static readonly Dictionary<PlayerState, PlayerStateLogic> PlayerStates = new Dictionary<PlayerState, PlayerStateLogic>
    {
        {PlayerState.None, new PlayerStateNone()},
        {PlayerState.Alive, new PlayerStateAlive()},
        {PlayerState.Dead, new PlayerStateDead()}
    };
  
    public readonly NetworkVariable<PlayerState> _playerState = new NetworkVariable<PlayerState>(new NetworkVariableSettings
    {
        ReadPermission = NetworkVariablePermission.Everyone,
        WritePermission = NetworkVariablePermission.Everyone
    }, PlayerState.None);

    public override void NetworkStart()
    {
        _playerState.OnValueChanged += ChangedPlayerState;
        Debug.Log(_playerState.Value);
        base.NetworkStart();
        if (!IsServer)
        {
            return;    
        }              
                       
        ChangePlayerState(PlayerState.Alive);
    }                  
                       
    private void Update()
    {                  
        PlayerStates[_playerState.Value].Update();
    }
  
    public void ChangePlayerState(PlayerState newPlayerState)
    {
        _playerState.Value = newPlayerState;
        _playerState.SetDirty(true);
    }

    private void ChangedPlayerState(PlayerState oldPlayerState, PlayerState newPlayerState)
    {
        Debug.Log($"{OwnerClientId} Changed Player State from {oldPlayerState.ToString()} to {newPlayerState.ToString()}");
        PlayerStates[oldPlayerState].End();
        PlayerStates[newPlayerState].Start();
    }
}

Better register delegate in OnEnable method. This way you won’t miss even triggers.

Yeah I think this is a bug currently in MLAPI. If you change a network variable value immediately after spawning on the server the OnValueChanged event on the clients will have the wrong value for the previous value. Can you create an issue on github? Sign in to GitHub · GitHub

This seems to remain an issue? Is there a workaround if you would like to change values on spawn?

I tried invoking the change after 5 seconds of spawn, but it still remains a problem. The Network Variable changes, however the OnChanged event is never triggered

I’m having the same issue. On the client when the object is spawned in the OnValueChanged call the previousValue and newValue are the same. This is a problem as the way I have it coded no action is taken if those values are the same. What’s the best workaround for this?

Not sure if this is related to your problem but just in case it is…

I noticed when joining a server with players already spawned, the NetworkVariable already had a non-default value set in Start and OnValueChange wasn’t called despite the delegate being set in OnEnable.

My solution for now is to check for non-default value in start and then call the OnValueChange destination manually.

1 Like

I’ve not delved into players late joining so haven’t come across that issue yet but thanks for mentioning it. Currently I do a specific condition check which I hope to remove when the issue is fixed. I did try working around it but it was creating timing issues and I decided it wasn’t worth the hassle going down that route.