-
Notifications
You must be signed in to change notification settings - Fork 450
feat: owner-writable NetworkVariable<T>
#1762
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
07aed2e
to
d174d40
Compare
OwnerNetworkVariable
NetworkVariable<T>
a3562e4
to
d174d40
Compare
[TestFixtureSource(nameof(TestDataSource))] | ||
public class NetworkVariablePermissionTests : NetcodeIntegrationTest | ||
{ | ||
public static IEnumerable<TestFixtureData> TestDataSource() |
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.
😍
yield return null; | ||
} | ||
|
||
private void AssertOwnerWritableAreEqualOnAll() |
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.
🎺
@@ -595,9 +593,11 @@ private void NetworkVariableUpdate(ulong clientId, int behaviourIndex) | |||
var shouldSend = false; | |||
for (int k = 0; k < NetworkVariableFields.Count; k++) | |||
{ | |||
if (NetworkVariableFields[k].ShouldWrite(clientId, IsServer)) | |||
var networkVariable = NetworkVariableFields[k]; | |||
if (networkVariable.IsDirty() && networkVariable.CanClientRead(targetClientId)) |
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.
What is the rationale for copying the code from ShouldWrite
and editing it here, as opposed to improving/adjusting/modifying ShouldWrite
? Just wondering what will make the code more easily maintainable.
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.
because there is no ShouldWrite
anymore. there are CanClientRead
and CanClientWrite
for perm checks, whether or not to write (send) is determined outside, here.
@@ -429,8 +425,7 @@ internal void SnapshotDespawn() | |||
internal void SnapshotDespawn(ulong clientId) | |||
{ | |||
var command = GetDespawnCommand(); | |||
command.TargetClientIds = new List<ulong>(); | |||
command.TargetClientIds.Add(clientId); | |||
command.TargetClientIds = new List<ulong> { clientId }; |
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.
Ok, yeah, those are great changes, but they make the PR harder to process and possibly harder to merge later. We should try to have PRs that address just one thing and side clean-up PRs are fine too :-). < /nitpick> < /jeffshouldgethiscoffee>
@@ -31,9 +31,9 @@ public void Serialize(FastBufferWriter writer) | |||
writer.WriteValue(NetworkObjectId); | |||
writer.WriteValue(NetworkBehaviourIndex); | |||
|
|||
for (int k = 0; k < NetworkBehaviour.NetworkVariableFields.Count; k++) | |||
for (int i = 0; i < NetworkBehaviour.NetworkVariableFields.Count; i++) |
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.
:headscratch:
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.
lol, sorry
/// </summary> | ||
/// <param name="values">The initial value to use for the NetworkList</param> | ||
public NetworkList(IEnumerable<T> values) | ||
public NetworkList(IEnumerable<T> values = 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.
😻
/// </summary> | ||
/// <param name="value">The initial value to use for the NetworkVariable</param> | ||
public NetworkVariable(T value) | ||
public NetworkVariable(T value = 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.
😻
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.
Other than adding a PR number to the end of your changelog entry, it looks good to me!
@@ -10,9 +10,12 @@ Additional documentation and release notes are available at [Multiplayer Documen | |||
|
|||
### Added | |||
|
|||
- Added `NetworkVariableWritePermission` to `NetworkVariableBase` and implemented `Owner` writable netvars. (#1762) |
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.
dunno if we want to mention this adds client driven capabilities? Just so users do the association?
testCompServer.OwnerWritable_Position.Value = newValue; | ||
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 2); | ||
|
||
AssertOwnerWritableAreEqualOnAll(); |
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.
if for whatever reason server side Value gets reverted back to it's original value and then client side the Value never changes, this test would still pass :(
with these, I usually prefer to set very specific values and make sure they are the ones expected on arrival.
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.
@SamuelBellomo Ok, I see what you are saying...
@MFatihMAR So maybe you could do this:
private void AssertServerWritableAreEqualOnAll(Vector3 expectedPosition)
{
var testObjServer = m_ServerNetworkManager.SpawnManager.SpawnedObjects[m_TestObjId];
var testCompServer = testObjServer.GetComponent<NetVarPermTestComp>();
foreach (var clientNetworkManager in m_ClientNetworkManagers)
{
var testObjClient = clientNetworkManager.SpawnManager.SpawnedObjects[m_TestObjId];
var testCompClient = testObjClient.GetComponent<NetVarPermTestComp>();
Assert.AreEqual(testCompServer.ServerWritable_Position.Value, expectedPosition);
Assert.AreEqual(testCompServer.ServerWritable_Position.Value, testCompClient.ServerWritable_Position.Value);
Assert.AreEqual(testCompServer.ServerWritable_Position.ReadPerm, testCompClient.ServerWritable_Position.ReadPerm);
Assert.AreEqual(testCompServer.ServerWritable_Position.WritePerm, testCompClient.ServerWritable_Position.WritePerm);
}
}
And update your tests to pass in the expected new value to AssertServerWritableAreEqualOnAll?
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.
technically that's being caught but the fact that it's hard to imply made me add an extra assert to compare .Value
against newValue
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ClientNetworkManagers[clientManagerIndex], 4); | ||
|
||
AssertOwnerWritableAreEqualOnAll(); | ||
} |
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 would also check that a non owner can't write on that netvar, but both owner and non-owner can still read
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 would also check that the server can't write to those, in addition to another client that's not the owner.
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.
implemented NetworkVariablePermissionTests.ServerCannotChangeOwnerWritableNetVar()
test
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ClientNetworkManagers[clientManagerIndex], 4); | ||
Assert.AreEqual(testCompServer.ServerWritable_Position.Value, oldValue); | ||
|
||
AssertServerWritableAreEqualOnAll(); |
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.
same here, if the value did change, even with throwing the exception, this would still pass
By adding
enum NetworkVariableWritePermission
and Owner/Server options, we're also implementing owner-writable network variables which allows owning clients to change value immediately and replicate that across the network via server.MTT-2845
Changelog
com.unity.netcode.gameobjects
NetworkVariableWritePermission
toNetworkVariableBase
and implementedOwner
writable netvars.Testing and Documentation
NetworkVariablePermissionTests