Skip to content

fix: NetworkVar implementations now uses Time of their owner NetworkM… #788

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

Merged
merged 3 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -77,7 +78,7 @@ public NetworkDictionary(IDictionary<TKey, TValue> value)
public void ResetDirty()
{
m_DirtyEvents.Clear();
LastSyncedTime = NetworkManager.Singleton.NetworkTime;
LastSyncedTime = m_NetworkBehaviour.NetworkManager.NetworkTime;
}

/// <inheritdoc />
Expand Down Expand Up @@ -388,7 +389,7 @@ public bool IsDirty()
return false;
}

if (NetworkManager.Singleton.NetworkTime - LastSyncedTime >= (1f / Settings.SendTickrate))
if (m_NetworkBehaviour.NetworkManager.NetworkTime - LastSyncedTime >= (1f / Settings.SendTickrate))
{
return true;
}
Expand All @@ -403,7 +404,9 @@ public TValue this[TKey key]
get => m_Dictionary[key];
set
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we only need these EnsureInitialized inside the IsServer block, yes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in general I'm a little confused. This is an instance method, and we have m_NetworkBehaviour being initialized in the xtor (as private and readonly). Are we thinking somehow it could be nullified after the xtor? if it could be null at xtor time, then I would think we should throw an exception then. Concern is code clarity and the cost of doing these checks on every access.


if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_Dictionary[key] = value;
}
Expand Down Expand Up @@ -434,7 +437,9 @@ public TValue this[TKey key]
/// <inheritdoc />
public void Add(TKey key, TValue value)
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_Dictionary.Add(key, value);
}
Expand All @@ -452,7 +457,9 @@ public void Add(TKey key, TValue value)
/// <inheritdoc />
public void Add(KeyValuePair<TKey, TValue> item)
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_Dictionary.Add(item);
}
Expand All @@ -470,7 +477,9 @@ public void Add(KeyValuePair<TKey, TValue> item)
/// <inheritdoc />
public void Clear()
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_Dictionary.Clear();
}
Expand Down Expand Up @@ -510,7 +519,9 @@ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
/// <inheritdoc />
public bool Remove(TKey key)
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_Dictionary.Remove(key);
}
Expand All @@ -534,7 +545,9 @@ public bool Remove(TKey key)
/// <inheritdoc />
public bool Remove(KeyValuePair<TKey, TValue> item)
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_Dictionary.Remove(item);
}
Expand All @@ -558,9 +571,9 @@ IEnumerator IEnumerable.GetEnumerator()

private void HandleAddDictionaryEvent(NetworkDictionaryEvent<TKey, TValue> dictionaryEvent)
{
if (NetworkManager.Singleton.IsServer)
if (m_NetworkBehaviour.NetworkManager.IsServer)
{
if (NetworkManager.Singleton.ConnectedClients.Count > 0)
if (m_NetworkBehaviour.NetworkManager.ConnectedClients.Count > 0)
{
m_DirtyEvents.Add(dictionaryEvent);
}
Expand All @@ -581,6 +594,14 @@ public ushort RemoteTick
return NetworkTickSystem.NoTick;
}
}

private void EnsureInitialized()
{
if (m_NetworkBehaviour == null)
{
throw new InvalidOperationException("Cannot access " + nameof(NetworkDictionary<TKey, TValue>) + " before it's initialized");
}
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -75,7 +76,7 @@ public NetworkList(IList<T> value)
public void ResetDirty()
{
m_DirtyEvents.Clear();
LastSyncedTime = NetworkManager.Singleton.NetworkTime;
LastSyncedTime = m_NetworkBehaviour.NetworkManager.NetworkTime;
}

/// <inheritdoc />
Expand All @@ -96,7 +97,7 @@ public bool IsDirty()
return false;
}

if (NetworkManager.Singleton.NetworkTime - LastSyncedTime >= (1f / Settings.SendTickrate))
if (m_NetworkBehaviour.NetworkManager.NetworkTime - LastSyncedTime >= (1f / Settings.SendTickrate))
{
return true;
}
Expand Down Expand Up @@ -428,7 +429,9 @@ IEnumerator IEnumerable.GetEnumerator()
/// <inheritdoc />
public void Add(T item)
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_List.Add(item);
}
Expand All @@ -446,7 +449,9 @@ public void Add(T item)
/// <inheritdoc />
public void Clear()
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_List.Clear();
}
Expand Down Expand Up @@ -474,7 +479,9 @@ public void CopyTo(T[] array, int arrayIndex)
/// <inheritdoc />
public bool Remove(T item)
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_List.Remove(item);
}
Expand Down Expand Up @@ -504,7 +511,9 @@ public int IndexOf(T item)
/// <inheritdoc />
public void Insert(int index, T item)
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_List.Insert(index, item);
}
Expand All @@ -522,7 +531,9 @@ public void Insert(int index, T item)
/// <inheritdoc />
public void RemoveAt(int index)
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_List.RemoveAt(index);
}
Expand All @@ -543,7 +554,9 @@ public T this[int index]
get => m_List[index];
set
{
if (NetworkManager.Singleton.IsServer)
EnsureInitialized();

if (m_NetworkBehaviour.NetworkManager.IsServer)
{
m_List[index] = value;
}
Expand All @@ -561,9 +574,9 @@ public T this[int index]

private void HandleAddListEvent(NetworkListEvent<T> listEvent)
{
if (NetworkManager.Singleton.IsServer)
if (m_NetworkBehaviour.NetworkManager.IsServer)
{
if (NetworkManager.Singleton.ConnectedClients.Count > 0)
if (m_NetworkBehaviour.NetworkManager.ConnectedClients.Count > 0)
{
m_DirtyEvents.Add(listEvent);
}
Expand All @@ -584,6 +597,14 @@ public ushort RemoteTick
return NetworkTickSystem.NoTick;
}
}

private void EnsureInitialized()
{
if (m_NetworkBehaviour == null)
{
throw new InvalidOperationException("Cannot access " + nameof(NetworkList<T>) + " before it's initialized");
}
}
}

/// <summary>
Expand Down
Loading