/// <summary> /// returns the next available ID, and records which object it's attached to /// </summary> /// <returns>next available id</returns> /// <param name="id">Identifier.</param> internal NetworkID GetNextID(NetObjectID id) { // find and assign appropriate object id if (Application.isPlaying) { for (int i = 0; i < gameIDs.Length; i++) { if (gameIDs[i].right == false) { gameIDs[i].left = id; gameIDs[i].right = true; return(new NetworkID((byte)i, NetworkIDType.game)); } } } else { for (int i = 0; i < sceneIDs.Length; i++) { if (sceneIDs[i].right == false) { sceneIDs[i].left = id; sceneIDs[i].right = true; return(new NetworkID((byte)i, NetworkIDType.scene)); } } } throw new System.InvalidOperationException("No more IDs available"); }
/// <summary> /// Destroys a network object. To be used in conjunction with NetworkInstantiate /// </summary> /// <param name="obj"></param> public void NetworkDestroy(NetObjectID obj) { NetworkID id = obj.netID; if (id.type != NetworkIDType.game) { Debug.LogError("Tried to destroy an ID that was not a game ID"); return; } // remove all buffered messages assigned to this object foreach (BufferedMessage msg in obj.bufferedMessages) { bufferedMessages.Remove(msg); } // create a destroy event for all clients NetWriter writer = StartNetworkMessage("cl_NetworkDestroy", thisNetworkID); id.WriteBytes(writer); // notify all clients to destroy this object SendMessageToOtherClients(writer, NetworkCore.AllCostMsg, false); // finally destroy this object Destroy(obj.gameObject); // unregister this ID gameIDs[id.idNumber].left = null; gameIDs[id.idNumber].right = false; }
public void REC_NewLocalPlayer(NetReader reader) { int playerNumber = reader.ReadInt(); NetworkID vehicleID = NetworkID.GetDataFromBytes(reader); // create local player mapping LocalPlayer newPlayer = new LocalPlayer(); newPlayer.playerNumber = playerNumber; newPlayer.vehicleID = vehicleID; Debug.Log(vehicleID); // TODO assign the vehicle NetObjectID id = NetworkManager.GetObjectForID(vehicleID); Debug.Log(id); BaseCar car = id.GetComponent <BaseCar>(); // assign our vehicle to follow Debug.Log("Got local player info from server!"); localPlayers.Add(newPlayer); Camera.main.GetComponent <RaceCamera>().SetCameraTarget(car.thisTransform); HasClient = true; }
/// <summary> /// Frees the network object ID, and unlinks it from the system. /// THIS METHOD DOES NOT DESTROY THE OBJECT. /// </summary> /// <param name="newID"></param> /// <returns>true if the id was removed, false otherwise</returns> public bool FreeNetworkID(NetObjectID newID) { if (newID.netID.type != NetworkIDType.game) { Debug.LogError("Tried to free a network ID that wasn't a game allocated one. Aborting."); return(false); } int num = newID.netID.idNumber; gameIDs[num].right = false; gameIDs[num].left = null; return(true); }
public void CL_NetworkInstantiate(NetReader reader) { int prefabID = reader.ReadInt(); Vector3 pos = reader.ReadVector3(); Vector3 euler = reader.ReadVector3(); Quaternion rot = Quaternion.Euler(euler); GameObject newObj = (GameObject)Instantiate(networkPrefabs[prefabID].clientPrefab, pos, rot); NetObjectID idComp = newObj.GetComponent <NetObjectID>(); NetworkID newID = NetworkID.GetDataFromBytes(reader); // assign the RegisterObjectAndID(newID, idComp); newObj.BroadcastMessage("OnNewNetworkSceneLoaded"); }
public void CL_NetworkDestroy(NetReader reader) { NetworkID idToDestroy = NetworkID.GetDataFromBytes(reader); if (idToDestroy.type == NetworkIDType.game) { NetObjectID objId = gameIDs[idToDestroy.idNumber].left; Destroy(objId.gameObject); gameIDs[idToDestroy.idNumber].left = null; gameIDs[idToDestroy.idNumber].right = false; } else { Debug.LogError("Tried to destroy a network ID that wasn't a game allocated one. " + idToDestroy); } }
private void RegisterObjectAndID(NetworkID newID, NetObjectID idComp) { int idNum = newID.idNumber; if (newID.type == NetworkIDType.game) { Debug.Assert(gameIDs[idNum].right == false && gameIDs[idNum].left == null, "Instantiated Game ID already exists! overwriting..."); gameIDs[idNum].left = idComp; gameIDs[idNum].right = true; idComp.SetNetID(newID); } else { Debug.LogError("Instantiate with invalid ID provided!!!"); } }
public NetworkID MakeTransientID(NetObjectID objectID, bool unlinkSceneIDs) { if (objectID.netID.type != NetworkIDType.scene) { Debug.LogError("Tried to make non-scene id " + objectID.netID + " transient."); return(objectID.netID); } NetworkID oldID = objectID.netID; // allocate a new game id NetworkID newID = GetNextID(objectID); // unlink the scene ID if required if (unlinkSceneIDs) { sceneIDs[oldID.idNumber].left = null; sceneIDs[oldID.idNumber].right = false; } return(newID); }
/// <summary> /// Instantiates a prefab and assigns a network ID to it. /// This is replicated across all peers, and is buffered for any new players. /// To destroy these objects, you should use the "NetworkDestroy" function. /// </summary> /// <param name="pos">Position.</param> /// <param name="rot">Rot.</param> public GameObject NetworkInstantiate(NetworkPrefab prefab, Vector3 pos, Quaternion rot, bool sendToLocalClient) { if (NetworkCore.isServer) { if (prefab == null) { Debug.LogError("NetworkInstantiate called with no prefab. Object not created"); return(null); } GameObject newObj = Instantiate(prefab.serverPrefab, pos, rot); NetObjectID idComp = newObj.GetComponent <NetObjectID>(); NetworkID newID = GetNextID(idComp); idComp.SetNetID(newID); NetWriter writer = StartNetworkMessage("cl_NetworkInstantiate", thisNetworkID); writer.WriteInt(prefab.id); writer.WriteVector3(pos); writer.WriteVector3(rot.eulerAngles); newID.WriteBytes(writer); BufferedMessage msg = null; if (sendToLocalClient) { msg = SendMessageToAllClients(writer, NetworkCore.AllCostMsg, true); } else { msg = SendMessageToOtherClients(writer, NetworkCore.AllCostMsg, true); } if (msg == null) { Debug.Log("Failed to successfully send Instantiation message. Unregistering Network ID: " + newID.ToString()); FreeNetworkID(idComp); return(newObj); } idComp.AddBufferedMessage(msg); newObj.BroadcastMessage("OnNewNetworkSceneLoaded"); return(newObj); } return(null); }