diff --git a/TestVelGameServer/Assets/VelGameServer/NetworkManager.cs b/TestVelGameServer/Assets/VelGameServer/NetworkManager.cs index da53077..3e37fd9 100644 --- a/TestVelGameServer/Assets/VelGameServer/NetworkManager.cs +++ b/TestVelGameServer/Assets/VelGameServer/NetworkManager.cs @@ -25,7 +25,8 @@ public class NetworkManager : MonoBehaviour public Action onPlayerLeft = delegate { }; public List prefabs = new List(); - NetworkObject[] sceneObjects; + public NetworkObject[] sceneObjects; + public List deletedSceneObjects = new List(); public Dictionary objects = new Dictionary(); //maintains a list of all known objects on the server (ones that have ids) NetworkPlayer masterPlayer = null; #endregion @@ -96,7 +97,7 @@ public class NetworkManager : MonoBehaviour { //we got a left message, kill it - //change ownership of all objects to + //change ownership of all objects to master foreach (KeyValuePair kvp in objects) { @@ -126,6 +127,7 @@ public class NetworkManager : MonoBehaviour player.manager = this; players.Add(m.sender, player); onPlayerJoined(player); + } } } @@ -147,6 +149,7 @@ public class NetworkManager : MonoBehaviour { sceneObjects[i].networkId = -1 + "-" + i; sceneObjects[i].owner = masterPlayer; + sceneObjects[i].isSceneObject = true; //needed for special handling when deleted objects.Add(sceneObjects[i].networkId,sceneObjects[i]); } @@ -380,4 +383,18 @@ public class NetworkManager : MonoBehaviour { SendNetworkMessage("5:" + groupname + ":" + String.Join(":", userids)); } + public void deleteNetworkObject(string networkId) + { + if (objects.ContainsKey(networkId)) + { + NetworkObject obj = objects[networkId]; + if (obj.isSceneObject) + { + deletedSceneObjects.Add(networkId); + } + GameObject.Destroy(obj.gameObject); + objects.Remove(networkId); + } + + } } \ No newline at end of file diff --git a/TestVelGameServer/Assets/VelGameServer/NetworkObject.cs b/TestVelGameServer/Assets/VelGameServer/NetworkObject.cs index cab1c88..96b5739 100644 --- a/TestVelGameServer/Assets/VelGameServer/NetworkObject.cs +++ b/TestVelGameServer/Assets/VelGameServer/NetworkObject.cs @@ -10,5 +10,6 @@ public abstract class NetworkObject: MonoBehaviour public NetworkPlayer owner; public string networkId; //this is forged from the combination of the creator's id (-1 in the case of a scene object) and an object id, so it's always unique for a room public string prefabName; //this may be empty if it's not a prefab (scene object) + public bool isSceneObject=false; public abstract void handleMessage(string identifier, byte[] message); } diff --git a/TestVelGameServer/Assets/VelGameServer/NetworkPlayer.cs b/TestVelGameServer/Assets/VelGameServer/NetworkPlayer.cs index fc3c500..767b3b7 100644 --- a/TestVelGameServer/Assets/VelGameServer/NetworkPlayer.cs +++ b/TestVelGameServer/Assets/VelGameServer/NetworkPlayer.cs @@ -34,6 +34,8 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer public List closePlayers = new List(); //for testing audio communications public bool changeClosePlayers = true; + + void Start() { myObject.owner = this; @@ -62,8 +64,7 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer } - - + } public void handlePlayerJoined(NetworkPlayer player) @@ -79,6 +80,12 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer } } + + if (isMaster) + { + //send a list of scene object ids when someone joins + sendSceneUpdate(); + } } } @@ -189,15 +196,21 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer break; } - case "8": //I'm trying to destroy a gameobject I own (I guess this is sent to everyone) + case "8": //I'm trying to destroy a gameobject I own { string networkId = sections[1]; - if (manager.objects.ContainsKey(networkId) && manager.objects[networkId].owner == this) - { - GameObject.Destroy(manager.objects[networkId].gameObject); - manager.objects.Remove(networkId); + manager.deleteNetworkObject(networkId); + break; + } + case "9": //deleted scene objects + { + + for (int k = 1; k < sections.Length; k++) + { + manager.deleteNetworkObject(sections[k]); } + break; } } @@ -278,6 +291,7 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer { if (!manager.objects.ContainsKey(networkId) || manager.objects[networkId].owner != this || !isLocal) return; //must be the local owner of the object to destroy it manager.sendTo(NetworkManager.MessageType.ALL_ORDERED, "8," + networkId); //send to all, which will make me delete as well + } public void takeOwnership(string networkId) @@ -289,5 +303,11 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer } + public void sendSceneUpdate() + { + + manager.sendTo(NetworkManager.MessageType.OTHERS, "9," + string.Join(",", manager.deletedSceneObjects)); + } + } diff --git a/server_basics.txt b/server_basics.txt index 6a561a7..4746be3 100644 --- a/server_basics.txt +++ b/server_basics.txt @@ -63,10 +63,7 @@ An interesting problem is what to do when new clients join. This is partially l Clients can manage network traffic using messaging groups. This is especially useful for audio, where you can maintain a list of people close enough to you to hear audio Todo: - -Scene objects that are destroyed need to be buffered to new clients. -Instantiation byte[] (full sync, basically). -Need to determine what happens when you specifically leave a room (session). +