Changed various networkedObject behaviour to ensure isSpawned is set

This commit is contained in:
Albin Corén 2018-04-02 08:31:01 +02:00
parent 879c54b38f
commit b6e36b0001
3 changed files with 54 additions and 17 deletions

View File

@ -9,7 +9,7 @@ namespace MLAPI.MonoBehaviours.Core
/// A component used to identify that a GameObject is networked /// A component used to identify that a GameObject is networked
/// </summary> /// </summary>
[AddComponentMenu("MLAPI/NetworkedObject", -99)] [AddComponentMenu("MLAPI/NetworkedObject", -99)]
public class NetworkedObject : MonoBehaviour public sealed class NetworkedObject : MonoBehaviour
{ {
/// <summary> /// <summary>
/// Gets the unique ID of this object that is synced across the network /// Gets the unique ID of this object that is synced across the network
@ -62,6 +62,7 @@ namespace MLAPI.MonoBehaviours.Core
/// <summary> /// <summary>
/// Gets or sets if this object should be replicated across the network. Can only be changed before the object is spawned /// Gets or sets if this object should be replicated across the network. Can only be changed before the object is spawned
/// </summary> /// </summary>
[SerializeField]
public bool ServerOnly = false; public bool ServerOnly = false;
/// <summary> /// <summary>
/// Gets if this object is part of a pool /// 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() private void OnDestroy()
{ {
SpawnManager.OnDestroyObject(NetworkId, false); SpawnManager.OnDestroyObject(NetworkId, false);
} }
internal bool isSpawned = false;
/// <summary> /// <summary>
/// Spawns this GameObject across the network. Can only be called from the Server /// Spawns this GameObject across the network. Can only be called from the Server
/// </summary> /// </summary>

View File

@ -80,8 +80,8 @@ namespace MLAPI.MonoBehaviours.Core
} }
} }
internal HashSet<int> pendingClients; internal HashSet<int> pendingClients;
internal bool isServer; internal bool _isServer;
internal bool isClient; internal bool _isClient;
/// <summary> /// <summary>
/// Gets if we are running as host /// Gets if we are running as host
/// </summary> /// </summary>
@ -92,6 +92,23 @@ namespace MLAPI.MonoBehaviours.Core
return isServer && isClient; return isServer && isClient;
} }
} }
public bool isClient
{
get
{
return _isClient;
}
}
public bool isServer
{
get
{
return _isServer;
}
}
private bool isListening; private bool isListening;
private byte[] messageBuffer; private byte[] messageBuffer;
internal int serverClientId; internal int serverClientId;
@ -192,6 +209,8 @@ namespace MLAPI.MonoBehaviours.Core
{ {
uint networkId = SpawnManager.GetNetworkObjectId(); uint networkId = SpawnManager.GetNetworkObjectId();
SpawnManager.spawnedObjects.Add(networkId, sceneObjects[i]); 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); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port); hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port);
isServer = true; _isServer = true;
isClient = false; _isClient = false;
isListening = true; isListening = true;
if (OnServerStarted != null) if (OnServerStarted != null)
@ -305,8 +324,8 @@ namespace MLAPI.MonoBehaviours.Core
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
hostId = NetworkTransport.AddHost(hostTopology, 0, null); hostId = NetworkTransport.AddHost(hostTopology, 0, null);
isServer = false; _isServer = false;
isClient = true; _isClient = true;
isListening = true; isListening = true;
serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error); 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); NetworkTransport.Disconnect(hostId, clientId, out error);
} }
} }
_isServer = false;
Shutdown(); Shutdown();
} }
@ -342,6 +362,8 @@ namespace MLAPI.MonoBehaviours.Core
/// </summary> /// </summary>
public void StopHost() public void StopHost()
{ {
_isClient = false;
_isServer = false;
StopServer(); 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 //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> /// </summary>
public void StopClient() public void StopClient()
{ {
_isClient = false;
NetworkTransport.Disconnect(hostId, serverClientId, out error); NetworkTransport.Disconnect(hostId, serverClientId, out error);
Shutdown(); Shutdown();
} }
@ -371,8 +394,8 @@ namespace MLAPI.MonoBehaviours.Core
} }
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null); hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null);
isServer = true; _isServer = true;
isClient = true; _isClient = true;
isListening = true; isListening = true;
connectedClients.Add(-1, new NetworkedClient() { ClientId = -1 }); connectedClients.Add(-1, new NetworkedClient() { ClientId = -1 });
if(NetworkConfig.HandleObjectSpawning) if(NetworkConfig.HandleObjectSpawning)
@ -408,8 +431,8 @@ namespace MLAPI.MonoBehaviours.Core
private void Shutdown() private void Shutdown()
{ {
isListening = false; isListening = false;
isClient = false; _isClient = false;
isServer = false; _isServer = false;
NetworkTransport.Shutdown(); NetworkTransport.Shutdown();
} }
@ -752,7 +775,7 @@ namespace MLAPI.MonoBehaviours.Core
} }
if(NetworkConfig.HandleObjectSpawning) if(NetworkConfig.HandleObjectSpawning)
{ {
SpawnManager.DestroyUnspawnedObjects(); SpawnManager.DestroySceneObjects();
int objectCount = messageReader.ReadInt32(); int objectCount = messageReader.ReadInt32();
for (int i = 0; i < objectCount; i++) for (int i = 0; i < objectCount; i++)
{ {

View File

@ -65,12 +65,12 @@ namespace MLAPI.NetworkingManagerComponents.Core
} }
} }
internal static void DestroyUnspawnedObjects() internal static void DestroySceneObjects()
{ {
NetworkedObject[] netObjects = MonoBehaviour.FindObjectsOfType<NetworkedObject>(); NetworkedObject[] netObjects = MonoBehaviour.FindObjectsOfType<NetworkedObject>();
for (int i = 0; i < netObjects.Length; i++) for (int i = 0; i < netObjects.Length; i++)
{ {
if (!netObjects[i].isSpawned) if (netObjects[i].sceneObject)
MonoBehaviour.Destroy(netObjects[i].gameObject); MonoBehaviour.Destroy(netObjects[i].gameObject);
} }
} }
@ -96,6 +96,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
netObject.ownerClientId = ownerId; netObject.ownerClientId = ownerId;
netObject.transform.position = position; netObject.transform.position = position;
netObject.transform.rotation = rotation; netObject.transform.rotation = rotation;
netObject._isSpawned = true;
spawnedObjects.Add(netObject.NetworkId, netObject); spawnedObjects.Add(netObject.NetworkId, netObject);
netObject.InvokeBehaviourNetworkSpawn(); netObject.InvokeBehaviourNetworkSpawn();
@ -121,6 +122,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
netObject.networkId = networkId; netObject.networkId = networkId;
} }
netObject._isPlayerObject = true; netObject._isPlayerObject = true;
netObject._isSpawned = true;
netManager.connectedClients[clientId].PlayerObject = go; netManager.connectedClients[clientId].PlayerObject = go;
spawnedObjects.Add(netObject.NetworkId, netObject); spawnedObjects.Add(netObject.NetworkId, netObject);
netObject.InvokeBehaviourNetworkSpawn(); netObject.InvokeBehaviourNetworkSpawn();
@ -191,7 +193,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
uint netId = GetNetworkObjectId(); uint netId = GetNetworkObjectId();
netObject.networkId = netId; netObject.networkId = netId;
spawnedObjects.Add(netId, netObject); spawnedObjects.Add(netId, netObject);
netObject.isSpawned = true; netObject._isSpawned = true;
if (clientOwnerId != null) if (clientOwnerId != null)
{ {
netObject.ownerClientId = clientOwnerId.Value; netObject.ownerClientId = clientOwnerId.Value;