Skip to content
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

feat: Adds nested NetworkObject support in prefabs #2645

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

Clicky02
Copy link

@Clicky02 Clicky02 commented Jul 27, 2023

Allows users to spawn nested NetworkObjects in prefabs. This is done using a new dependent NetworkObjects system.

When you spawn a prefab with nested NetworkObjects, all of the nested NetworkObjects will also be spawned/synchronized. These nested NetworkObjects will be dependent on the root NetworkObject. This means they are initially synchronized in the same message as the root NetworkObject, and cannot outlive the root NetworkObject (when the root gets destroyed the depending NetworkObjects will be destroyed as well). Aside from these two exceptions, depending NetworkObjects behave identically to normal NetworkObjects.

To achieve this I made 3 major changes:

  1. I added two hidden serialized fields to NetworkObject for saving its dependent NetworkObject and depending NetworkObjects. Respectively, these are m_DependentNetworkObject and m_DependingNetworkObjects. They are determined in OnValidate and have public getters.
  2. When Spawn() is called on a NetworkObject, it locally spawns depending NetworkObjects as well.
  3. The SceneObject struct (which contains the data needed to spawn or initially synchronize NetworkObjects) now contains a list of DependingSceneObjects. This is a new struct that contains a subset of the properties of SceneObject along with a IsSpawned boolean. When a SceneObject is spawned remotely, the information in the DependingSceneObject list is used to initialize the depending NetworkObjects. Any DependingSceneObject where IsSpawned is false has its corresponding NetworkObject destroyed instead of initialized. This occurs when a depending NetworkObject has been destroyed before the NetworkObject it is dependent on.

Resolves #2637

Changelog

  • Added: The ability to spawn (and properly network) prefabs with nested NetworkObjects

Testing and Documentation

  • Adds tests to ensure that:
    1. Player prefabs can have nested NetworkObjects
    2. Depending NetworkObjects are reparented properly
    3. Depending NetworkObjects are deleted properly
    4. Dependent NetworkObjects are deleted properly

Introduces the concept of Dependent NetworkObjects. Dependent NetworkObjects cannot outlive the
NetworkObject they are dependent on. When spawning a prefab with nested NetworkObjects, all nested
NetworkObjects are dependent on the root NetworkObject. This means that nested NetworkObjects can
always be spawned/synchronized by spawning the root NetworkObject (and despawning any
NetworkObjects that are no longer spawned).

Resolves Unity-Technologies#2637
@Clicky02 Clicky02 requested a review from a team as a code owner July 27, 2023 03:13
@unity-cla-assistant
Copy link

unity-cla-assistant commented Jul 27, 2023

CLA assistant check
All committers have signed the CLA.

Cleaned up code to be more specific, remove a log statement, hide serialized dependent
NetworkObject fields in editor, and correct typos.
@Clicky02 Clicky02 marked this pull request as draft July 28, 2023 18:44
Removed unnecessary serialization code
added public property to see what NetworkObjects are depending on a NetworkObject
Fixed a bug throwing errors when spawning prefabs without children
Fixed a bug where dependent network objects wouldn't be reparented to root on late-joining clients
Fixed a bug where player prefabs could not have nested NetworkObjects
Fixed a bug where despawned NetworkObjects would attempt to be serialized
Validates that nested network objects work properly.
Updated changelog to include nested NetworkObjects
@Clicky02 Clicky02 marked this pull request as ready for review July 30, 2023 20:54
@Clicky02
Copy link
Author

PR has been updated to fix a number of issues and to add tests

@razveck
Copy link

razveck commented Jan 30, 2024

Any update on this? The PR is not merged

@Toastarve
Copy link

Still waiting after almost a year :(
In my current production it would be rather necessary to have this feature...

@SubatomicPlanets
Copy link

I need this too. This is the only feature that is making it impossible for me to use Netcode for GameObjects properly. Please make this happen!

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.

Add Support for Nested NetworkObjects in Prefabs
5 participants