OnObjectSpawnFinished message split into OnObjectSpawnStarted + OnObjectSpawnFinished. Saves 2 bytes of bandwidth per client spawn, and makes the code more obvious.

This commit is contained in:
vis2k 2019-01-16 22:10:08 +01:00
parent 7e9ed33448
commit 4ece68d053
4 changed files with 15 additions and 28 deletions

View File

@ -220,6 +220,7 @@ internal static void RegisterSystemHandlers(NetworkClient client, bool localClie
// LocalClient shares the sim/scene with the server, no need for these events // LocalClient shares the sim/scene with the server, no need for these events
client.RegisterHandler(MsgType.SpawnPrefab, OnSpawnPrefab); client.RegisterHandler(MsgType.SpawnPrefab, OnSpawnPrefab);
client.RegisterHandler(MsgType.SpawnSceneObject, OnSpawnSceneObject); client.RegisterHandler(MsgType.SpawnSceneObject, OnSpawnSceneObject);
client.RegisterHandler(MsgType.SpawnStarted, OnObjectSpawnStarted);
client.RegisterHandler(MsgType.SpawnFinished, OnObjectSpawnFinished); client.RegisterHandler(MsgType.SpawnFinished, OnObjectSpawnFinished);
client.RegisterHandler(MsgType.ObjectDestroy, OnObjectDestroy); client.RegisterHandler(MsgType.ObjectDestroy, OnObjectDestroy);
client.RegisterHandler(MsgType.ObjectHide, OnObjectDestroy); client.RegisterHandler(MsgType.ObjectHide, OnObjectDestroy);
@ -511,18 +512,18 @@ static void OnSpawnSceneObject(NetworkMessage netMsg)
ApplySpawnPayload(spawnedId, msg.position, msg.rotation, msg.payload, msg.netId); ApplySpawnPayload(spawnedId, msg.position, msg.rotation, msg.payload, msg.netId);
} }
static void OnObjectSpawnFinished(NetworkMessage netMsg) static void OnObjectSpawnStarted(NetworkMessage netMsg)
{ {
ObjectSpawnFinishedMessage msg = netMsg.ReadMessage<ObjectSpawnFinishedMessage>(); if (LogFilter.Debug) { Debug.Log("SpawnStarted"); }
if (LogFilter.Debug) { Debug.Log("SpawnFinished:" + msg.state); }
if (msg.state == 0)
{
PrepareToSpawnSceneObjects(); PrepareToSpawnSceneObjects();
s_IsSpawnFinished = false; s_IsSpawnFinished = false;
return;
} }
static void OnObjectSpawnFinished(NetworkMessage netMsg)
{
if (LogFilter.Debug) { Debug.Log("SpawnFinished"); }
// paul: Initialize the objects in the same order as they were initialized // paul: Initialize the objects in the same order as they were initialized
// in the server. This is important if spawned objects // in the server. This is important if spawned objects
// use data from scene objects // use data from scene objects

View File

@ -249,21 +249,9 @@ public override void Serialize(NetworkWriter writer)
} }
} }
// TODO use ByteMessage or use a SpawnStarted/Finished message. class ObjectSpawnStartedMessage : EmptyMessage {}
class ObjectSpawnFinishedMessage : MessageBase
{
public byte state; // byte because it's always 0 or 1
public override void Deserialize(NetworkReader reader) class ObjectSpawnFinishedMessage : EmptyMessage {}
{
state = reader.ReadByte();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(state);
}
}
class ObjectDestroyMessage : MessageBase class ObjectDestroyMessage : MessageBase
{ {

View File

@ -717,9 +717,7 @@ public static void SetClientReady(NetworkConnection conn)
// Spawn/update all current server objects // Spawn/update all current server objects
if (LogFilter.Debug) { Debug.Log("Spawning " + NetworkIdentity.spawned.Count + " objects for conn " + conn.connectionId); } if (LogFilter.Debug) { Debug.Log("Spawning " + NetworkIdentity.spawned.Count + " objects for conn " + conn.connectionId); }
ObjectSpawnFinishedMessage msg = new ObjectSpawnFinishedMessage(); conn.Send((short)MsgType.SpawnStarted, new ObjectSpawnStartedMessage());
msg.state = 0;
conn.Send((short)MsgType.SpawnFinished, msg);
foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values) foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values)
{ {
@ -742,8 +740,7 @@ public static void SetClientReady(NetworkConnection conn)
} }
} }
msg.state = 1; conn.Send((short)MsgType.SpawnFinished, new ObjectSpawnFinishedMessage());
conn.Send((short)MsgType.SpawnFinished, msg);
} }
internal static void ShowForConnection(NetworkIdentity identity, NetworkConnection conn) internal static void ShowForConnection(NetworkIdentity identity, NetworkConnection conn)

View File

@ -35,6 +35,7 @@ public enum MsgType : short
UpdateVars = 8, UpdateVars = 8,
SpawnPrefab = 3, SpawnPrefab = 3,
SpawnSceneObject = 10, SpawnSceneObject = 10,
SpawnStarted = 11,
SpawnFinished = 12, SpawnFinished = 12,
ObjectHide = 13, ObjectHide = 13,
LocalClientAuthority = 15, LocalClientAuthority = 15,