Skip to content

Commit b6e36b0

Browse files
committed
Changed various networkedObject behaviour to ensure isSpawned is set
1 parent 879c54b commit b6e36b0

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace MLAPI.MonoBehaviours.Core
99
/// A component used to identify that a GameObject is networked
1010
/// </summary>
1111
[AddComponentMenu("MLAPI/NetworkedObject", -99)]
12-
public class NetworkedObject : MonoBehaviour
12+
public sealed class NetworkedObject : MonoBehaviour
1313
{
1414
/// <summary>
1515
/// Gets the unique ID of this object that is synced across the network
@@ -62,6 +62,7 @@ public bool isPlayerObject
6262
/// <summary>
6363
/// Gets or sets if this object should be replicated across the network. Can only be changed before the object is spawned
6464
/// </summary>
65+
[SerializeField]
6566
public bool ServerOnly = false;
6667
/// <summary>
6768
/// Gets if this object is part of a pool
@@ -108,13 +109,24 @@ public bool isOwner
108109
}
109110
}
110111

112+
/// <summary>
113+
/// Gets if the object has yet been spawned across the network
114+
/// </summary>
115+
public bool isSpawned
116+
{
117+
get
118+
{
119+
return _isSpawned;
120+
}
121+
}
122+
internal bool _isSpawned = false;
123+
internal bool sceneObject = false;
124+
111125
private void OnDestroy()
112126
{
113127
SpawnManager.OnDestroyObject(NetworkId, false);
114128
}
115129

116-
internal bool isSpawned = false;
117-
118130
/// <summary>
119131
/// Spawns this GameObject across the network. Can only be called from the Server
120132
/// </summary>

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public Dictionary<int, NetworkedClient> ConnectedClients
8080
}
8181
}
8282
internal HashSet<int> pendingClients;
83-
internal bool isServer;
84-
internal bool isClient;
83+
internal bool _isServer;
84+
internal bool _isClient;
8585
/// <summary>
8686
/// Gets if we are running as host
8787
/// </summary>
@@ -92,6 +92,23 @@ public bool isHost
9292
return isServer && isClient;
9393
}
9494
}
95+
96+
public bool isClient
97+
{
98+
get
99+
{
100+
return _isClient;
101+
}
102+
}
103+
104+
public bool isServer
105+
{
106+
get
107+
{
108+
return _isServer;
109+
}
110+
}
111+
95112
private bool isListening;
96113
private byte[] messageBuffer;
97114
internal int serverClientId;
@@ -192,6 +209,8 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
192209
{
193210
uint networkId = SpawnManager.GetNetworkObjectId();
194211
SpawnManager.spawnedObjects.Add(networkId, sceneObjects[i]);
212+
sceneObjects[i]._isSpawned = true;
213+
sceneObjects[i].sceneObject = true;
195214
}
196215
}
197216

@@ -287,8 +306,8 @@ public void StartServer(NetworkingConfiguration netConfig)
287306
}
288307
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
289308
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port);
290-
isServer = true;
291-
isClient = false;
309+
_isServer = true;
310+
_isClient = false;
292311
isListening = true;
293312

294313
if (OnServerStarted != null)
@@ -305,8 +324,8 @@ public void StartClient(NetworkingConfiguration netConfig)
305324
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
306325
hostId = NetworkTransport.AddHost(hostTopology, 0, null);
307326

308-
isServer = false;
309-
isClient = true;
327+
_isServer = false;
328+
_isClient = true;
310329
isListening = true;
311330
serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
312331
}
@@ -334,6 +353,7 @@ public void StopServer()
334353
NetworkTransport.Disconnect(hostId, clientId, out error);
335354
}
336355
}
356+
_isServer = false;
337357
Shutdown();
338358
}
339359

@@ -342,6 +362,8 @@ public void StopServer()
342362
/// </summary>
343363
public void StopHost()
344364
{
365+
_isClient = false;
366+
_isServer = false;
345367
StopServer();
346368
//We don't stop client since we dont actually have a transport connection to our own host. We just handle host messages directly in the MLAPI
347369
}
@@ -351,6 +373,7 @@ public void StopHost()
351373
/// </summary>
352374
public void StopClient()
353375
{
376+
_isClient = false;
354377
NetworkTransport.Disconnect(hostId, serverClientId, out error);
355378
Shutdown();
356379
}
@@ -371,8 +394,8 @@ public void StartHost(NetworkingConfiguration netConfig)
371394
}
372395
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
373396
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null);
374-
isServer = true;
375-
isClient = true;
397+
_isServer = true;
398+
_isClient = true;
376399
isListening = true;
377400
connectedClients.Add(-1, new NetworkedClient() { ClientId = -1 });
378401
if(NetworkConfig.HandleObjectSpawning)
@@ -408,8 +431,8 @@ private void OnDestroy()
408431
private void Shutdown()
409432
{
410433
isListening = false;
411-
isClient = false;
412-
isServer = false;
434+
_isClient = false;
435+
_isServer = false;
413436
NetworkTransport.Shutdown();
414437
}
415438

@@ -752,7 +775,7 @@ private void HandleIncomingData(int clientId, byte[] data, int channelId)
752775
}
753776
if(NetworkConfig.HandleObjectSpawning)
754777
{
755-
SpawnManager.DestroyUnspawnedObjects();
778+
SpawnManager.DestroySceneObjects();
756779
int objectCount = messageReader.ReadInt32();
757780
for (int i = 0; i < objectCount; i++)
758781
{

MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ internal static void ChangeOwnership(uint netId, int clientId)
6565
}
6666
}
6767

68-
internal static void DestroyUnspawnedObjects()
68+
internal static void DestroySceneObjects()
6969
{
7070
NetworkedObject[] netObjects = MonoBehaviour.FindObjectsOfType<NetworkedObject>();
7171
for (int i = 0; i < netObjects.Length; i++)
7272
{
73-
if (!netObjects[i].isSpawned)
73+
if (netObjects[i].sceneObject)
7474
MonoBehaviour.Destroy(netObjects[i].gameObject);
7575
}
7676
}
@@ -96,6 +96,7 @@ internal static GameObject SpawnObject(int spawnablePrefabIndex, uint networkId,
9696
netObject.ownerClientId = ownerId;
9797
netObject.transform.position = position;
9898
netObject.transform.rotation = rotation;
99+
netObject._isSpawned = true;
99100

100101
spawnedObjects.Add(netObject.NetworkId, netObject);
101102
netObject.InvokeBehaviourNetworkSpawn();
@@ -121,6 +122,7 @@ internal static GameObject SpawnPlayerObject(int clientId, uint networkId)
121122
netObject.networkId = networkId;
122123
}
123124
netObject._isPlayerObject = true;
125+
netObject._isSpawned = true;
124126
netManager.connectedClients[clientId].PlayerObject = go;
125127
spawnedObjects.Add(netObject.NetworkId, netObject);
126128
netObject.InvokeBehaviourNetworkSpawn();
@@ -191,7 +193,7 @@ internal static void OnSpawnObject(NetworkedObject netObject, int? clientOwnerId
191193
uint netId = GetNetworkObjectId();
192194
netObject.networkId = netId;
193195
spawnedObjects.Add(netId, netObject);
194-
netObject.isSpawned = true;
196+
netObject._isSpawned = true;
195197
if (clientOwnerId != null)
196198
{
197199
netObject.ownerClientId = clientOwnerId.Value;

0 commit comments

Comments
 (0)