Skip to content

Conversation

EmandM
Copy link
Collaborator

@EmandM EmandM commented Sep 19, 2025

Purpose of this PR

Methods inside of properties are much less performant to access that fields or methods without properties.

This PR focusses on:

  1. Removing references to NetworkBehaviour.NetworkObject from within NetworkBehaviour. Instead using m_NetworkObject anywhere that will be called after the NetworkBehaviour is spawned.
    a. Ensure that the NetworkObject explicitly sets m_NetworkManager when it calculates whether or not a NetworkBehaviour belongs in it's ChildNetworkBehaviours list.
  2. Simplifies the areas where the NetworkBehaviour lifecycle callbacks are invoked
    a. Simplifies the callsites around NetworkBehaviour.OnLostOwnership and NetworkBehaviour.OnGainedOwnership
    b. Reduces loop duplication around NetworkBehaviour.OnNetworkSpawn

Jira ticket

Link to related jira ticket (Use the smart commits). Short version (e.g. MTT-123) also works and gets auto-linked
to be created.

Changelog

  • Fixed: Improved NetworkBehaviour performance.

Documentation

  • No documentation changes or additions were necessary.

Testing & QA (How your changes can be verified during release Playtest)

This change needs a full play of astroids, and potentially the OwnershipChanging demo. Everything here should be covered by the automated tests, but this is a hot enough codepath that playtesting is definitely needed here.

Functional Testing

Manual testing :

  • Manual testing done

Automated tests:

  • Covered by existing automated tests
  • Covered by new automated tests

Does the change require QA team to:

  • Review automated tests?
  • Execute manual tests?
  • Provide feedback about the PR?

If any boxes above are checked the QA team will be automatically added as a PR reviewer.

Backports

This is a pure performance improvement and so doesn't need a backport.

@EmandM EmandM requested a review from a team as a code owner September 19, 2025 15:46
@EmandM EmandM requested review from NoelStephensUnity and a team as code owners September 22, 2025 15:55
@michalChrobot
Copy link
Collaborator

When you will be ready you can ping me and I can organize the Playtest. Just note that I'm off next week

/// Invokes the <see cref="ChildNetworkBehaviours"/> <see cref="NetworkBehaviour.OnLostOwnership"/> and <see cref="NetworkBehaviour.OnGainedOwnership"/> events.
/// <see cref="NetworkSpawnManager.UpdateOwnershipTable"/> is called to update the ownership in-between the two callbacks.
/// </summary>
internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulong newOwnerClientId)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nicely done!
Collapsed, organized, and unified.
🥇

{
networkObject.InvokeBehaviourOnGainedOwnership();
}
// Notify lost ownership, update the ownership, then notify gained ownership for the network behaviours
Copy link
Collaborator

Choose a reason for hiding this comment

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

Flex the unification!
:godmode:

// Always notify locally on the server when a new owner is assigned
networkObject.InvokeBehaviourOnGainedOwnership();
// Notify lost ownership, update the ownership, then notify gained ownership for the network behaviours
networkObject.InvokeBehaviourOnOwnershipChanged(originalOwner, clientId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

🔔

protected IEnumerator WaitForSpawnedOnAllOrTimeOut(NetworkObject networkObject, TimeoutHelper timeOutHelper = null)
{
var networkObjectId = networkObject.GetComponent<NetworkObject>().NetworkObjectId;
var networkObjectId = networkObject.NetworkObjectId;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice addition!

Copy link
Collaborator

@NoelStephensUnity NoelStephensUnity left a comment

Choose a reason for hiding this comment

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

:godmode:

Very nice.... 👍

@NoelStephensUnity NoelStephensUnity enabled auto-merge (squash) September 24, 2025 20:40
@NoelStephensUnity NoelStephensUnity merged commit 47ed2dd into develop-2.0.0 Sep 25, 2025
27 checks passed
@NoelStephensUnity NoelStephensUnity deleted the chore/optimize-networkbehaviour-neworkobject-access branch September 25, 2025 00:12
NoelStephensUnity added a commit that referenced this pull request Sep 26, 2025
Changing #3687 adjustment to use the version of SpawnNetworkObjectLocally that accepts a SceneObject which doesn't invoke pre-spawn but does invoke post spawn and processes deferred messages.
These were moved in this PR to assure post spawn was invoked after the object was 100% done locally spawning and as a last step process any deferred messages targeting this object.
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.

3 participants