Skip to content

Commit d82feb7

Browse files
committed
Added Spawn payload
1 parent 3037031 commit d82feb7

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ public virtual void NetworkStart()
120120
{
121121

122122
}
123+
124+
public virtual void NetworkStart(BitReader payloadReader)
125+
{
126+
NetworkStart();
127+
}
128+
123129
internal void InternalNetworkStart()
124130
{
125131
CacheAttributedMethods();

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ public void Spawn()
227227
SpawnManager.SpawnObject(this);
228228
}
229229

230+
public void Spawn(BitWriter spawnPayload)
231+
{
232+
if (NetworkingManager.singleton != null)
233+
SpawnManager.SpawnObject(this, null, spawnPayload);
234+
}
235+
230236
/// <summary>
231237
/// Unspawns this GameObject and destroys it for other clients. This should be used if the object should be kept on the server
232238
/// </summary>
@@ -245,6 +251,14 @@ public void SpawnWithOwnership(uint clientId)
245251
if (NetworkingManager.singleton != null)
246252
SpawnManager.SpawnObject(this, clientId);
247253
}
254+
255+
public void SpawnWithOwnership(uint clientId, BitWriter payload)
256+
{
257+
if (NetworkingManager.singleton != null)
258+
SpawnManager.SpawnObject(this, clientId, payload);
259+
}
260+
261+
248262
/// <summary>
249263
/// Removes all ownership of an object from any client. Can only be called from server
250264
/// </summary>
@@ -277,15 +291,15 @@ internal void InvokeBehaviourOnGainedOwnership()
277291
}
278292
}
279293

280-
internal void InvokeBehaviourNetworkSpawn()
294+
internal void InvokeBehaviourNetworkSpawn(BitReader reader)
281295
{
282296
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
283297
{
284298
//We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject.
285299
if(!childNetworkedBehaviours[i].networkedStartInvoked)
286300
{
287301
childNetworkedBehaviours[i].InternalNetworkStart();
288-
childNetworkedBehaviours[i].NetworkStart();
302+
childNetworkedBehaviours[i].NetworkStart(reader);
289303
childNetworkedBehaviours[i].SyncVarInit();
290304
childNetworkedBehaviours[i].networkedStartInvoked = true;
291305
}

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ public void StartHost(Vector3? pos = null, Quaternion? rot = null, int prefabId
633633
if (NetworkConfig.HandleObjectSpawning)
634634
{
635635
prefabId = prefabId == -1 ? NetworkConfig.NetworkPrefabIds[NetworkConfig.PlayerPrefabName] : prefabId;
636-
SpawnManager.CreateSpawnedObject(prefabId, 0, hostClientId, true, pos.GetValueOrDefault(), rot.GetValueOrDefault(), null);
636+
SpawnManager.CreateSpawnedObject(prefabId, 0, hostClientId, true, pos.GetValueOrDefault(), rot.GetValueOrDefault(), null, false, false);
637637
}
638638

639639
if (OnServerStarted != null)
@@ -1161,7 +1161,7 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
11611161
{
11621162
uint networkId = SpawnManager.GetNetworkObjectId();
11631163
prefabId = prefabId == -1 ? NetworkConfig.NetworkPrefabIds[NetworkConfig.PlayerPrefabName] : prefabId;
1164-
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, clientId, true, position, rotation);
1164+
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, clientId, true, position, rotation, null, false, false);
11651165
netObject = go.GetComponent<NetworkedObject>();
11661166
connectedClients[clientId].PlayerObject = go;
11671167
}
@@ -1258,6 +1258,8 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
12581258
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.rotation.eulerAngles.y);
12591259
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.rotation.eulerAngles.z);
12601260

1261+
writer.WriteBool(false); //No payload data
1262+
12611263
if (connectedClients[clientId].PlayerObject.GetComponent<NetworkedObject>().observers.Contains(clientPair.Key))
12621264
connectedClients[clientId].PlayerObject.GetComponent<NetworkedObject>().WriteFormattedSyncedVarData(writer);
12631265
}

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ internal static void HandleConnectionApproved(uint clientId, BitReader reader, i
106106
float zRot = reader.ReadFloat();
107107

108108
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject,
109-
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
109+
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), reader, visible, false);
110110
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
111111
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
112112
go.SetActive(isActive);
@@ -142,13 +142,15 @@ internal static void HandleAddObject(uint clientId, BitReader reader, int channe
142142
float yRot = reader.ReadFloat();
143143
float zRot = reader.ReadFloat();
144144

145+
bool hasPayload = reader.ReadBool();
146+
145147
if (isPlayerObject)
146148
{
147149
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
148150
netManager.connectedClientsList.Add(netManager.connectedClients[ownerId]);
149151
}
150152
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject,
151-
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
153+
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), reader, visible, hasPayload);
152154

153155
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
154156
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
@@ -271,7 +273,7 @@ internal static void HandleAddObjects(uint clientId, BitReader reader, int chann
271273
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
272274
netManager.connectedClientsList.Add(netManager.connectedClients[ownerId]);
273275
}
274-
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
276+
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), reader, visible, false);
275277
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
276278
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
277279

MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ internal static void ChangeOwnership(uint netId, uint clientId)
7272
InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer, null);
7373
}
7474
}
75-
75+
7676
internal static void DestroyNonSceneObjects()
7777
{
78-
if(spawnedObjects != null)
78+
if (spawnedObjects != null)
7979
{
8080
foreach (KeyValuePair<uint, NetworkedObject> netObject in spawnedObjects)
8181
{
@@ -102,7 +102,7 @@ internal static void MarkSceneObjects()
102102
{
103103
if (netObjects[i].sceneObject == null)
104104
{
105-
netObjects[i].InvokeBehaviourNetworkSpawn();
105+
netObjects[i].InvokeBehaviourNetworkSpawn(null);
106106
netObjects[i].sceneObject = true;
107107
}
108108
}
@@ -152,7 +152,7 @@ internal static void FlushSceneObjects()
152152
}
153153
}
154154

155-
internal static GameObject CreateSpawnedObject(int networkedPrefabId, uint networkId, uint owner, bool playerObject, Vector3 position, Quaternion rotation, BitReader reader = null)
155+
internal static GameObject CreateSpawnedObject(int networkedPrefabId, uint networkId, uint owner, bool playerObject, Vector3 position, Quaternion rotation, BitReader reader, bool readSyncedVar, bool readPayload)
156156
{
157157
if (!netManager.NetworkConfig.NetworkPrefabNames.ContainsKey(networkedPrefabId))
158158
{
@@ -168,8 +168,7 @@ internal static GameObject CreateSpawnedObject(int networkedPrefabId, uint netwo
168168
netObject = go.AddComponent<NetworkedObject>();
169169
}
170170

171-
if (reader != null)
172-
netObject.SetFormattedSyncedVarData(reader);
171+
if (readSyncedVar) netObject.SetFormattedSyncedVarData(reader);
173172

174173
netObject.NetworkedPrefabName = netManager.NetworkConfig.NetworkPrefabNames[networkedPrefabId];
175174
netObject._isSpawned = true;
@@ -184,7 +183,7 @@ internal static GameObject CreateSpawnedObject(int networkedPrefabId, uint netwo
184183
netObject.transform.position = position;
185184
netObject.transform.rotation = rotation;
186185
spawnedObjects.Add(netObject.NetworkId, netObject);
187-
netObject.InvokeBehaviourNetworkSpawn();
186+
netObject.InvokeBehaviourNetworkSpawn(reader);
188187
return go;
189188
}
190189

@@ -195,7 +194,7 @@ internal static void UnSpawnObject(NetworkedObject netObject)
195194
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Cannot unspawn objects that are not spawned");
196195
return;
197196
}
198-
else if(!netManager.isServer)
197+
else if (!netManager.isServer)
199198
{
200199
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Only server can unspawn objects");
201200
return;
@@ -209,7 +208,7 @@ internal static void UnSpawnObject(NetworkedObject netObject)
209208
OnDestroyObject(netObject.networkId, false);
210209
}
211210

212-
internal static void SpawnObject(NetworkedObject netObject, uint? clientOwnerId = null)
211+
internal static void SpawnObject(NetworkedObject netObject, uint? clientOwnerId = null, BitWriter payload = null)
213212
{
214213
if (netObject.isSpawned)
215214
{
@@ -236,7 +235,9 @@ internal static void SpawnObject(NetworkedObject netObject, uint? clientOwnerId
236235
spawnedObjects.Add(netId, netObject);
237236
netObject._isSpawned = true;
238237
netObject.sceneObject = false;
239-
netObject.InvokeBehaviourNetworkSpawn();
238+
if (payload == null) netObject.InvokeBehaviourNetworkSpawn(null);
239+
else using (BitReader payloadReader = BitReader.Get(payload.Finalize())) netObject.InvokeBehaviourNetworkSpawn(payloadReader);
240+
240241
if (clientOwnerId != null)
241242
{
242243
netObject.ownerClientId = clientOwnerId.Value;
@@ -261,9 +262,13 @@ internal static void SpawnObject(NetworkedObject netObject, uint? clientOwnerId
261262
writer.WriteFloat(netObject.transform.rotation.eulerAngles.y);
262263
writer.WriteFloat(netObject.transform.rotation.eulerAngles.z);
263264

265+
writer.WriteBool(payload != null);
266+
264267
if (netObject.observers.Contains(client.Key))
265268
netObject.WriteFormattedSyncedVarData(writer);
266269

270+
if (payload != null) writer.WriteWriter(payload);
271+
267272
InternalMessageHandler.Send(client.Key, "MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer, null);
268273
}
269274
}

0 commit comments

Comments
 (0)