Skip to content

Commit bba72a4

Browse files
refactor!: remove spawn payload technical debt (#1005)
* refactor Removing the option to send a payload when spawning an object. The primary reason for this is that we do not save the payload data on a per NetworkObject instance basis and if a user were to use the spawn payload parameter that included data that was required to set any specific states of a NetworkObject or associated components then late joining players would not receive the payload information and would become out of sych with the game. * refactor and style Needed more than 1 commit and left out a few checks for payload when reading as well as removed XML Doc parameters that referred to "payload".
1 parent cbe74c2 commit bba72a4

File tree

8 files changed

+37
-89
lines changed

8 files changed

+37
-89
lines changed

com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,21 +316,12 @@ protected NetworkBehaviour GetNetworkBehaviour(ushort behaviourId)
316316
public ulong OwnerClientId => NetworkObject.OwnerClientId;
317317

318318
/// <summary>
319-
/// Gets called when message handlers are ready to be registered and the network is setup
319+
/// Gets called when the <see cref="MLAPI.NetworkObject"/> gets spawned, message handlers are ready to be registered and the network is setup.
320320
/// </summary>
321321
public virtual void OnNetworkSpawn() { }
322322

323323
/// <summary>
324-
/// Gets called when the <see cref="NetworkObject"/> gets spawned, message handlers are ready to be registered and the network is setup. Provides a Payload if it was provided
325-
/// </summary>
326-
/// <param name="stream">The stream containing the spawn payload</param>
327-
public virtual void OnNetworkSpawn(Stream stream)
328-
{
329-
OnNetworkSpawn();
330-
}
331-
332-
/// <summary>
333-
/// Gets called when the <see cref="NetworkObject"/> gets de-spawned. Is called both on the server and clients.
324+
/// Gets called when the <see cref="MLAPI.NetworkObject"/> gets despawned. Is called both on the server and clients.
334325
/// </summary>
335326
public virtual void OnNetworkDespawn() { }
336327

com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ internal void HandleApproval(ulong ownerClientId, bool createPlayerObject, uint?
14151415
if (createPlayerObject)
14161416
{
14171417
var networkObject = SpawnManager.CreateLocalNetworkObject(false, playerPrefabHash ?? NetworkConfig.PlayerPrefab.GetComponent<NetworkObject>().GlobalObjectIdHash, ownerClientId, null, position, rotation);
1418-
SpawnManager.SpawnNetworkObjectLocally(networkObject, SpawnManager.GetNetworkObjectId(), false, true, ownerClientId, null, false, 0, false, false);
1418+
SpawnManager.SpawnNetworkObjectLocally(networkObject, SpawnManager.GetNetworkObjectId(), false, true, ownerClientId, null, false, false);
14191419

14201420
ConnectedClients[ownerClientId].PlayerObject = networkObject;
14211421
}

com.unity.multiplayer.mlapi/Runtime/Core/NetworkObject.cs

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
using System.IO;
44
using System.Linq;
55
using System.Runtime.CompilerServices;
6-
using MLAPI.Configuration;
76
using MLAPI.Exceptions;
87
using MLAPI.Hashing;
98
using MLAPI.Logging;
109
using MLAPI.Messaging;
11-
using MLAPI.Serialization.Pooled;
1210
using MLAPI.Transports;
1311
using MLAPI.Serialization;
1412
using UnityEngine;
@@ -222,11 +220,10 @@ private void Awake()
222220
}
223221

224222
/// <summary>
225-
/// Shows a previously hidden object to a client
223+
/// Shows a previously hidden <see cref="NetworkObject"/> to a client
226224
/// </summary>
227-
/// <param name="clientId">The client to show the object to</param>
228-
/// <param name="payload">An optional payload to send as part of the spawn</param>
229-
public void NetworkShow(ulong clientId, Stream payload = null)
225+
/// <param name="clientId">The client to show the <see cref="NetworkObject"/> to</param>
226+
public void NetworkShow(ulong clientId)
230227
{
231228
if (!IsSpawned)
232229
{
@@ -245,16 +242,15 @@ public void NetworkShow(ulong clientId, Stream payload = null)
245242

246243
Observers.Add(clientId);
247244

248-
NetworkManager.SpawnManager.SendSpawnCallForObject(clientId, OwnerClientId, this, payload);
245+
NetworkManager.SpawnManager.SendSpawnCallForObject(clientId, OwnerClientId, this);
249246
}
250247

251248
/// <summary>
252-
/// Shows a list of previously hidden objects to a client
249+
/// Shows a list of previously hidden <see cref="NetworkObject"/>s to a client
253250
/// </summary>
254-
/// <param name="networkObjects">The objects to show</param>
251+
/// <param name="networkObjects">The <see cref="NetworkObject"/>s to show</param>
255252
/// <param name="clientId">The client to show the objects to</param>
256-
/// <param name="payload">An optional payload to send as part of the spawns</param>
257-
public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientId, Stream payload = null)
253+
public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientId)
258254
{
259255
if (networkObjects == null || networkObjects.Count == 0)
260256
{
@@ -304,7 +300,7 @@ public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientI
304300
networkObjects[i].Observers.Add(clientId);
305301

306302
networkManager.SpawnManager.WriteSpawnCallForObject(nonNullContext.NetworkWriter, clientId,
307-
networkObjects[i], payload);
303+
networkObjects[i]);
308304
}
309305
}
310306
}
@@ -429,7 +425,7 @@ private void OnDestroy()
429425
}
430426

431427
[MethodImpl(MethodImplOptions.AggressiveInlining)]
432-
private void SpawnInternal(Stream spawnPayload, bool destroyWithScene, ulong? ownerClientId, bool playerObject)
428+
private void SpawnInternal(bool destroyWithScene, ulong? ownerClientId, bool playerObject)
433429
{
434430
if (!NetworkManager.IsListening)
435431
{
@@ -441,52 +437,44 @@ private void SpawnInternal(Stream spawnPayload, bool destroyWithScene, ulong? ow
441437
throw new NotServerException($"Only server can spawn {nameof(NetworkObject)}s");
442438
}
443439

444-
if (spawnPayload != null)
445-
{
446-
spawnPayload.Position = 0;
447-
}
448-
449-
NetworkManager.SpawnManager.SpawnNetworkObjectLocally(this, NetworkManager.SpawnManager.GetNetworkObjectId(), false, playerObject, ownerClientId, spawnPayload, spawnPayload != null, spawnPayload == null ? 0 : (int)spawnPayload.Length, false, destroyWithScene);
440+
NetworkManager.SpawnManager.SpawnNetworkObjectLocally(this, NetworkManager.SpawnManager.GetNetworkObjectId(), false, playerObject, ownerClientId,null, false, destroyWithScene);
450441
ulong ownerId = ownerClientId != null ? ownerClientId.Value : NetworkManager.ServerClientId;
451442
for (int i = 0; i < NetworkManager.ConnectedClientsList.Count; i++)
452443
{
453444
if (Observers.Contains(NetworkManager.ConnectedClientsList[i].ClientId))
454445
{
455-
NetworkManager.SpawnManager.SendSpawnCallForObject(NetworkManager.ConnectedClientsList[i].ClientId, ownerId, this, spawnPayload);
446+
NetworkManager.SpawnManager.SendSpawnCallForObject(NetworkManager.ConnectedClientsList[i].ClientId, ownerId, this);
456447
}
457448
}
458449
}
459450

460451
/// <summary>
461-
/// Spawns this GameObject across the network. Can only be called from the Server
452+
/// Spawns this <see cref="NetworkObject"/> across the network. Can only be called from the Server
462453
/// </summary>
463-
/// <param name="spawnPayload">The writer containing the spawn payload</param>
464454
/// <param name="destroyWithScene">Should the object be destroyed when the scene is changed</param>
465-
public void Spawn(Stream spawnPayload = null, bool destroyWithScene = false)
455+
public void Spawn(bool destroyWithScene = false)
466456
{
467-
SpawnInternal(spawnPayload, destroyWithScene, null, false);
457+
SpawnInternal(destroyWithScene, null, false);
468458
}
469459

470460
/// <summary>
471-
/// Spawns an object across the network with a given owner. Can only be called from server
461+
/// Spawns a <see cref="NetworkObject"/> across the network with a given owner. Can only be called from server
472462
/// </summary>
473463
/// <param name="clientId">The clientId to own the object</param>
474-
/// <param name="spawnPayload">The writer containing the spawn payload</param>
475-
/// <param name="destroyWithScene">Should the object be destroyd when the scene is changed</param>
476-
public void SpawnWithOwnership(ulong clientId, Stream spawnPayload = null, bool destroyWithScene = false)
464+
/// <param name="destroyWithScene">Should the object be destroyed when the scene is changed</param>
465+
public void SpawnWithOwnership(ulong clientId, bool destroyWithScene = false)
477466
{
478-
SpawnInternal(spawnPayload, destroyWithScene, clientId, false);
467+
SpawnInternal(destroyWithScene, clientId, false);
479468
}
480469

481470
/// <summary>
482-
/// Spawns an object across the network and makes it the player object for the given client
471+
/// Spawns a <see cref="NetworkObject"/> across the network and makes it the player object for the given client
483472
/// </summary>
484473
/// <param name="clientId">The clientId whos player object this is</param>
485-
/// <param name="spawnPayload">The writer containing the spawn payload</param>
486474
/// <param name="destroyWithScene">Should the object be destroyd when the scene is changed</param>
487-
public void SpawnAsPlayerObject(ulong clientId, Stream spawnPayload = null, bool destroyWithScene = false)
475+
public void SpawnAsPlayerObject(ulong clientId, bool destroyWithScene = false)
488476
{
489-
SpawnInternal(spawnPayload, destroyWithScene, clientId, true);
477+
SpawnInternal(destroyWithScene, clientId, true);
490478
}
491479

492480
/// <summary>
@@ -781,12 +769,12 @@ internal static void CheckOrphanChildren()
781769
}
782770
}
783771

784-
internal void InvokeBehaviourNetworkSpawn(Stream stream)
772+
internal void InvokeBehaviourNetworkSpawn()
785773
{
786774
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
787775
{
788776
ChildNetworkBehaviours[i].InternalOnNetworkSpawn();
789-
ChildNetworkBehaviours[i].OnNetworkSpawn(stream);
777+
ChildNetworkBehaviours[i].OnNetworkSpawn();
790778
}
791779
}
792780

@@ -1055,7 +1043,7 @@ internal static NetworkObject DeserializeSceneObject(NetworkBuffer objectStream,
10551043
}
10561044

10571045
// Spawn the NetworkObject
1058-
networkManager.SpawnManager.SpawnNetworkObjectLocally(networkObject, networkId, isSceneObject, isPlayerObject, ownerClientId, objectStream, false, 0, true, false);
1046+
networkManager.SpawnManager.SpawnNetworkObjectLocally(networkObject, networkId, isSceneObject, isPlayerObject, ownerClientId, objectStream, true, false);
10591047

10601048
return networkObject;
10611049
}

com.unity.multiplayer.mlapi/Runtime/Messaging/InternalMessageHandler.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,9 @@ public void HandleAddObject(ulong clientId, Stream stream)
164164

165165
var (isReparented, latestParent) = NetworkObject.ReadNetworkParenting(reader);
166166

167-
var hasPayload = reader.ReadBool();
168-
var payLoadLength = hasPayload ? reader.ReadInt32Packed() : 0;
169-
170167
var networkObject = NetworkManager.SpawnManager.CreateLocalNetworkObject(softSync, prefabHash, ownerClientId, parentNetworkId, pos, rot, isReparented);
171168
networkObject.SetNetworkParenting(isReparented, latestParent);
172-
NetworkManager.SpawnManager.SpawnNetworkObjectLocally(networkObject, networkId, softSync, isPlayerObject, ownerClientId, stream, hasPayload, payLoadLength, true, false);
169+
NetworkManager.SpawnManager.SpawnNetworkObjectLocally(networkObject, networkId, softSync, isPlayerObject, ownerClientId, stream, true, false);
173170
}
174171
}
175172

com.unity.multiplayer.mlapi/Runtime/SceneManagement/NetworkSceneManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private void OnServerLoadedScene(Guid switchSceneGuid)
346346
{
347347
if (!keyValuePair.Value.IsPlayerObject)
348348
{
349-
m_NetworkManager.SpawnManager.SpawnNetworkObjectLocally(keyValuePair.Value, m_NetworkManager.SpawnManager.GetNetworkObjectId(), true, false, null, null, false, 0, false, true);
349+
m_NetworkManager.SpawnManager.SpawnNetworkObjectLocally(keyValuePair.Value, m_NetworkManager.SpawnManager.GetNetworkObjectId(), true, false, null, null, false, true);
350350
}
351351
}
352352

com.unity.multiplayer.mlapi/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ internal NetworkObject CreateLocalNetworkObject(bool isSceneObject, uint globalO
282282
}
283283

284284
// Ran on both server and client
285-
internal void SpawnNetworkObjectLocally(NetworkObject networkObject, ulong networkId, bool sceneObject, bool playerObject, ulong? ownerClientId, Stream dataStream, bool readPayload, int payloadLength, bool readNetworkVariable, bool destroyWithScene)
285+
internal void SpawnNetworkObjectLocally(NetworkObject networkObject, ulong networkId, bool sceneObject, bool playerObject, ulong? ownerClientId, Stream dataStream, bool readNetworkVariable, bool destroyWithScene)
286286
{
287287
if (networkObject == null)
288288
{
@@ -350,24 +350,10 @@ internal void SpawnNetworkObjectLocally(NetworkObject networkObject, ulong netwo
350350
networkObject.SetCachedParent(networkObject.transform.parent);
351351
networkObject.ApplyNetworkParenting();
352352
NetworkObject.CheckOrphanChildren();
353-
354-
if (readPayload)
355-
{
356-
using (var payloadBuffer = PooledNetworkBuffer.Get())
357-
{
358-
payloadBuffer.CopyUnreadFrom(dataStream, payloadLength);
359-
dataStream.Position += payloadLength;
360-
payloadBuffer.Position = 0;
361-
networkObject.InvokeBehaviourNetworkSpawn(payloadBuffer);
362-
}
363-
}
364-
else
365-
{
366-
networkObject.InvokeBehaviourNetworkSpawn(null);
367-
}
353+
networkObject.InvokeBehaviourNetworkSpawn();
368354
}
369355

370-
internal void SendSpawnCallForObject(ulong clientId, ulong ownerClientId, NetworkObject networkObject, Stream payload)
356+
internal void SendSpawnCallForObject(ulong clientId, ulong ownerClientId, NetworkObject networkObject)
371357
{
372358
//Currently, if this is called and the clientId (destination) is the server's client Id, this case
373359
//will be checked within the below Send function. To avoid unwarranted allocation of a PooledNetworkBuffer
@@ -386,12 +372,12 @@ internal void SendSpawnCallForObject(ulong clientId, ulong ownerClientId, Networ
386372
{
387373
using (var nonNullContext = (InternalCommandContext)context)
388374
{
389-
WriteSpawnCallForObject(nonNullContext.NetworkWriter, ownerClientId, networkObject, payload);
375+
WriteSpawnCallForObject(nonNullContext.NetworkWriter, ownerClientId, networkObject);
390376
}
391377
}
392378
}
393379

394-
internal void WriteSpawnCallForObject(PooledNetworkWriter writer, ulong clientId, NetworkObject networkObject, Stream payload)
380+
internal void WriteSpawnCallForObject(PooledNetworkWriter writer, ulong clientId, NetworkObject networkObject)
395381
{
396382
writer.WriteBool(networkObject.IsPlayerObject);
397383
writer.WriteUInt64Packed(networkObject.NetworkObjectId);
@@ -437,24 +423,10 @@ internal void WriteSpawnCallForObject(PooledNetworkWriter writer, ulong clientId
437423
var (isReparented, latestParent) = networkObject.GetNetworkParenting();
438424
NetworkObject.WriteNetworkParenting(writer, isReparented, latestParent);
439425
}
440-
441-
442-
writer.WriteBool(payload != null);
443-
444-
if (payload != null)
445-
{
446-
writer.WriteInt32Packed((int)payload.Length);
447-
}
448-
449426
if (NetworkManager.NetworkConfig.EnableNetworkVariable)
450427
{
451428
networkObject.WriteNetworkVariableData(writer.GetStream(), clientId);
452429
}
453-
454-
if (payload != null)
455-
{
456-
payload.CopyTo(writer.GetStream());
457-
}
458430
}
459431

460432
internal void DespawnObject(NetworkObject networkObject, bool destroyObject = false)
@@ -583,7 +555,7 @@ internal void ServerSpawnSceneObjectsOnStartSweep()
583555
{
584556
if (networkObjects[i].IsSceneObject == null)
585557
{
586-
SpawnNetworkObjectLocally(networkObjects[i], GetNetworkObjectId(), true, false, null, null, false, 0, false, true);
558+
SpawnNetworkObjectLocally(networkObjects[i], GetNetworkObjectId(), true, false, null, null, false, true);
587559
}
588560
}
589561
}

testproject/Assets/Tests/Manual/Scripts/NetworkPrefabPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ private IEnumerator SpawnObjects()
350350
var no = go.GetComponent<NetworkObject>();
351351
if (!no.IsSpawned)
352352
{
353-
no.Spawn(null, true);
353+
no.Spawn(true);
354354
}
355355
}
356356
}

testproject/Assets/Tests/Manual/Scripts/StatsDisplay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private void OnClientConnectedCallback(ulong clientId)
6666
var networkObject = GetComponent<NetworkObject>();
6767
if (networkObject != null)
6868
{
69-
networkObject.SpawnWithOwnership(clientId, null, true);
69+
networkObject.SpawnWithOwnership(clientId, true);
7070
}
7171
}
7272
}

0 commit comments

Comments
 (0)