Skip to content

Commit 7a99f62

Browse files
committed
Changed observer API
The observer set in OnRebuildObservers is no longer cleared before it's passed over
1 parent 6f0d8ca commit 7a99f62

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ public bool isSpawned
136136
/// <summary>
137137
/// The current clients observing the object
138138
/// </summary>
139-
public HashSet<uint> observers = new HashSet<uint>();
140-
private HashSet<uint> previousObservers = new HashSet<uint>();
139+
public readonly HashSet<uint> observers = new HashSet<uint>();
140+
private readonly HashSet<uint> previousObservers = new HashSet<uint>();
141141

142142
internal void RebuildObservers(uint? clientId = null)
143143
{
@@ -162,7 +162,6 @@ internal void RebuildObservers(uint? clientId = null)
162162
previousObservers.Clear();
163163
foreach (var item in observers)
164164
previousObservers.Add(item);
165-
observers.Clear();
166165
bool update = false;
167166
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
168167
{
@@ -199,6 +198,7 @@ internal void RebuildObservers(uint? clientId = null)
199198
foreach (var item in previousObservers)
200199
observers.Add(item);
201200
}
201+
previousObservers.Clear();
202202
}
203203
}
204204

MLAPI/MonoBehaviours/Prototyping/NetworkedProximity.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public override bool OnCheckObserver(uint newClientId)
8686
/// <returns>Wheter or not we changed anything</returns>
8787
public override bool OnRebuildObservers(HashSet<uint> observers)
8888
{
89+
// This implementation is an example.
90+
// Not efficient. We remove all old observers as the API doesn't clear them for us.
91+
// The reason it's not cleared is so that you don't have to iterate over your things if you simply
92+
// Have an event driven system where you want to remove a player. Ex if they leave a zone
93+
observers.Clear();
94+
observers = new HashSet<uint>();
95+
8996
if (ForceHidden)
9097
{
9198
// ensure player can still see themself

0 commit comments

Comments
 (0)