send message to remove deleted scene objects when a new player loads
parent
001e89509f
commit
57310f180c
|
|
@ -25,7 +25,8 @@ public class NetworkManager : MonoBehaviour
|
||||||
public Action<NetworkPlayer> onPlayerLeft = delegate { };
|
public Action<NetworkPlayer> onPlayerLeft = delegate { };
|
||||||
|
|
||||||
public List<NetworkObject> prefabs = new List<NetworkObject>();
|
public List<NetworkObject> prefabs = new List<NetworkObject>();
|
||||||
NetworkObject[] sceneObjects;
|
public NetworkObject[] sceneObjects;
|
||||||
|
public List<string> deletedSceneObjects = new List<string>();
|
||||||
public Dictionary<string, NetworkObject> objects = new Dictionary<string, NetworkObject>(); //maintains a list of all known objects on the server (ones that have ids)
|
public Dictionary<string, NetworkObject> objects = new Dictionary<string, NetworkObject>(); //maintains a list of all known objects on the server (ones that have ids)
|
||||||
NetworkPlayer masterPlayer = null;
|
NetworkPlayer masterPlayer = null;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
@ -96,7 +97,7 @@ public class NetworkManager : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
|
||||||
//we got a left message, kill it
|
//we got a left message, kill it
|
||||||
//change ownership of all objects to
|
//change ownership of all objects to master
|
||||||
|
|
||||||
foreach (KeyValuePair<string, NetworkObject> kvp in objects)
|
foreach (KeyValuePair<string, NetworkObject> kvp in objects)
|
||||||
{
|
{
|
||||||
|
|
@ -126,6 +127,7 @@ public class NetworkManager : MonoBehaviour
|
||||||
player.manager = this;
|
player.manager = this;
|
||||||
players.Add(m.sender, player);
|
players.Add(m.sender, player);
|
||||||
onPlayerJoined(player);
|
onPlayerJoined(player);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -147,6 +149,7 @@ public class NetworkManager : MonoBehaviour
|
||||||
{
|
{
|
||||||
sceneObjects[i].networkId = -1 + "-" + i;
|
sceneObjects[i].networkId = -1 + "-" + i;
|
||||||
sceneObjects[i].owner = masterPlayer;
|
sceneObjects[i].owner = masterPlayer;
|
||||||
|
sceneObjects[i].isSceneObject = true; //needed for special handling when deleted
|
||||||
objects.Add(sceneObjects[i].networkId,sceneObjects[i]);
|
objects.Add(sceneObjects[i].networkId,sceneObjects[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -380,4 +383,18 @@ public class NetworkManager : MonoBehaviour
|
||||||
{
|
{
|
||||||
SendNetworkMessage("5:" + groupname + ":" + String.Join(":", userids));
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -10,5 +10,6 @@ public abstract class NetworkObject: MonoBehaviour
|
||||||
public NetworkPlayer owner;
|
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 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 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);
|
public abstract void handleMessage(string identifier, byte[] message);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,8 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
|
||||||
|
|
||||||
public List<int> closePlayers = new List<int>(); //for testing audio communications
|
public List<int> closePlayers = new List<int>(); //for testing audio communications
|
||||||
public bool changeClosePlayers = true;
|
public bool changeClosePlayers = true;
|
||||||
|
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
myObject.owner = this;
|
myObject.owner = this;
|
||||||
|
|
@ -62,8 +64,7 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handlePlayerJoined(NetworkPlayer player)
|
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;
|
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];
|
string networkId = sections[1];
|
||||||
|
|
||||||
if (manager.objects.ContainsKey(networkId) && manager.objects[networkId].owner == this)
|
manager.deleteNetworkObject(networkId);
|
||||||
{
|
break;
|
||||||
GameObject.Destroy(manager.objects[networkId].gameObject);
|
}
|
||||||
manager.objects.Remove(networkId);
|
case "9": //deleted scene objects
|
||||||
|
{
|
||||||
|
|
||||||
|
for (int k = 1; k < sections.Length; k++)
|
||||||
|
{
|
||||||
|
manager.deleteNetworkObject(sections[k]);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
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
|
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
|
manager.sendTo(NetworkManager.MessageType.ALL_ORDERED, "8," + networkId); //send to all, which will make me delete as well
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void takeOwnership(string networkId)
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
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:
|
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).
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue