-
Notifications
You must be signed in to change notification settings - Fork 450
feat!: OnNetworkSpawn / OnNetworkDespawn #865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1abfa3f
a50902b
0eb957d
332d6cd
72eb349
0cc2a6a
af5460e
beaa11f
9e4035b
da7a9f8
8e8940f
0c243f4
3c1f9df
dce66c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,9 +295,6 @@ protected NetworkBehaviour GetNetworkBehaviour(ushort behaviourId) | |
/// </summary> | ||
public ulong OwnerClientId => NetworkObject.OwnerClientId; | ||
|
||
internal bool NetworkStartInvoked = false; | ||
internal bool InternalNetworkStartInvoked = false; | ||
|
||
/// <summary> | ||
/// Stores the network tick at the NetworkBehaviourUpdate time | ||
/// This allows sending NetworkVariables not more often than once per network tick, regardless of the update rate | ||
|
@@ -307,22 +304,32 @@ protected NetworkBehaviour GetNetworkBehaviour(ushort behaviourId) | |
/// <summary> | ||
/// Gets called when message handlers are ready to be registered and the network is setup | ||
/// </summary> | ||
public virtual void NetworkStart() { } | ||
public virtual void OnNetworkSpawn() { } | ||
|
||
/// <summary> | ||
/// Gets called when message handlers are ready to be registered and the network is setup. Provides a Payload if it was provided | ||
/// Gets called when the <see cref="NetworkObject"/> gets spawned, message handlers are ready to be registered and the network is setup. Provides a Payload if it was provided | ||
/// </summary> | ||
/// <param name="stream">The stream containing the spawn payload</param> | ||
public virtual void NetworkStart(Stream stream) | ||
public virtual void OnNetworkSpawn(Stream stream) | ||
{ | ||
NetworkStart(); | ||
OnNetworkSpawn(); | ||
} | ||
|
||
internal void InternalNetworkStart() | ||
/// <summary> | ||
/// Gets called when the <see cref="NetworkObject"/> gets de-spawned. Is called both on the server and clients. | ||
/// </summary> | ||
public virtual void OnNetworkDespawn() { } | ||
|
||
internal void InternalOnNetworkSpawn() | ||
{ | ||
InitializeVariables(); | ||
} | ||
|
||
internal void InternalOnNetworkDespawn() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The plan is to have this empty function filled before pre-release as we will most likely have events to unsubscribe here. That's why I pre-emptively added this function for completeness. |
||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Gets called when the local client gains ownership of this object | ||
/// </summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -397,7 +397,7 @@ private void OnDestroy() | |
{ | ||
if (NetworkManager != null && NetworkManager.SpawnManager != null && NetworkManager.SpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId)) | ||
{ | ||
NetworkManager.SpawnManager.OnDestroyObject(NetworkObjectId, false); | ||
NetworkManager.SpawnManager.OnDespawnObject(NetworkObjectId, false); | ||
} | ||
} | ||
|
||
|
@@ -504,33 +504,21 @@ internal void InvokeBehaviourOnGainedOwnership() | |
} | ||
} | ||
|
||
internal void ResetNetworkStartInvoked() | ||
internal void InvokeBehaviourNetworkSpawn(Stream stream) | ||
{ | ||
if (ChildNetworkBehaviours != null) | ||
0xFA11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (int i = 0; i < ChildNetworkBehaviours.Count; i++) | ||
{ | ||
for (int i = 0; i < ChildNetworkBehaviours.Count; i++) | ||
{ | ||
ChildNetworkBehaviours[i].NetworkStartInvoked = false; | ||
} | ||
ChildNetworkBehaviours[i].InternalOnNetworkSpawn(); | ||
ChildNetworkBehaviours[i].OnNetworkSpawn(stream); | ||
} | ||
} | ||
|
||
internal void InvokeBehaviourNetworkSpawn(Stream stream) | ||
internal void InvokeBehaviourNetworkDespawn() | ||
{ | ||
for (int i = 0; i < ChildNetworkBehaviours.Count; i++) | ||
{ | ||
//We check if we are it's NetworkObject owner incase a NetworkObject exists as a child of our NetworkObject | ||
if (!ChildNetworkBehaviours[i].NetworkStartInvoked) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these checks did not do anything. They were always set to false before this function was run. The conclusion seems to be that this is legacy code which is not needed. |
||
{ | ||
if (!ChildNetworkBehaviours[i].InternalNetworkStartInvoked) | ||
{ | ||
ChildNetworkBehaviours[i].InternalNetworkStart(); | ||
ChildNetworkBehaviours[i].InternalNetworkStartInvoked = true; | ||
} | ||
|
||
ChildNetworkBehaviours[i].NetworkStart(stream); | ||
ChildNetworkBehaviours[i].NetworkStartInvoked = true; | ||
} | ||
ChildNetworkBehaviours[i].InternalOnNetworkDespawn(); | ||
ChildNetworkBehaviours[i].OnNetworkDespawn(); | ||
} | ||
} | ||
|
||
|
@@ -698,7 +686,7 @@ internal void SerializeSceneObject(NetworkWriter writer, ulong targetClientId) | |
// If our current buffer position is greater than our positionBeforeNetworkVariableData then we wrote NetworkVariable data | ||
// Part 1: This will include the total NetworkVariable data size, if there was NetworkVariable data written, to the stream | ||
// in order to be able to skip past this entry on the deserialization side in the event this NetworkObject fails to be | ||
// constructed (See Part 2 below in the DeserializeSceneObject method) | ||
// constructed (See Part 2 below in the DeserializeSceneObject method) | ||
if (buffer.Position > positionBeforeNetworkVariableData) | ||
{ | ||
// Store our current stream buffer position | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -332,8 +332,6 @@ internal void SpawnNetworkObjectLocally(NetworkObject networkObject, ulong netwo | |
} | ||
} | ||
|
||
networkObject.ResetNetworkStartInvoked(); | ||
|
||
if (readPayload) | ||
{ | ||
using (var payloadBuffer = PooledNetworkBuffer.Get()) | ||
|
@@ -452,7 +450,7 @@ internal void DespawnObject(NetworkObject networkObject, bool destroyObject = fa | |
throw new NotServerException("Only server can despawn objects"); | ||
} | ||
|
||
OnDestroyObject(networkObject.NetworkObjectId, destroyObject); | ||
OnDespawnObject(networkObject.NetworkObjectId, destroyObject); | ||
} | ||
|
||
// Makes scene objects ready to be reused | ||
|
@@ -490,7 +488,7 @@ internal void ServerDestroySpawnedSceneObjects() | |
if (NetworkManager.PrefabHandler != null && NetworkManager.PrefabHandler.ContainsHandler(sobj)) | ||
{ | ||
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(sobj); | ||
OnDestroyObject(sobj.NetworkObjectId, false); | ||
OnDespawnObject(sobj.NetworkObjectId, false); | ||
} | ||
else | ||
{ | ||
|
@@ -514,7 +512,7 @@ internal void DestroyNonSceneObjects() | |
{ | ||
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObjects[i]); | ||
|
||
OnDestroyObject(networkObjects[i].NetworkObjectId, false); | ||
OnDespawnObject(networkObjects[i].NetworkObjectId, false); | ||
} | ||
else | ||
{ | ||
|
@@ -538,7 +536,7 @@ internal void DestroySceneObjects() | |
if (NetworkManager.PrefabHandler.ContainsHandler(networkObjects[i])) | ||
{ | ||
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObjects[i]); | ||
OnDestroyObject(networkObjects[i].NetworkObjectId, false); | ||
OnDespawnObject(networkObjects[i].NetworkObjectId, false); | ||
} | ||
else | ||
{ | ||
|
@@ -599,21 +597,21 @@ internal void ClientCollectSoftSyncSceneObjectSweep(NetworkObject[] networkObjec | |
} | ||
} | ||
|
||
internal void OnDestroyObject(ulong networkId, bool destroyGameObject) | ||
internal void OnDespawnObject(ulong networkId, bool destroyGameObject) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renaming this because this is really not |
||
{ | ||
if (NetworkManager == null) | ||
{ | ||
return; | ||
} | ||
|
||
//Removal of spawned object | ||
if (!SpawnedObjects.TryGetValue(networkId, out NetworkObject sobj)) | ||
if (!SpawnedObjects.TryGetValue(networkId, out NetworkObject networkObject)) | ||
{ | ||
Debug.LogWarning($"Trying to destroy object {networkId} but it doesn't seem to exist anymore!"); | ||
return; | ||
} | ||
|
||
if (!sobj.IsOwnedByServer && !sobj.IsPlayerObject && NetworkManager.Singleton.ConnectedClients.TryGetValue(sobj.OwnerClientId, out NetworkClient networkClient)) | ||
if (!networkObject.IsOwnedByServer && !networkObject.IsPlayerObject && NetworkManager.Singleton.ConnectedClients.TryGetValue(networkObject.OwnerClientId, out NetworkClient networkClient)) | ||
{ | ||
//Someone owns it. | ||
for (int i = networkClient.OwnedObjects.Count - 1; i > -1; i--) | ||
|
@@ -625,7 +623,8 @@ internal void OnDestroyObject(ulong networkId, bool destroyGameObject) | |
} | ||
} | ||
|
||
sobj.IsSpawned = false; | ||
networkObject.IsSpawned = false; | ||
networkObject.InvokeBehaviourNetworkDespawn(); | ||
|
||
if (NetworkManager != null && NetworkManager.IsServer) | ||
{ | ||
|
@@ -641,7 +640,7 @@ internal void OnDestroyObject(ulong networkId, bool destroyGameObject) | |
var rpcQueueContainer = NetworkManager.RpcQueueContainer; | ||
if (rpcQueueContainer != null) | ||
{ | ||
if (sobj != null) | ||
if (networkObject != null) | ||
{ | ||
// As long as we have any remaining clients, then notify of the object being destroy. | ||
if (NetworkManager.ConnectedClientsList.Count > 0) | ||
|
@@ -668,14 +667,14 @@ internal void OnDestroyObject(ulong networkId, bool destroyGameObject) | |
} | ||
} | ||
|
||
var gobj = sobj.gameObject; | ||
var gobj = networkObject.gameObject; | ||
|
||
if (destroyGameObject && gobj != null) | ||
{ | ||
if (NetworkManager.PrefabHandler.ContainsHandler(sobj)) | ||
if (NetworkManager.PrefabHandler.ContainsHandler(networkObject)) | ||
{ | ||
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(sobj); | ||
OnDestroyObject(networkId, false); | ||
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObject); | ||
OnDespawnObject(networkId, false); | ||
} | ||
else | ||
{ | ||
|
@@ -688,7 +687,7 @@ internal void OnDestroyObject(ulong networkId, bool destroyGameObject) | |
// of the function | ||
if (SpawnedObjects.Remove(networkId)) | ||
{ | ||
SpawnedObjectsList.Remove(sobj); | ||
SpawnedObjectsList.Remove(networkObject); | ||
} | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stream overload is something we probably want to remove but will be a separate RFC/ PR