/// <summary> /// Spawns the <paramref name="identity"/> and assigns <paramref name="owner"/> to be it's owner /// </summary> public void Spawn(NetworkIdentity identity, INetworkPlayer owner) { if (!Server || !Server.Active) { throw new InvalidOperationException("NetworkServer is not active. Cannot spawn objects without an active server."); } ThrowIfPrefab(identity.gameObject); identity.Owner = owner; identity.SetServerValues(Server, this); // special case to make sure hasAuthority is set // on start server in host mode if (owner == Server.LocalPlayer) { identity.HasAuthority = true; } if (!identity.IsSpawned) { // the object has not been spawned yet identity.NetId = GetNextNetworkId(); identity.StartServer(); Server.World.AddIdentity(identity.NetId, identity); } if (logger.LogEnabled()) { logger.Log($"SpawnObject instance ID {identity.NetId} asset ID {identity.PrefabHash:X}"); } identity.RebuildObservers(true); }
public void RebuildObserversOnlyAddsOwnPlayerIfReady() { // add at least one observers component, otherwise it will just add // all server connections gameObject.AddComponent <RebuildEmptyObserversNetworkBehaviour>(); // add own player connection that isn't ready (_, NetworkConnection connection) = PipedConnections(); identity.ConnectionToClient = connection; // call OnStartServer so that observers dict is created identity.StartServer(); // rebuild shouldn't add own player because conn wasn't set ready identity.RebuildObservers(true); Assert.That(identity.observers, Does.Not.Contains(identity.ConnectionToClient)); }
internal void SpawnObject(GameObject obj, INetworkConnection ownerConnection) { if (!Server || !Server.Active) { throw new InvalidOperationException("SpawnObject for " + obj + ", NetworkServer is not active. Cannot spawn objects without an active server."); } NetworkIdentity identity = obj.GetComponent <NetworkIdentity>(); if (identity is null) { throw new InvalidOperationException("SpawnObject " + obj + " has no NetworkIdentity. Please add a NetworkIdentity to " + obj); } identity.ConnectionToClient = ownerConnection; identity.Server = Server; identity.ServerObjectManager = this; identity.Client = Server.LocalClient; // special case to make sure hasAuthority is set // on start server in host mode if (ownerConnection == Server.LocalConnection) { identity.HasAuthority = true; } if (identity.NetId == 0) { // the object has not been spawned yet identity.NetId = GetNextNetworkId(); SpawnedObjects[identity.NetId] = identity; identity.StartServer(); Spawned.Invoke(identity); } if (logger.LogEnabled()) { logger.Log("SpawnObject instance ID " + identity.NetId + " asset ID " + identity.AssetId); } identity.RebuildObservers(true); }
internal void SpawnObject(GameObject obj, INetworkPlayer ownerPlayer) { if (!Server || !Server.Active) { throw new InvalidOperationException("NetworkServer is not active. Cannot spawn objects without an active server."); } NetworkIdentity identity = obj.GetComponent <NetworkIdentity>(); if (identity is null) { throw new InvalidOperationException("SpawnObject " + obj + " has no NetworkIdentity. Please add a NetworkIdentity to " + obj); } identity.ConnectionToClient = ownerPlayer; identity.SetServerValues(Server, this); // special case to make sure hasAuthority is set // on start server in host mode if (ownerPlayer == Server.LocalPlayer) { identity.HasAuthority = true; } if (identity.NetId == 0) { // the object has not been spawned yet identity.NetId = GetNextNetworkId(); identity.StartServer(); Server.World.AddIdentity(identity.NetId, identity); } if (logger.LogEnabled()) { logger.Log("SpawnObject instance ID " + identity.NetId + " asset ID " + identity.AssetId); } identity.RebuildObservers(true); }