I’ve been trying to fix a problem for a few days now and I don’t know how to solve it anymore.
Basically I can’t correctly synchronize my variable with clients. On my GameManager.cs
public static GameManager instance;
public readonly SyncDictionary<NetworkConnection, string> playerNames = new(new SyncTypeSettings( WritePermission.ClientUnsynchronized) );
private void Awake()
{
instance = this;
}
public void AddPlayerName(NetworkConnection conn)
{
playerNames.Add(conn, GenerateName());
}
public void RemovePlayerName(NetworkConnection conn)
{
playerNames.Remove(conn);
}
// ......
The data is not being updated using Observer (not even without it). This script is attached on Player (PlayerManager.cs)
// It works
public override void OnStartServer()
{
base.OnStartServer();
GameManager.instance.AddPlayerName(base.Owner);
UpdateUserboard();
}
// It dont
public override void OnStopServer()
{
base.OnStopServer();
GameManager.instance.RemovePlayerName(base.Owner);
UpdateUserboard();
}
public override void OnStopNetwork()
{
base.OnStopNetwork();
GameManager.instance.RemovePlayerName(base.Owner);
UpdateUserboard();
}
[ObserversRpc(BufferLast = true)]
void UpdateUserboard()
{
HUDController.instance.UpdateUserboard();
}
This is the function that updates the HUD interface on HUDController.cs:
public void UpdateUserboard()
{
playerNames.text = "<b>Players:</b>";
foreach (var playerName in GameManager.instance.playerNames)
{
playerNames.text += "
"+playerName.Value;
}
}
I even managed to solve my problem using this in my Update on player controller script, but the issue is that I don’t want to consume game resources when updating the player. I would like it to only update when the player is disconnected.
In my logic, everything should work, by the way, when the player is disconnected, the server removes it from the synchronous list and notifies the observers. Where is the problem?