-
Notifications
You must be signed in to change notification settings - Fork 450
fix: NetworkBehaviour Property Initialization Updates #1785
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
fix: NetworkBehaviour Property Initialization Updates #1785
Conversation
@@ -237,40 +237,41 @@ internal void __endSendClientRpc(ref FastBufferWriter bufferWriter, uint rpcMeth | |||
/// <summary> | |||
/// Gets if the object is the the personal clients player object | |||
/// </summary> | |||
public bool IsLocalPlayer => NetworkObject.IsLocalPlayer; | |||
public bool IsLocalPlayer => m_NetworkObject != null ? m_NetworkObject.IsLocalPlayer : 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.
Is this actually an improvement though? If the NO is null, I think IsLocalPlayer
should be undefined / access should crash as we actually don't really know. If there's code later that wants to tolerate not having the NO set up yet, fine, then let that code do this check. Also, since we call this frequently...
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.
I went ahead and made this a private set and just initialize it during the spawning/despawning stages.
It removes the processing overhead and is cleaner.
public ulong OwnerClientId => NetworkObject.OwnerClientId; | ||
public ulong OwnerClientId { get; internal set; } | ||
|
||
internal void UpdatePropertyStates() |
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.
internal void UpdatePropertyStates() | |
internal void UpdateNetworkProperties() |
internal void UpdateOwnership() | ||
{ | ||
if (NetworkObject != null) | ||
{ | ||
IsOwnedByServer = NetworkObject.IsOwnedByServer; | ||
IsOwner = NetworkObject.IsOwner; | ||
OwnerClientId = NetworkObject.OwnerClientId; | ||
} | ||
else // Shouldn't happen, but if it does then just clear out everything | ||
{ | ||
IsOwnedByServer = IsOwner = false; | ||
OwnerClientId = 0; | ||
} | ||
} |
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.
move this logic into UpdateNetworkProperties()
and maintain updating all net props in one place. easier to maintain.
|
||
/// <summary> | ||
/// Gets called when the local client gains ownership of this object | ||
/// </summary> | ||
public virtual void OnGainedOwnership() { } | ||
|
||
internal void GainedOwnership() |
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.
internal void GainedOwnership() | |
internal void InternalOnGainedOwnership() |
/// <summary> | ||
/// Gets called when we loose ownership of this object | ||
/// </summary> | ||
public virtual void OnLostOwnership() { } | ||
|
||
internal void LostOwnership() |
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.
internal void LostOwnership() | |
internal void InternalOnLostOwnership() |
private bool m_IsServer; | ||
/// <summary> | ||
/// Gets if we are executing as server | ||
/// </summary> | ||
protected bool IsServer => IsRunning && NetworkManager.IsServer; | ||
protected bool IsServer { get { return m_IsServer; } } |
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.
I'd suggest a simpler syntax like:
protected bool IsServer { get; private set; }
and for other properties too :)
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.
and you don't need to have another field to back it up.
@@ -366,21 +392,56 @@ protected NetworkBehaviour GetNetworkBehaviour(ushort behaviourId) | |||
|
|||
internal void InternalOnNetworkSpawn() |
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.
I like the fact that now we are relying on OnNetworkSpawn
and OnNetworkDespawn
now <3
} | ||
else // Shouldn't happen, but if so then set the properties to their default value; | ||
{ | ||
OwnerClientId = NetworkObjectId = default; |
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.
Is this an error condition we should log? Would be a way to spot mis-use / regressions
Accessing specific properties of NetworkBehaviour can too easily cause exceptions to occur. Depending upon frequency of usage, they can also impact performance unnecessarily.
MTT-2785
Changelog
com.unity.netcode.gameobjects
Testing and Documentation