Skip to content

Commit

Permalink
Simplify spawning
Browse files Browse the repository at this point in the history
Network server knows about the host mode client, so
we don't have to pass it every time

this also improves compatibility with Mirror

BREAKING CHANGE: Spawn no longer receives NetworkClient
  • Loading branch information
paulpach committed Jan 25, 2020
1 parent 279297f commit c87a38a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Assets/Mirror/Examples/Pong/Scripts/NetworkManagerPong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void OnServerAddPlayer(NetworkConnection conn)
if (numPlayers == 2)
{
ball = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "Ball"));
server.Spawn(ball, client);
server.Spawn(ball);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Examples/Room/Scripts/Spawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void SpawnPrize()
Reward reward = newPrize.gameObject.GetComponent<Reward>();
reward.spawner = this;

server.Spawn(newPrize, client);
server.Spawn(newPrize);
}
}
}
2 changes: 1 addition & 1 deletion Assets/Mirror/Examples/Tanks/Scripts/Tank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void Update()
void CmdFire()
{
GameObject projectile = Instantiate(projectilePrefab, projectileMount.position, transform.rotation);
server.Spawn(projectile, client);
server.Spawn(projectile);
RpcOnFire();
}

Expand Down
10 changes: 5 additions & 5 deletions Assets/Mirror/Runtime/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public void StartServer()
// otherwise spawn directly
else
{
server.SpawnObjects(client);
server.SpawnObjects();
}
}

Expand Down Expand Up @@ -474,7 +474,7 @@ void FinishStartHost()
client.ConnectHost(server);

// server scene was loaded. now spawn all the objects
server.SpawnObjects(client);
server.SpawnObjects();

// connect client and call OnStartClient AFTER server scene was
// loaded and all objects were spawned.
Expand Down Expand Up @@ -851,7 +851,7 @@ void OnSceneLoaded(Scene scene, LoadSceneMode mode)
if (server.active)
{
// TODO only respawn the server objects from that scene later!
server.SpawnObjects(client);
server.SpawnObjects();
Debug.Log("Respawned Server objects after additive scene load: " + scene.name);
}
if (client.active)
Expand Down Expand Up @@ -944,7 +944,7 @@ void FinishLoadSceneHost()
else
{
// spawn server objects
server.SpawnObjects(client);
server.SpawnObjects();

// call OnServerSceneChanged
OnServerSceneChanged(networkSceneName);
Expand Down Expand Up @@ -989,7 +989,7 @@ void FinishLoadSceneServerOnly()
// it's very obvious to notice.
Debug.Log("Finished loading scene in server-only mode.");

server.SpawnObjects(client);
server.SpawnObjects();
OnServerSceneChanged(networkSceneName);
}

Expand Down
28 changes: 17 additions & 11 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ public class NetworkServer : MonoBehaviour
// => removed it for easier code. use .localConnection now!
public NetworkConnectionToClient localConnection { get; private set; }

// The host client for this server
public NetworkClient localClient;

/// <summary>
/// <para>True is a local client is currently active on the server.</para>
/// <para>This will be true for "Hosts" on hosted server games.</para>
/// </summary>
public bool localClientActive => localConnection != null;
public bool localClientActive => localClient.active;

/// <summary>
/// A list of local connections on the server.
Expand Down Expand Up @@ -108,6 +111,9 @@ void Initialize()
if (initialized)
return;

if (localClient == null)
localClient = GetComponent<NetworkClient>();

initialized = true;
if (LogFilter.Debug) Debug.Log("NetworkServer Created version " + Version.Current);

Expand Down Expand Up @@ -720,7 +726,7 @@ void Respawn(NetworkIdentity identity)
if (identity.netId == 0)
{
// If the object has not been spawned, then do a full spawn and update observers
Spawn(identity.gameObject, identity.client, identity.connectionToClient);
Spawn(identity.gameObject, identity.connectionToClient);
}
else
{
Expand Down Expand Up @@ -896,7 +902,7 @@ void OnCommandMessage(NetworkConnection conn, CommandMessage msg)
identity.HandleCommand(msg.componentIndex, msg.functionHash, new NetworkReader(msg.payload));
}

internal void SpawnObject(GameObject obj, NetworkClient client, NetworkConnection ownerConnection)
internal void SpawnObject(GameObject obj, NetworkConnection ownerConnection)
{
if (!active)
{
Expand All @@ -913,7 +919,7 @@ internal void SpawnObject(GameObject obj, NetworkClient client, NetworkConnectio
identity.Reset();
identity.connectionToClient = (NetworkConnectionToClient)ownerConnection;
identity.server = this;
identity.client = client;
identity.client = localClient;

// special case to make sure hasAuthority is set
// on start server in host mode
Expand Down Expand Up @@ -993,11 +999,11 @@ public void DestroyPlayerForConnection(NetworkConnection conn)
/// <param name="obj">Game object with NetworkIdentity to spawn.</param>
/// <param name="client">Client associated to the object.</param>
/// <param name="ownerConnection">The connection that has authority over the object</param>
public void Spawn(GameObject obj, NetworkClient client, NetworkConnection ownerConnection = null)
public void Spawn(GameObject obj, NetworkConnection ownerConnection = null)
{
if (VerifyCanSpawn(obj))
{
SpawnObject(obj, client, ownerConnection);
SpawnObject(obj, ownerConnection);
}
}

Expand Down Expand Up @@ -1048,7 +1054,7 @@ public void Spawn(GameObject obj, GameObject player)
return;
}

Spawn(obj, identity.client, identity.connectionToClient);
Spawn(obj, identity.connectionToClient);
}

/// <summary>
Expand All @@ -1059,15 +1065,15 @@ public void Spawn(GameObject obj, GameObject player)
/// <param name="assetId">The assetId of the object to spawn. Used for custom spawn handlers.</param>
/// <param name="client">The client associated to the object.</param>
/// <param name="ownerConnection">The connection that has authority over the object</param>
public void Spawn(GameObject obj, Guid assetId, NetworkClient client, NetworkConnection ownerConnection = null)
public void Spawn(GameObject obj, Guid assetId, NetworkConnection ownerConnection = null)
{
if (VerifyCanSpawn(obj))
{
if (GetNetworkIdentity(obj, out NetworkIdentity identity))
{
identity.assetId = assetId;
}
SpawnObject(obj, client, ownerConnection);
SpawnObject(obj, ownerConnection);
}
}

Expand Down Expand Up @@ -1157,7 +1163,7 @@ bool ValidateSceneObject(NetworkIdentity identity)
/// </summary>
/// <param name="client">The client associated to the objects.</param>
/// <returns>Success if objects where spawned.</returns>
public bool SpawnObjects(NetworkClient client)
public bool SpawnObjects()
{
if (!active)
return true;
Expand All @@ -1176,7 +1182,7 @@ public bool SpawnObjects(NetworkClient client)
foreach (NetworkIdentity identity in identities)
{
if (ValidateSceneObject(identity))
Spawn(identity.gameObject, client);
Spawn(identity.gameObject);
}
return true;
}
Expand Down

0 comments on commit c87a38a

Please sign in to comment.