Skip to content

refactor: Refactor spawncall and improve error handling for spawning #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 26 additions & 33 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using MLAPI.Configuration;
using MLAPI.Exceptions;
using MLAPI.Hashing;
Expand Down Expand Up @@ -366,21 +367,22 @@ private void OnDestroy()
}
}

/// <summary>
/// Spawns this GameObject across the network. Can only be called from the Server
/// </summary>
/// <param name="spawnPayload">The writer containing the spawn payload</param>
/// <param name="destroyWithScene">Should the object be destroyd when the scene is changed</param>
public void Spawn(Stream spawnPayload = null, bool destroyWithScene = false)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SpawnInternal(Stream spawnPayload, bool destroyWithScene , ulong? ownerClientId, bool playerObject)
{
if (!NetworkManager.Singleton.IsListening)
{
throw new NotListeningException($"{nameof(NetworkManager)} isn't listening, start a server, client or host before spawning objects.");
throw new NotListeningException($"{nameof(NetworkManager)} isn't listening, start a server or host before spawning objects.");
}

if (!NetworkManager.Singleton.IsServer)
{
throw new NotServerException($"Only server can spawn {nameof(NetworkObject)}s");
}

if (spawnPayload != null) spawnPayload.Position = 0;

NetworkSpawnManager.SpawnNetworkObjectLocally(this, NetworkSpawnManager.GetNetworkObjectId(), false, false, null, spawnPayload, spawnPayload != null, spawnPayload == null ? 0 : (int)spawnPayload.Length, false, destroyWithScene);
NetworkSpawnManager.SpawnNetworkObjectLocally(this, NetworkSpawnManager.GetNetworkObjectId(), false, playerObject, ownerClientId, spawnPayload, spawnPayload != null, spawnPayload == null ? 0 : (int)spawnPayload.Length, false, destroyWithScene);

for (int i = 0; i < NetworkManager.Singleton.ConnectedClientsList.Count; i++)
{
Expand All @@ -392,11 +394,13 @@ public void Spawn(Stream spawnPayload = null, bool destroyWithScene = false)
}

/// <summary>
/// Despawns this GameObject and destroys it for other clients. This should be used if the object should be kept on the server
/// Spawns this GameObject across the network. Can only be called from the Server
/// </summary>
public void Despawn(bool destroy = false)
/// <param name="spawnPayload">The writer containing the spawn payload</param>
/// <param name="destroyWithScene">Should the object be destroyd when the scene is changed</param>
public void Spawn(Stream spawnPayload = null, bool destroyWithScene = false)
{
NetworkSpawnManager.DespawnObject(this, destroy);
SpawnInternal(spawnPayload, destroyWithScene, null, false);
}

/// <summary>
Expand All @@ -407,17 +411,7 @@ public void Despawn(bool destroy = false)
/// <param name="destroyWithScene">Should the object be destroyd when the scene is changed</param>
public void SpawnWithOwnership(ulong clientId, Stream spawnPayload = null, bool destroyWithScene = false)
{
if (spawnPayload != null) spawnPayload.Position = 0;

NetworkSpawnManager.SpawnNetworkObjectLocally(this, NetworkSpawnManager.GetNetworkObjectId(), false, false, clientId, spawnPayload, spawnPayload != null, spawnPayload == null ? 0 : (int)spawnPayload.Length, false, destroyWithScene);

for (int i = 0; i < NetworkManager.Singleton.ConnectedClientsList.Count; i++)
{
if (m_Observers.Contains(NetworkManager.Singleton.ConnectedClientsList[i].ClientId))
{
NetworkSpawnManager.SendSpawnCallForObject(NetworkManager.Singleton.ConnectedClientsList[i].ClientId, this, spawnPayload);
}
}
SpawnInternal(spawnPayload, destroyWithScene, clientId, false);
}

/// <summary>
Expand All @@ -428,19 +422,18 @@ public void SpawnWithOwnership(ulong clientId, Stream spawnPayload = null, bool
/// <param name="destroyWithScene">Should the object be destroyd when the scene is changed</param>
public void SpawnAsPlayerObject(ulong clientId, Stream spawnPayload = null, bool destroyWithScene = false)
{
if (spawnPayload != null) spawnPayload.Position = 0;

NetworkSpawnManager.SpawnNetworkObjectLocally(this, NetworkSpawnManager.GetNetworkObjectId(), false, true, clientId, spawnPayload, spawnPayload != null, spawnPayload == null ? 0 : (int)spawnPayload.Length, false, destroyWithScene);
SpawnInternal(spawnPayload, destroyWithScene, clientId, true);
}

for (int i = 0; i < NetworkManager.Singleton.ConnectedClientsList.Count; i++)
{
if (m_Observers.Contains(NetworkManager.Singleton.ConnectedClientsList[i].ClientId))
{
NetworkSpawnManager.SendSpawnCallForObject(NetworkManager.Singleton.ConnectedClientsList[i].ClientId, this, spawnPayload);
}
}
/// <summary>
/// Despawns this GameObject and destroys it for other clients. This should be used if the object should be kept on the server
/// </summary>
public void Despawn(bool destroy = false)
{
NetworkSpawnManager.DespawnObject(this, destroy);
}


/// <summary>
/// Removes all ownership of an object from any client. Can only be called from server
/// </summary>
Expand Down Expand Up @@ -575,4 +568,4 @@ internal NetworkBehaviour GetNetworkBehaviourAtOrderIndex(ushort index)
return ChildNetworkBehaviours[index];
}
}
}
}