Skip to content

Commit

Permalink
fix: moving syncvar sender to networkserver so it gets intilized earlier
Browse files Browse the repository at this point in the history
fixes SyncVarSender being null sometimes when Server.Started is called
  • Loading branch information
James-Frowen committed Jun 1, 2021
1 parent 850c4b9 commit 8b2b828
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Assets/Mirage/Runtime/INetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public interface INetworkServer

NetworkWorld World { get; }

SyncVarSender SyncVarSender { get; }

IReadOnlyCollection<INetworkPlayer> Players { get; }

void Stop();
Expand Down
4 changes: 2 additions & 2 deletions Assets/Mirage/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private void SyncObject_OnChange()
{
if (IsServer)
{
ServerObjectManager.SyncVarSender.AddDirtyObject(NetIdentity);
Server.SyncVarSender.AddDirtyObject(NetIdentity);
}
}

Expand Down Expand Up @@ -377,7 +377,7 @@ public void SetDirtyBit(ulong dirtyBit)
{
SyncVarDirtyBits |= dirtyBit;
if (IsServer)
ServerObjectManager.SyncVarSender.AddDirtyObject(NetIdentity);
Server.SyncVarSender.AddDirtyObject(NetIdentity);
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public class NetworkServer : MonoBehaviour, INetworkServer
public NetworkTime Time { get; } = new NetworkTime();

public NetworkWorld World { get; private set; }
public SyncVarSender SyncVarSender { get; private set; }


/// <summary>
/// This shuts down the server and disconnects all clients.
Expand Down Expand Up @@ -199,6 +201,8 @@ public void StartServer(NetworkClient localClient = null)

LocalClient = localClient;
World = new NetworkWorld();
SyncVarSender = new SyncVarSender();


ISocket socket = SocketFactory.CreateServerSocket();
var dataHandler = new DataHandler(connections);
Expand Down Expand Up @@ -246,6 +250,7 @@ void ThrowIfSocketIsMissing()
private void Update()
{
peer?.Update();
SyncVarSender?.Update();
}

private void Peer_OnConnected(IConnection conn)
Expand Down Expand Up @@ -293,6 +298,9 @@ private void Cleanup()
_onStopHost.Reset();
_stopped.Reset();

World = null;
SyncVarSender = null;

Application.quitting -= Stop;

if (peer != null)
Expand Down
10 changes: 0 additions & 10 deletions Assets/Mirage/Runtime/ServerObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public class ServerObjectManager : MonoBehaviour, IServerObjectManager
uint nextNetworkId = 1;
uint GetNextNetworkId() => checked(nextNetworkId++);

public SyncVarSender SyncVarSender { get; private set; }

public void Start()
{
if (Server != null)
Expand All @@ -68,12 +66,6 @@ public void Start()
}
}

// The user should never need to pump the update loop manually
internal void Update()
{
SyncVarSender?.Update();
}

internal void RegisterMessageHandlers(INetworkPlayer player)
{
player.RegisterHandler<ReadyMessage>(OnClientReadyMessage);
Expand All @@ -87,7 +79,6 @@ void OnAuthenticated(INetworkPlayer player)

void OnServerStarted()
{
SyncVarSender = new SyncVarSender();
SpawnOrActivate();
}

Expand All @@ -101,7 +92,6 @@ void OnServerStopped()
}

Server.World.ClearSpawnedObjects();
SyncVarSender = null;
// reset so ids stay small in each session
nextNetworkId = 1;
}
Expand Down

0 comments on commit 8b2b828

Please sign in to comment.