Skip to content

Commit 1da76b2

Browse files
fix!: added plainly-callable Add() method to NetworkSet [MTT-1005] (#1022)
* fix!: added NetworkSet plainly-callable Add() method MTT-1005
1 parent d71dd1b commit 1da76b2

File tree

2 files changed

+23
-39
lines changed

2 files changed

+23
-39
lines changed

com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkSet.cs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -331,29 +331,6 @@ IEnumerator IEnumerable.GetEnumerator()
331331
return m_Set.GetEnumerator();
332332
}
333333

334-
/// <inheritdoc />
335-
void ICollection<T>.Add(T item)
336-
{
337-
EnsureInitialized();
338-
339-
if (m_NetworkBehaviour.NetworkManager.IsServer)
340-
{
341-
m_Set.Add(item);
342-
}
343-
344-
var setEvent = new NetworkSetEvent<T>()
345-
{
346-
Type = NetworkSetEvent<T>.EventType.Add,
347-
Value = item
348-
};
349-
m_DirtyEvents.Add(setEvent);
350-
351-
if (m_NetworkBehaviour.NetworkManager.IsServer && OnSetChanged != null)
352-
{
353-
OnSetChanged(setEvent);
354-
}
355-
}
356-
357334
/// <inheritdoc />
358335
public void ExceptWith(IEnumerable<T> other)
359336
{
@@ -478,8 +455,7 @@ public void UnionWith(IEnumerable<T> other)
478455
}
479456
}
480457

481-
/// <inheritdoc />
482-
bool ISet<T>.Add(T item)
458+
public void Add(T item)
483459
{
484460
EnsureInitialized();
485461

@@ -499,10 +475,21 @@ bool ISet<T>.Add(T item)
499475
{
500476
OnSetChanged(setEvent);
501477
}
478+
}
502479

480+
/// <inheritdoc />
481+
bool ISet<T>.Add(T item)
482+
{
483+
Add(item);
503484
return true;
504485
}
505486

487+
/// <inheritdoc />
488+
void ICollection<T>.Add(T item)
489+
{
490+
Add(item);
491+
}
492+
506493
/// <inheritdoc />
507494
public void Clear()
508495
{

com.unity.multiplayer.mlapi/Tests/Runtime/NetworkVariableTests.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections;
3-
using System.Collections.Generic;
43
using UnityEngine;
54
using UnityEngine.TestTools;
65
using NUnit.Framework;
@@ -101,6 +100,7 @@ public override IEnumerator Setup()
101100

102101
// This is the *SERVER VERSION* of the *CLIENT PLAYER*
103102
var serverClientPlayerResult = new MultiInstanceHelpers.CoroutineResultWrapper<NetworkObject>();
103+
104104
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation(
105105
x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId,
106106
m_ServerNetworkManager, serverClientPlayerResult));
@@ -118,14 +118,16 @@ public override IEnumerator Setup()
118118
m_ClientComp = clientSideClientPlayer.GetComponent<NetworkVariableTest>();
119119

120120
m_ServerComp.TheList.Clear();
121+
m_ServerComp.TheSet.Clear();
122+
m_ServerComp.TheDictionary.Clear();
121123

122-
if (m_ServerComp.TheList.Count > 0)
124+
if (m_ServerComp.TheList.Count > 0 || m_ServerComp.TheSet.Count > 0 || m_ServerComp.TheDictionary.Count > 0)
123125
{
124-
throw new Exception("server network list not empty at start");
126+
throw new Exception("at least one server network container not empty at start");
125127
}
126-
if (m_ClientComp.TheList.Count > 0)
128+
if (m_ClientComp.TheList.Count > 0 || m_ClientComp.TheSet.Count > 0 || m_ClientComp.TheDictionary.Count > 0)
127129
{
128-
throw new Exception("client network list not empty at start");
130+
throw new Exception("at least one client network container not empty at start");
129131
}
130132
}
131133

@@ -178,8 +180,6 @@ public IEnumerator AllNetworkVariableTypes()
178180
[UnityTest]
179181
public IEnumerator NetworkListAdd()
180182
{
181-
var waitResult = new MultiInstanceHelpers.CoroutineResultWrapper<bool>();
182-
183183
yield return MultiInstanceHelpers.RunAndWaitForCondition(
184184
() =>
185185
{
@@ -245,9 +245,8 @@ public IEnumerator NetworkSetAdd()
245245
yield return MultiInstanceHelpers.RunAndWaitForCondition(
246246
() =>
247247
{
248-
ISet<int> iSet = m_ServerComp.TheSet;
249-
iSet.Add(k_TestVal1);
250-
iSet.Add(k_TestVal2);
248+
m_ServerComp.TheSet.Add(k_TestVal1);
249+
m_ServerComp.TheSet.Add(k_TestVal2);
251250
},
252251
() =>
253252
{
@@ -272,8 +271,7 @@ public IEnumerator NetworkSetRemove()
272271
yield return MultiInstanceHelpers.RunAndWaitForCondition(
273272
() =>
274273
{
275-
ISet<int> iSet = m_ServerComp.TheSet;
276-
iSet.Remove(k_TestVal1);
274+
m_ServerComp.TheSet.Remove(k_TestVal1);
277275
},
278276
() =>
279277
{
@@ -296,8 +294,7 @@ public IEnumerator NetworkSetClear()
296294
yield return MultiInstanceHelpers.RunAndWaitForCondition(
297295
() =>
298296
{
299-
ISet<int> iSet = m_ServerComp.TheSet;
300-
iSet.Clear();
297+
m_ServerComp.TheSet.Clear();
301298
},
302299
() =>
303300
{

0 commit comments

Comments
 (0)