Skip to content

Commit f83d50a

Browse files
committed
Added Object spawning functionality
1 parent 4ad74c7 commit f83d50a

File tree

2 files changed

+101
-12
lines changed

2 files changed

+101
-12
lines changed

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
namespace MLAPI
44
{
5-
//TODO
6-
//Will be used for objects which will be spawned automatically across clients
75
public class NetworkedObject : MonoBehaviour
86
{
97
[HideInInspector]
108
public uint NetworkId;
119
[HideInInspector]
1210
public int OwnerClientId = -1;
1311
[HideInInspector]
14-
public int SpawnablePrefabId;
12+
public int SpawnablePrefabIndex;
1513
[HideInInspector]
1614
public bool IsPlayerObject = false;
1715
public bool ServerOnly = false;
@@ -25,7 +23,13 @@ public bool isLocalPlayer
2523

2624
private void OnDestroy()
2725
{
28-
NetworkingManager.OnDestroyObject(NetworkId);
26+
NetworkingManager.singleton.OnDestroyObject(NetworkId, false);
27+
}
28+
29+
internal bool isSpawned = false;
30+
public void Spawn()
31+
{
32+
NetworkingManager.singleton.OnSpawnObject(this);
2933
}
3034
}
3135
}

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ private void OnValidate()
7070
{
7171
for (int i = 0; i < SpawnablePrefabs.Count; i++)
7272
{
73-
if(SpawnablePrefabs[i].GetComponentInChildren<NetworkedObject>() == null)
73+
NetworkedObject netObject = SpawnablePrefabs[i].GetComponentInChildren<NetworkedObject>();
74+
if(netObject == null)
7475
{
7576
Debug.LogWarning("MLAPI: All SpawnablePrefabs need a NetworkedObject component. Please add one to the prefab " + SpawnablePrefabs[i].gameObject.name);
77+
continue;
7678
}
79+
netObject.SpawnablePrefabIndex = i;
7780
}
7881
}
7982

@@ -86,7 +89,7 @@ internal GameObject SpawnObject(int spawnablePrefabIndex, uint networkId, int ow
8689
Debug.LogWarning("MLAPI: Please add a NetworkedObject component to the root of all spawnable objects");
8790
netObject = go.AddComponent<NetworkedObject>();
8891
}
89-
netObject.SpawnablePrefabId = spawnablePrefabIndex;
92+
netObject.SpawnablePrefabIndex = spawnablePrefabIndex;
9093
if(singleton.isServer)
9194
{
9295
netObject.NetworkId = singleton.GetNetworkObjectId();
@@ -125,15 +128,82 @@ internal GameObject SpawnPlayerObject(int clientId, uint networkId)
125128
return go;
126129
}
127130

128-
internal static void OnDestroyObject(uint networkId)
131+
internal void OnDestroyObject(uint networkId, bool destroyGameObject)
129132
{
130-
if (!singleton.isServer)
133+
if (!spawnedObjects.ContainsKey(networkId) || !NetworkConfig.HandleObjectSpawning)
131134
return;
135+
GameObject go = spawnedObjects[networkId].gameObject;
136+
if(isServer)
137+
{
138+
releasedNetworkObjectIds.Push(networkId);
139+
if (!spawnedObjects[networkId].ServerOnly)
140+
{
141+
using (MemoryStream stream = new MemoryStream())
142+
{
143+
using (BinaryWriter writer = new BinaryWriter(stream))
144+
{
145+
writer.Write(networkId);
146+
}
147+
//If we are host, send to everyone except ourselves. Otherwise, send to all
148+
if (isHost)
149+
Send("MLAPI_DESTROY_OBJECT", "MLAPI_RELIABLE_FRAGMENTED", stream.ToArray(), -1);
150+
else
151+
Send("MLAPI_DESTROY_OBJECT", "MLAPI_RELIABLE_FRAGMENTED", stream.ToArray());
152+
}
153+
}
154+
}
155+
if(destroyGameObject)
156+
Destroy(go);
157+
spawnedObjects.Remove(networkId);
158+
}
132159

133-
if (!singleton.spawnedObjects.ContainsKey(networkId))
160+
internal void OnSpawnObject(NetworkedObject netObject, int? clientOwnerId = null)
161+
{
162+
if (netObject.isSpawned)
163+
{
164+
Debug.LogWarning("MLAPI: Object already spawned");
165+
return;
166+
}
167+
else if(!isServer)
168+
{
169+
Debug.LogWarning("MLAPI: Only server can spawn objects");
170+
return;
171+
}
172+
else if(netObject.SpawnablePrefabIndex == -1)
173+
{
174+
Debug.LogWarning("MLAPI: Invalid prefab index");
175+
return;
176+
}
177+
else if(netObject.ServerOnly)
178+
{
179+
Debug.LogWarning("MLAPI: Server only objects does not have to be spawned");
180+
return;
181+
}
182+
else if(NetworkConfig.HandleObjectSpawning)
183+
{
184+
Debug.LogWarning("MLAPI: NetworkingConfiguration is set to not handle object spawning");
134185
return;
135-
NetworkedObject netObject = singleton.spawnedObjects[networkId];
136-
singleton.releasedNetworkObjectIds.Push(networkId);
186+
}
187+
uint netId = GetNetworkObjectId();
188+
spawnedObjects.Add(netId, netObject);
189+
netObject.isSpawned = true;
190+
if (clientOwnerId != null)
191+
netObject.OwnerClientId = (int)clientOwnerId;
192+
using(MemoryStream stream = new MemoryStream())
193+
{
194+
using(BinaryWriter writer = new BinaryWriter(stream))
195+
{
196+
writer.Write(false);
197+
writer.Write(netObject.NetworkId);
198+
writer.Write(netObject.OwnerClientId);
199+
writer.Write(netObject.SpawnablePrefabIndex);
200+
}
201+
//If we are host, send to everyone except ourselves. Otherwise, send to all
202+
if (isHost)
203+
Send("MLAPI_ADD_OBJECT", "MLAPI_RELIABLE_FRAGMENTED", stream.ToArray(), -1);
204+
else
205+
Send("MLAPI_ADD_OBJECT", "MLAPI_RELIABLE_FRAGMENTED", stream.ToArray());
206+
}
137207
}
138208

139209
internal void InvokeMessageHandlers(string messageType, byte[] data, int clientId)
@@ -238,6 +308,7 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
238308
messageTypes.Add("MLAPI_CONNECTION_APPROVED", 1);
239309
messageTypes.Add("MLAPI_ADD_OBJECT", 2);
240310
messageTypes.Add("MLAPI_CLIENT_DISCONNECT", 3);
311+
messageTypes.Add("MLAPI_DESTROY_OBJECT", 4);
241312

242313

243314
HashSet<string> channelNames = new HashSet<string>();
@@ -564,6 +635,20 @@ private void HandleIncomingData(int connectonId, byte[] data)
564635
}
565636
}
566637
break;
638+
case 4:
639+
//Server infroms clients to destroy an object
640+
if(isClient)
641+
{
642+
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
643+
{
644+
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
645+
{
646+
uint netId = messageReader.ReadUInt32();
647+
OnDestroyObject(netId, true);
648+
}
649+
}
650+
}
651+
break;
567652
}
568653
}
569654
}
@@ -822,7 +907,7 @@ private void HandleApproval(int clientId, bool approved)
822907
writer.Write(pair.Value.IsPlayerObject);
823908
writer.Write(pair.Value.NetworkId);
824909
writer.Write(pair.Value.OwnerClientId);
825-
writer.Write(pair.Value.SpawnablePrefabId);
910+
writer.Write(pair.Value.SpawnablePrefabIndex);
826911
}
827912
}
828913
}

0 commit comments

Comments
 (0)