Skip to content

refactor!: NetworkPrefabHandler and INetworkPrefabInstanceHandler API & XmlDoc changes #977

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

Merged
merged 14 commits into from
Jul 29, 2021

Conversation

NoelStephensUnity
Copy link
Collaborator

@NoelStephensUnity NoelStephensUnity commented Jul 23, 2021

MTT-961

  • Improved comments and added additional XML Documentation for the NetworkPrefabHandler and associated INetworkPrefabInstanceHandler.
  • Renamed the two methods within INetworkPrefabInstanceHandler to better reflect what their function was and to avoid user confusion.
  • Provided the ability for Hosts to properly display NetworkPrefab overrides.
  • Now when hosts spawn the NetworkPrefab override, messages to clients about the overridden NetworkObject will send the source NetworkPrefab's GlobalObjetIdHash value as opposed to the overriden NetworkPrefab's.
  • The above two refactors are applied to NetworkPrefab Handlers
  • Host can now register 1 or more NetworkPrefab overrides with the NetworkPrefabHandler, which allows for the destory method of the INetworkPrefabInstanceHandler implementation to be invoked on the host.
  • Removed areas where we were recursively calling OnDespawnObject where the prefab message handler was detected on a NetworkObject.
  • NetworkPrefabPool has been updated to reflect some of the above changes
  • Improved the GenericNetworkObjectBehaviour and adjusted it to account for some of the above changes as well as clients now will no longer have a "flicker" in re-cycled and re-spawned NetworkObjects that was due to latency in the new NetworkTransform position (I think when snapshot handles spawning and despawning this could be removed).
  • Added new method NetworkManager.GetNetworkPrefabOverride that is specific to Prefab Handler Overrides, Host mode, and the above changes (although there could be other uses for this)

Renamed the two methods within INetworkPrefabInstanceHandler to better reflect what their function was and to avoid user confusion.

Improved comments and added additional XML Documentation for the NetworkPrefabHandler and associated INetworkPrefabInstanceHandler
Minor updates on the ClientInstantiateOverride parameter names
@0xFA11 0xFA11 changed the title refactor: NetworkPrefabHandler and INetworkPrefabInstanceHandler clarity and xml doc updates refactor!: NetworkPrefabHandler and INetworkPrefabInstanceHandler API & XmlDoc changes Jul 26, 2021
Comment on lines 24 to 36
NetworkObject ClientInstantiateOverride(ulong ownerClientId, Vector3 position, Quaternion rotation);

/// <summary>
/// Client and Server
/// Once an implementation is registered with the <see cref="NetworkPrefabHandler"/>, this method will be called every time
/// a Network Prefab associated <see cref="NetworkObject"/> is destroyed.
/// If <see cref="NetworkObject.Despawn(bool)"/> is invoked with the default destroy parameter (i.e. false) then this method will not be invoked.
///
/// Note on Pooling: When invoked, you do not need to despawn or destroy the NetworkObject as long as you want your pool to persist.
/// The most common approach is to make the <see cref="NetworkObject"/> inactive by calling <see cref="GameObject.SetActive(bool)"/>.
/// </summary>
/// <param name="networkObject">The <see cref="NetworkObject"/> being destroyed</param>
void DestroyOverride(NetworkObject networkObject);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I'd go for ClientInstantiateOverride & DestroyOverride instead of HandleNetworkPrefabSpawn & HandleNetworkPrefabDestroy under an interface called INetworkPrefabInstanceHandler.

I think I'd drop "-Override" suffix and keep "Handle-" prefix around.

Also "Client-" prefix sounds a little risky to me in terms of our future potential and its API-breaking nature.
We might expand this feature's impact area and even make it work for both client and server sides. That would require another API-breaking change just to drop "Client-" prefix or we might have to add another super similar API that starts with "Server-" prefix to not break the old API (which would not be ideal IMHO).

Only thing I'd change here would be HandleNetworkPrefabDestroyHandleNetworkPrefabDespawn to match NetworkBehaviour.OnNetworkSpawn() and NetworkBehaviour.OnNetworkDespawn() naming convention.

Also, it'd be tempting to keep client-specific comments around but I think they should go away and make feature non-client specific. Client-specific comment may go under NetworkPrefabHandler since it is the source where client-specific nature is coming from (and even that might change in the future to cover both client and server side, hence my earlier comments above).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the old naming here was very inconsistent. Spawn, Destroy don't match. We either want HandleNetworkPrefabDespawn or rename the Spawn to Instantiate.

Now the reason we want for Instantiate/Destroy was is because you can spawn/despawn a networkobject in MLAPI without triggering these handlers (GameObject.Destroy on the server for instance skips the handler). And what they really do is override a GameObject Instatiate / Destroy function and replace it with something else.

I prefer the Override suffix over Handle prefix. Because HandleNetworkPrefabDestroy does not indicate whether this handler is run in addition to the destroy or instead of the destroy. Dropping the Client prefix on InstantiateOverride sounds good if we consider future potential.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so it's:

  • HandleNetworkPrefabSpawn
  • HandleNetworkPrefabDespawn

vs

  • InstantiateOverride
  • DestroyOverride

:)

I believe it's fair to say that we'd go for Instantiate/Destroy.

so my final suggestion would be:

  • HandleInstantiate
  • HandleDestroy

:P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HandleInstantiate, HandleDestroy sounds good to me 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only issue I have with "HandleInstantiate" is that this is only invoked on the client side and could be misleading. However, that can be made clear in documentation.

How about we even further simplify this? I mean it is a "Handler" so not sure we have to put "handle" in front of all of the methods. Why not:

  • Instantiate

  • Destroy

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only invoked on the client side

hence my first comment, it is currently client-only but we could expand it to cover both server and client.

it is a "Handler" so ...

I think that's also a fair point. INetworkPrefabInstanceHandler.Instantiate & INetworkPrefabInstanceHandler.Destroy it is :)

Renamed the interface methods.  Updated comments.
Refactored NetworkPrefabHandler to handle GitHub Issue 990 as well as made adjustments to be able to send the appropriate GlobalObjectIdHash as a host.
This also includes a public facing method in the NetworkManager to be able to lookup a NetworkManager defined NetworkPrefab Override for some associated cases relating to Host mode and NetworkPrefabHandlers and NetworkPrefab Overrides.
Made adjustments to the SceneTransitioning test and associated scenes and assets.
@NoelStephensUnity NoelStephensUnity requested a review from 0xFA11 July 28, 2021 19:57
@@ -86,8 +86,7 @@ private void OnTriggerEnter(Collider other)
}
else
{
NetworkObject.Despawn();
NetworkObject.gameObject.SetActive(false);
Copy link
Contributor

@0xFA11 0xFA11 Jul 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did we drop this intentionally?
I'm talking about NetworkObject.gameObject.SetActive(false);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review it again as I just updated.
It should make more sense...

@@ -85,6 +82,21 @@ private void RegisterCustomPrefabHandler()
}
}


private void DeRegisterCustomPrefabHandler()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super-nit :)

Suggested change
private void DeRegisterCustomPrefabHandler()
private void DeregisterCustomPrefabHandler()

Added the case where no prefab handler override is registered but there is still a host.
Added a lookup table to quickly get the proper source GlobalObjectIdHash value for the NetworkPrefab that has an override in the NetworkManager.
The first scene in the SceneTransitioningTest uses a NetworkManager defined prefab override, in host mode the host will pool while clients will not pool (no need since there is no way to override the instantiation) and the host will always send the source NetworkPrefab's GlobalObjectIdHash value as opposed to the target override's GlobalObjectIdHash.
Fixed Fatih's super-nit-pick :)
Removing an artifact property from a previous merge.
Fixing missing change to deregister and issue with despawning during fixed update.
Need to reset the position for the object when no handler exists otherwise the trigger will fire immediately and cause it to despawn immediately.
Removing despawn replication and from places where they are no longer needed either due to the now proper message ordering.

Updated the GenericNetowrkObjectBehaviour such that clients disable the MeshRenderer component and then have a delayed re-enabling of it upon being spawned in order to avoid the latent update in the transform's position (removes the flickering of NetworkObjects on the client).

Added the ability to make the SwitchSceneHandler have a delayed "auto switch" in order to run cycled long burn runs to assure everything is working "as expected".

Fixed an access violation in StatsDisplay ( minor thing that was bugging me).
Copy link
Contributor

@0xFA11 0xFA11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing a few other changes going in after I approved this PR.
I think I should re-review this again some time — please hmu when you think it's ready to be reviewed again :)

@NoelStephensUnity NoelStephensUnity requested a review from 0xFA11 July 28, 2021 23:31
@@ -504,14 +504,14 @@ internal void ServerDestroySpawnedSceneObjects()
// This **needs** to be here until we overhaul NetworkSceneManager due to dependencies
// that occur shortly after NetworkSceneManager invokes ServerDestroySpawnedSceneObjects
// within the NetworkSceneManager.SwitchScene method.
SpawnedObjectsList.Remove(sobj);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really a short term change and will be updated in PR-955 Additive Scene Loading.

@@ -559,7 +559,10 @@ internal void DestroySceneObjects()
if (NetworkManager.PrefabHandler.ContainsHandler(networkObjects[i]))
{
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObjects[i]);
OnDespawnObject(networkObjects[i], false);
if (SpawnedObjects.ContainsKey(networkObjects[i].NetworkObjectId))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More short term updates until PR-955.

Refactored a better client side "no blinking when spawning" solution.

Fixed an issue where everyone was sending destroy messages to everyone else.
@@ -504,14 +504,14 @@ internal void ServerDestroySpawnedSceneObjects()
// This **needs** to be here until we overhaul NetworkSceneManager due to dependencies
// that occur shortly after NetworkSceneManager invokes ServerDestroySpawnedSceneObjects
// within the NetworkSceneManager.SwitchScene method.
SpawnedObjectsList.Remove(sobj);

if (NetworkManager.PrefabHandler != null && NetworkManager.PrefabHandler.ContainsHandler(sobj))
{
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(sobj);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This I am leaving up to the user to handle how they want to deal with scene transitions and prefab handler overrides.

@@ -681,7 +684,6 @@ internal void OnDespawnObject(NetworkObject networkObject, bool destroyGameObjec
if (NetworkManager.PrefabHandler.ContainsHandler(networkObject))
{
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObject);
OnDespawnObject(networkObject, false);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LukeStampfli , I could you double check this? I did some testing on my end and I think this is no longer needed with the more recent updates (I think this was for some message ordering issue, but that should be fixed now).
Gladly will revert this is you think there is a special case for having that OnDespawnObject here.

Copy link
Contributor

@LukeStampfli LukeStampfli Jul 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried understand this but not sure why we had it. It does a recursive call into OnDespawnObject again for some reason I can't find a reason why we would want to do that. So from what I know this is not needed?

Added a more simplified example that uses a INetworkPrefabInstanceHandler implementation as a 1:1 NetworkObject pool.
Copy link
Contributor

@LukeStampfli LukeStampfli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks all good and great XML docs!

Fixed issue with spawning:
- Sending to every client for each client within the observers list
- Making the client ownership equal to the client it was sending to

This fixes the issue with connecting more than 1 client to a host or server.
Copy link
Contributor

@LukeStampfli LukeStampfli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The additional fix to the SendSpawnCallForObject looks good.

@NoelStephensUnity NoelStephensUnity merged commit d6b2a48 into develop Jul 29, 2021
@NoelStephensUnity NoelStephensUnity deleted the refactor/INetworkPrefabInstanceHandler branch July 29, 2021 19:06
NoelStephensUnity added a commit to Unity-Technologies/com.unity.multiplayer.docs that referenced this pull request Jul 30, 2021
This updates the object pooling example to reflect the changes from PR-977:
Unity-Technologies/com.unity.netcode.gameobjects#977
Briancoughlin added a commit to Unity-Technologies/com.unity.multiplayer.docs that referenced this pull request Aug 25, 2021
* refactor and updates

This updates the object pooling example to reflect the changes from PR-977:
Unity-Technologies/com.unity.netcode.gameobjects#977

* additional notes

This adds additional information about using more than one prefab with an override.
This also adds a link to the to-be-existing folder in the master branch.

* refactor

Removed some code from advanced example that was not being used.

* refactor 

Updating to most recent changes from PR-955 (yet to be merged)

* refactor

making changes to reflect the final version that was merged into the develop branch.

Co-authored-by: Briancoughlin <76997526+Briancoughlin@users.noreply.github.com>
Briancoughlin added a commit to Unity-Technologies/com.unity.multiplayer.docs that referenced this pull request Sep 13, 2021
* refactor and updates

This updates the object pooling example to reflect the changes from PR-977:
Unity-Technologies/com.unity.netcode.gameobjects#977

* additional notes

This adds additional information about using more than one prefab with an override.
This also adds a link to the to-be-existing folder in the master branch.

* refactor

Removed some code from advanced example that was not being used.

* refactor 

Updating to most recent changes from PR-955 (yet to be merged)

* MTTDOC-317 initial page creation

* #MTTDOC-317 update to main page

* refactor

making changes to reflect the final version that was merged into the develop branch.

* MTTDOC-317 updating to ensure successful build

* MTTDOC-317 fixing some typos and naming

* update

Ran through this one more time removing any references to MLAPI.
I also tried to improve upon the text copy flow (might need a second pair of eyes)

* update 

minor language tweak and italicized the reference to object pooling link

* update

left out the trailing *

* Update object-pooling.md

* Update object-pooling.md

* Update object-pooling.md

* Delete networktime-ticks.md

Co-authored-by: Brian Coughlin <brian.coughlin@unity3d.com>
Co-authored-by: Briancoughlin <76997526+Briancoughlin@users.noreply.github.com>
Briancoughlin pushed a commit to Unity-Technologies/com.unity.multiplayer.docs that referenced this pull request Sep 14, 2021
This updates the object pooling example to reflect the changes from PR-977:
Unity-Technologies/com.unity.netcode.gameobjects#977
Briancoughlin added a commit to Unity-Technologies/com.unity.multiplayer.docs that referenced this pull request Sep 14, 2021
* refactor and updates

This updates the object pooling example to reflect the changes from PR-977:
Unity-Technologies/com.unity.netcode.gameobjects#977

* additional notes

This adds additional information about using more than one prefab with an override.
This also adds a link to the to-be-existing folder in the master branch.

* refactor

Removed some code from advanced example that was not being used.

* refactor 

Updating to most recent changes from PR-955 (yet to be merged)

* refactor

making changes to reflect the final version that was merged into the develop branch.

Co-authored-by: Briancoughlin <76997526+Briancoughlin@users.noreply.github.com>
Briancoughlin added a commit to Unity-Technologies/com.unity.multiplayer.docs that referenced this pull request Sep 14, 2021
* refactor and updates

This updates the object pooling example to reflect the changes from PR-977:
Unity-Technologies/com.unity.netcode.gameobjects#977

* additional notes

This adds additional information about using more than one prefab with an override.
This also adds a link to the to-be-existing folder in the master branch.

* refactor

Removed some code from advanced example that was not being used.

* refactor 

Updating to most recent changes from PR-955 (yet to be merged)

* MTTDOC-317 initial page creation

* #MTTDOC-317 update to main page

* refactor

making changes to reflect the final version that was merged into the develop branch.

* MTTDOC-317 updating to ensure successful build

* MTTDOC-317 fixing some typos and naming

* update

Ran through this one more time removing any references to MLAPI.
I also tried to improve upon the text copy flow (might need a second pair of eyes)

* update 

minor language tweak and italicized the reference to object pooling link

* update

left out the trailing *

* Update object-pooling.md

* Update object-pooling.md

* Update object-pooling.md

* Delete networktime-ticks.md

Co-authored-by: Brian Coughlin <brian.coughlin@unity3d.com>
Co-authored-by: Briancoughlin <76997526+Briancoughlin@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants