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();
}
}