-
Notifications
You must be signed in to change notification settings - Fork 450
feat: client network variables #1522
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
Changes from all commits
515e246
702d1ee
2c2cd36
f6975e1
062bf78
e581a45
f3f8901
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
|
||
namespace Unity.Netcode | ||
{ | ||
/// <summary> | ||
/// A ClientNetworkVariable is special in that: | ||
/// - only the owner of the variable can write to it | ||
/// - not even the server can write to it | ||
/// - it is not snapshotted | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought the direction we were taking was for everything to be snapshotted. AFAIK the client can send snapshots to the server as well. |
||
/// - it must be sent reliably | ||
/// | ||
/// (This class may be removed in the future when integrated into NetworkVariable natively) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Glad I read this comment, I was gonna ask this question. |
||
/// </summary> | ||
[Serializable] | ||
public class ClientNetworkVariable<T> : NetworkVariable<T> where T : unmanaged | ||
{ | ||
/// <summary> | ||
/// Creates a ClientNetworkVariable with the default read permission | ||
/// </summary> | ||
public ClientNetworkVariable() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a ClientNetworkVariable with a initial value | ||
/// </summary> | ||
/// <param name="value">The initial value to use for the ClientNetworkVariable</param> | ||
public ClientNetworkVariable(T value) : base(value) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a ClientNetworkVariable with a specified read permission | ||
/// </summary> | ||
/// <param name="readPerm">The readPermission to use</param> | ||
public ClientNetworkVariable(NetworkVariableReadPermission readPerm) : base(readPerm) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a ClientNetworkVariable with a initial value and the specified read permission | ||
/// </summary> | ||
/// <param name="readPerm">The initial read permission to use for the ClientNetworkVariable</param> | ||
/// <param name="value">The initial value to use for the ClientNetworkVariable</param> | ||
public ClientNetworkVariable(NetworkVariableReadPermission readPerm, T value) : base(readPerm, value) | ||
{ | ||
} | ||
|
||
public override bool CanClientWrite(ulong clientId) | ||
{ | ||
return m_NetworkBehaviour.OwnerClientId == clientId; | ||
} | ||
|
||
public override bool ShouldWrite(ulong clientId, bool isServer) | ||
{ | ||
return m_IsDirty && !isServer && m_NetworkBehaviour.IsOwner; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A host wouldn't be able to write? If my character is client driven, I don't want different code for my host and my clients, both should be able to update their own characters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, hosts should be able to write these, although I still yearn for the day when a "host" is just a client and a server running in the same process but each having their own version of each object. |
||
} | ||
|
||
/// <summary> | ||
/// The value of the ClientNetworkVariable container | ||
/// </summary> | ||
public override T Value | ||
{ | ||
get => m_InternalValue; | ||
set | ||
{ | ||
// this could be improved. The Networking Manager is not always initialized here | ||
// Good place to decouple network manager from the network variable | ||
|
||
// Also, note this is not really very water-tight, if you are running as a host | ||
// we cannot tell if a ClientNetworkVariable write is happening inside server-ish code | ||
if (m_NetworkBehaviour && m_NetworkBehaviour.NetworkManager.IsServer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be an opportunity to think about how we can decouple this from NetworkBehaviour and NetworkManager. We have NetworkBehaviour doing some initialization work on network variables, what about removing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably also needs to pass OwnerClientId. Or possibly it could pass in delegates for "can write" and "can read" instead of booleans. That could give us the opportunity to give more advanced permission control to the user and let them write more complicated ownership and authority logic. |
||
{ | ||
throw new InvalidOperationException("Server not allowed to write to ClientNetworkVariables"); | ||
} | ||
Set(value); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -69,6 +69,16 @@ public virtual bool ShouldWrite(ulong clientId, bool isServer) | |||||
return IsDirty() && isServer && CanClientRead(clientId); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Gets Whether or not a specific client can read to the varaible | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// </summary> | ||||||
/// <param name="clientId">The clientId of the remote client</param> | ||||||
/// <returns>Whether or not the client can read to the variable</returns> | ||||||
public virtual bool CanClientWrite(ulong clientId) | ||||||
{ | ||||||
return false; | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Gets Whether or not a specific client can read to the varaible | ||||||
/// </summary> | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.
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 might have missed this with Owernship, but to me this would be covered by the IsOwner check no? IsOwner checks for
NetworkManager != null && OwnerClientId == NetworkManager.LocalClientId;
This would be false for the server?