Changed various networkedObject behaviour to ensure isSpawned is set
This commit is contained in:
parent
879c54b38f
commit
b6e36b0001
@ -9,7 +9,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// A component used to identify that a GameObject is networked
|
||||
/// </summary>
|
||||
[AddComponentMenu("MLAPI/NetworkedObject", -99)]
|
||||
public class NetworkedObject : MonoBehaviour
|
||||
public sealed class NetworkedObject : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the unique ID of this object that is synced across the network
|
||||
@ -62,6 +62,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <summary>
|
||||
/// Gets or sets if this object should be replicated across the network. Can only be changed before the object is spawned
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public bool ServerOnly = false;
|
||||
/// <summary>
|
||||
/// Gets if this object is part of a pool
|
||||
@ -108,13 +109,24 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets if the object has yet been spawned across the network
|
||||
/// </summary>
|
||||
public bool isSpawned
|
||||
{
|
||||
get
|
||||
{
|
||||
return _isSpawned;
|
||||
}
|
||||
}
|
||||
internal bool _isSpawned = false;
|
||||
internal bool sceneObject = false;
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
SpawnManager.OnDestroyObject(NetworkId, false);
|
||||
}
|
||||
|
||||
internal bool isSpawned = false;
|
||||
|
||||
/// <summary>
|
||||
/// Spawns this GameObject across the network. Can only be called from the Server
|
||||
/// </summary>
|
||||
|
@ -80,8 +80,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
}
|
||||
}
|
||||
internal HashSet<int> pendingClients;
|
||||
internal bool isServer;
|
||||
internal bool isClient;
|
||||
internal bool _isServer;
|
||||
internal bool _isClient;
|
||||
/// <summary>
|
||||
/// Gets if we are running as host
|
||||
/// </summary>
|
||||
@ -92,6 +92,23 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
return isServer && isClient;
|
||||
}
|
||||
}
|
||||
|
||||
public bool isClient
|
||||
{
|
||||
get
|
||||
{
|
||||
return _isClient;
|
||||
}
|
||||
}
|
||||
|
||||
public bool isServer
|
||||
{
|
||||
get
|
||||
{
|
||||
return _isServer;
|
||||
}
|
||||
}
|
||||
|
||||
private bool isListening;
|
||||
private byte[] messageBuffer;
|
||||
internal int serverClientId;
|
||||
@ -192,6 +209,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
{
|
||||
uint networkId = SpawnManager.GetNetworkObjectId();
|
||||
SpawnManager.spawnedObjects.Add(networkId, sceneObjects[i]);
|
||||
sceneObjects[i]._isSpawned = true;
|
||||
sceneObjects[i].sceneObject = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -287,8 +306,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
}
|
||||
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
|
||||
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port);
|
||||
isServer = true;
|
||||
isClient = false;
|
||||
_isServer = true;
|
||||
_isClient = false;
|
||||
isListening = true;
|
||||
|
||||
if (OnServerStarted != null)
|
||||
@ -305,8 +324,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
|
||||
hostId = NetworkTransport.AddHost(hostTopology, 0, null);
|
||||
|
||||
isServer = false;
|
||||
isClient = true;
|
||||
_isServer = false;
|
||||
_isClient = true;
|
||||
isListening = true;
|
||||
serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
|
||||
}
|
||||
@ -334,6 +353,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
NetworkTransport.Disconnect(hostId, clientId, out error);
|
||||
}
|
||||
}
|
||||
_isServer = false;
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
@ -342,6 +362,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// </summary>
|
||||
public void StopHost()
|
||||
{
|
||||
_isClient = false;
|
||||
_isServer = false;
|
||||
StopServer();
|
||||
//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
|
||||
}
|
||||
@ -351,6 +373,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// </summary>
|
||||
public void StopClient()
|
||||
{
|
||||
_isClient = false;
|
||||
NetworkTransport.Disconnect(hostId, serverClientId, out error);
|
||||
Shutdown();
|
||||
}
|
||||
@ -371,8 +394,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
}
|
||||
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
|
||||
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null);
|
||||
isServer = true;
|
||||
isClient = true;
|
||||
_isServer = true;
|
||||
_isClient = true;
|
||||
isListening = true;
|
||||
connectedClients.Add(-1, new NetworkedClient() { ClientId = -1 });
|
||||
if(NetworkConfig.HandleObjectSpawning)
|
||||
@ -408,8 +431,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
private void Shutdown()
|
||||
{
|
||||
isListening = false;
|
||||
isClient = false;
|
||||
isServer = false;
|
||||
_isClient = false;
|
||||
_isServer = false;
|
||||
NetworkTransport.Shutdown();
|
||||
}
|
||||
|
||||
@ -752,7 +775,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
}
|
||||
if(NetworkConfig.HandleObjectSpawning)
|
||||
{
|
||||
SpawnManager.DestroyUnspawnedObjects();
|
||||
SpawnManager.DestroySceneObjects();
|
||||
int objectCount = messageReader.ReadInt32();
|
||||
for (int i = 0; i < objectCount; i++)
|
||||
{
|
||||
|
@ -65,12 +65,12 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
}
|
||||
}
|
||||
|
||||
internal static void DestroyUnspawnedObjects()
|
||||
internal static void DestroySceneObjects()
|
||||
{
|
||||
NetworkedObject[] netObjects = MonoBehaviour.FindObjectsOfType<NetworkedObject>();
|
||||
for (int i = 0; i < netObjects.Length; i++)
|
||||
{
|
||||
if (!netObjects[i].isSpawned)
|
||||
if (netObjects[i].sceneObject)
|
||||
MonoBehaviour.Destroy(netObjects[i].gameObject);
|
||||
}
|
||||
}
|
||||
@ -96,6 +96,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
netObject.ownerClientId = ownerId;
|
||||
netObject.transform.position = position;
|
||||
netObject.transform.rotation = rotation;
|
||||
netObject._isSpawned = true;
|
||||
|
||||
spawnedObjects.Add(netObject.NetworkId, netObject);
|
||||
netObject.InvokeBehaviourNetworkSpawn();
|
||||
@ -121,6 +122,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
netObject.networkId = networkId;
|
||||
}
|
||||
netObject._isPlayerObject = true;
|
||||
netObject._isSpawned = true;
|
||||
netManager.connectedClients[clientId].PlayerObject = go;
|
||||
spawnedObjects.Add(netObject.NetworkId, netObject);
|
||||
netObject.InvokeBehaviourNetworkSpawn();
|
||||
@ -191,7 +193,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
uint netId = GetNetworkObjectId();
|
||||
netObject.networkId = netId;
|
||||
spawnedObjects.Add(netId, netObject);
|
||||
netObject.isSpawned = true;
|
||||
netObject._isSpawned = true;
|
||||
if (clientOwnerId != null)
|
||||
{
|
||||
netObject.ownerClientId = clientOwnerId.Value;
|
||||
|
Loading…
x
Reference in New Issue
Block a user