add way to track values and when they changed

This commit is contained in:
D1GQ committed 2026-03-18 01:07:43 -05:00
1 parent 30430dc95b
commit 5cb055f04d
2 files changed
+46 -5

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
namespace BetterAmongUs.Modules;
/// <summary>
/// Tracks a value and records the last time it was changed.
/// </summary>
/// <typeparam name="T">The type of value to track.</typeparam>
internal sealed class ValueTracker<T>
{
private T? _currentValue;
private T? _previousTrackedValue;
private float _lastChangeTime;
/// <summary>
/// Gets the current tracked value.
/// </summary>
public T? Value => _currentValue;
/// <summary>
/// Gets the time elapsed since the last value change.
/// </summary>
internal float TimeSinceLastChange => UnityEngine.Time.time - _lastChangeTime;
/// <summary>
/// Updates the tracked value if it differs from the current value.
/// </summary>
/// <param name="newValue">The new value to set.</param>
internal void Update(T newValue)
{
_currentValue = newValue;
if (!Equals(newValue, _previousTrackedValue))
{
_previousTrackedValue = newValue;
_lastChangeTime = UnityEngine.Time.time;
}
}
}
+9 -5
View File
@@ -59,15 +59,19 @@ internal sealed class ExtendedPlayerControl : MonoBehaviour, IMonoExtension<Play
}
}
internal readonly ValueTracker<bool> InVentTracker = new();
private void Update()
{
if (_Player == null)
return;
InVentTracker.Update(_Player.inVent);
}
private void OnDestroy()
{
this.UnregisterExtension();
}
/// <summary>
/// Dictionary storing last name set for each player.
/// </summary>
internal readonly Dictionary<NetworkedPlayerInfo, string> NameSetLastFor = [];
}
/// <summary>