-
Notifications
You must be signed in to change notification settings - Fork 450
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
Conversation
com.unity.netcode.gameobjects/Runtime/Messaging/InternalMessageHandler.cs
Show resolved
Hide resolved
|
||
var networkObject = NetworkManager.SpawnManager.SpawnedObjects[networkId]; | ||
if (networkObject.OwnerClientId == NetworkManager.LocalClientId) | ||
if (!NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(networkObjectId, out var networkObject)) |
There was a problem hiding this comment.
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>()); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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!
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.