Skip to content

feat: AttachableBehaviour and ComponentController #3518

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

Open
wants to merge 33 commits into
base: develop-2.0.0
Choose a base branch
from

Conversation

NoelStephensUnity
Copy link
Collaborator

@NoelStephensUnity NoelStephensUnity commented Jun 24, 2025

AttachableBehaviour and Support Components

The purpose of this PR (feat) is to address the complexity of "picking up" or "dropping" an item in the world which can become complex when using the traditional NetworkObject parenting pattern. In this PR there are three primary components added to help reduce this complexity:

  • AttachableBehaviour: Provides "out of the box" support for attaching (i.e. parenting) a nested child GameObject that includes an AttachableBehaviour component to another nested child GameObject with an AttachableNode component that is associated with a different NetworkObject.
  • AttachableNode: This component is required by the AttachableBehaviour component in order to be able to attach (i.e. parent) to another GameObject without having to parent the entire NetworkObject component the AttachableBehaviour component is associated with.
  • ComponentController: This component provides users the ability to synchronize the enabling or disabling of any Object derived component that has an enabled property.

This PR also incorporates a new "Helpers" subfolder under the NGO components folder where additional helper components will live.

Attaching vs NetworkObject parenting

Fundamentally, attaching is another form of synchronized (i.e. netcode) parenting that does not require one to use the traditional NetworkObject parenting. Attaching a child GameObject nested under a NeworkObject (really the GameObject the NetworkObject component belongs to) will only take the child GameObject and parent it under another child GameObject nested under or on the same GameObject with a different NeworkObject.

NetworkObject parenting

The traditional approach has been to spawn two network prefab instances:
image

Then parent one instance under the other:
image

This is simple enough for many scenarios, but can become cumbersome under more specific scenarios where a user might want to have a "world" version of the item and a "picked up" version of the item.

Attaching

With attaching, a user would create nested GameObject children that represent the item when it is picked up and when it is dropped/placed somewhere in the scene (i.e. world).
image

  • The WorldItemRoot is where the NetworkObject component is placed.
  • The NestedChild-World contains the components needed for the item when it is placed in the world.
  • The NestedChild-PickedUp contains the components needed for the item when it is picked up by a player.

By placing an AttachableBehaviour component on the NestedChild-PickedUp GameObject and an AttachableNode component on the TargetNode, a user can then invoke the AttachableBehaviour.Attach method while passing in the AttachableNode component and the NestedChild-PickedUp GameObject will get parented under the TargetNode while also synchronizing this action with all other clients.
image

ComponentController

Taking the above example into consideration, it would make sense that a user would want to be able to easily control whether a specific component is enabled or disabled. As an example:

  • When the WorldItemRoot is in the "placed in the world" state, it would make sense to disable any MeshRenderer, Collider, and other components on the NestedChild-PickedUp GameObject while enabling similar types of components on the NestedChild-World.
  • When the WorldItemRoot is in the "picked up" state, it would make sense to enable any MeshRenderer, Collider, and other components on the NestedChild-PickedUp GameObject while disabling similar types of components on the NestedChild-World.
  • It would also make sense to synchronize the enabling or disabling of components with all instances.

The ComponentController provides this type of functionality.

Example of synchronized RPC driven properties

Both the AttachableBehaviour and the ComponentController provide an example of using synchronized RPC driven properties in place of NetworkVariable. Under certain conditions it is better to use RPCs when a specific order of operations is needed as opposed to NetworkVariables which can update out of order (regarding the order in which certain states were updated) depending upon several edge case scenarios.

Under this condition using reliable RPCs will assure the messages are received in the order they were generated while also reducing the latency time between the change and the non-authority instances being notified of the change. Synchronized RPC driven properties only require overriding the NetworkBehaviour.OnSynchronize method and serializing any properties that need to be synchronized with late joining players or handling network object visibility related scenarios.

NetworkBehaviour.OnNetworkPreDespawn

Added another virtual method to NetworkBehaviour, OnNetworkPreDespawn, that is invoked before running through the despawn sequence for the NetworkObject and all NetworkBehaviour children of the NetworkObject being despawned. This provides an opportunity to do any kind of cleanup up or last micro-second state updates prior to despawning.

Changelog

  • Added: AttachableBehaviour helper component to provide an alternate approach to parenting items without using the NetworkObject parenting.
  • Added : AttachableNode helper component that is used by AttachableBehaviour as the target node for parenting.
  • Added: ComponentController helper component that can be used to synchronize the enabling and disabling of components and can be used in conjunction with AttachableBehaviour.
  • Added: NetworkBehaviour.OnNetworkPreDespawn that is invoked before running through the despawn sequence for the NetworkObject and all NetworkBehaviour children of the NetworkObject being despawned.

Testing and Documentation

  • Includes two new integration tests:
    • AttachableBehaviourTests
    • ComponentControllerTests
  • No internal API documentation changes or additions were necessary.
  • Requires public documentation that provides usage and examples (wip).

Backport

These new helper components are currently only targeting v2.x.

Adding AttachableBehaviour and ObjectController.
Renaming ObjectController to ComponentController.
Added some additional validation checking and handling.
Updated XML API.
Adding helpers meta.
Adding an AttachableNode as the target for AttachableBehaviour.
Refactoring AttachableBehaviour.
Adding new test for attachables.
Replacing any improperly spelled "detatch" with "detach".
XML API and private methods.
Minor XML API fixes.
Simplified the nameof AttachableBehaviour.Detach to just Detach.
Refactoring the ComponentController to provide more flexibility as well as being able to have component entries that will apply the inverse of the current ComponentController's current state....which allows for switching between different sets of components depending upon the controller's state.
Made some minor adjustments while writing the base test for ComponentController.
Adding the base ComponentController test.
switching to a Light component as opposed to BoxCollider as BoxCollider requires the physics package and we are just testing the functionality of ComponentController and not specifically any one other type of component.
Removing using directive.
Updating the change log entries.
@NoelStephensUnity NoelStephensUnity marked this pull request as ready for review July 10, 2025 19:43
@NoelStephensUnity NoelStephensUnity requested a review from a team as a code owner July 10, 2025 19:43
Removing debug log info.
Work in progress adjustments for usability under various scenarios.
Using RPCs and synchronizing the property being set using NetworkBehaviour.OnSynchronize in order to assure order of operations when it comes to messages.

Updated the ComponentController to be able to stagger state updates in the event this occurs (wip and I might remove this part and not allow changes to state until any pending state is finished).
Minor adjustment to the range for delays allow it to be zero.
Fixing spelling issues.
Had to make some minor adjustments in order to assure that users could handle sending any last micro-second tasks on any spawned instances prior to them despawning.

Added NetworkBehaviour.OnNetworkPreDespawn.
Did a slight order of operations on NetworkManager.Shutdown internal in order to assure sending RPCs during despawn would still work.

Minor adjustments to the new helper components associated with this PR.
Updating the AttachableBehaviourTests to include testing that an attachable will be automatically detatched upon despawning the AttachableNode it is attached to.
Updating changelog entry.
Doing a second inspector UI pass to include tool tips and rename each element item to the component's standard inspector view naming where it is the GameObject's name followed by the class name that is separated by capitalization and contained within parenthesis.
Fixing an exception that can occur when you have a network prefab opened for editing and then you delete the prefab asset before exiting the prefab edit mode.
Fixing some PVP related issues.
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.

1 participant