Skip to content

fix: change OwnerClientId before firing OnGainedOwnership() callback #1092

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 2 commits into from
Aug 25, 2021

Conversation

0xFA11
Copy link
Contributor

@0xFA11 0xFA11 commented Aug 25, 2021

fixes #1075


Summary

we were calling OnLostOwnership() and OnGainedOwnership() callbacks before we change NetworkObject.OwnerClientId property.

with this PR, we're calling OnLostOwnership() callback, change NetworkObject.OwnerClientId and then call OnGainedOwnership() callback.

this makes it more intiutive and useful (arguably) for developers. now they can handle losing ownership in their OnLostOwnership() callback while still having previous OwnerClientId and then later on, they can also handle latest/new ownership change in OnGainedOwnership() callback to do something with new OwnerClientId.


var networkObject = NetworkManager.SpawnManager.SpawnedObjects[networkId];
if (networkObject.OwnerClientId == NetworkManager.LocalClientId)
if (!NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(networkObjectId, out var networkObject))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice... trap ahead of time for invalid usage or non-existent NetworkObjects.

m_DummyPrefab = new GameObject("DummyPrefabPrototype");
m_DummyPrefab.AddComponent<NetworkObject>();
m_DummyPrefab.AddComponent<NetworkObjectOwnershipComponent>();
MultiInstanceHelpers.MakeNetworkedObjectTestPrefab(m_DummyPrefab.GetComponent<NetworkObject>());
Copy link
Collaborator

Choose a reason for hiding this comment

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

For this test you probably don't need to worry about it but I wanted to bring this to your attention.
When we create prefabs using MultiInstanceHelpers.MakeNetworkedObjectTestPrefab I am going to propose we make it a "best practice" to give it some form of GlobalObjectIdHash value other than default (i.e zero). It looks like it will work out for you since you are just dealing with 1 NetworkPrefab...but if you had more than one it would become problematic since we synchronize everything off of the GlobalObjectIdHash value.
Not requesting you change it, but just making you aware of that area of "pain" to watch out for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MakeNetworkedObjecttestPrefab kind of does that for us:

public static void MakeNetworkedObjectTestPrefab(NetworkObject networkObject, uint globalObjectIdHash = default)
{
    // Set a globalObjectId for prefab
    if (globalObjectIdHash != default)
    {
        networkObject.TempGlobalObjectIdHashOverride = globalObjectIdHash;
    }

    // Force generation
    networkObject.GenerateGlobalObjectIdHash();

    // Prevent object from being snapped up as a scene object
    networkObject.IsSceneObject = false;
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

default value of uint is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

0 but we force generate if it's default. if it's not default (0) then we override its hash with whatever we passed in. this code block covers both use cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you were right! addressing with #1094

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.

Liked the using usage... (lol)
And just provided a minor note on GlobalObjectIdHash values and creating Network Prefab on the fly.
Other than that it looks good to me!

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.

2 participants