perf: Distance Interest Management caches custom ranges to avoid runtime TryGetComponent overhead (#3372)

* fix: Distance Interest Mgmt static dictionary

* Clear on Reset

* Allow for changing range at runtime

* Use OnSpawned / OnDestroyed

* reverted default visRange

* Update DistanceInterestManagement.cs

* Update DistanceInterestManagement.cs

---------

Co-authored-by: mischa <16416509+vis2k@users.noreply.github.com>
This commit is contained in:
MrGadget 2023-02-07 23:02:55 -05:00 committed by GitHub
parent 340a4b907a
commit 229a1a6509
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,16 +14,32 @@ public class DistanceInterestManagement : InterestManagement
public float rebuildInterval = 1;
double lastRebuildTime;
// cache custom ranges to avoid runtime TryGetComponent lookups
readonly Dictionary<NetworkIdentity, DistanceInterestManagementCustomRange> CustomRanges = new Dictionary<NetworkIdentity, DistanceInterestManagementCustomRange>();
// helper function to get vis range for a given object, or default.
[ServerCallback]
int GetVisRange(NetworkIdentity identity)
{
return identity.TryGetComponent(out DistanceInterestManagementCustomRange custom) ? custom.visRange : visRange;
return CustomRanges.TryGetValue(identity, out DistanceInterestManagementCustomRange custom) ? custom.visRange : visRange;
}
[ServerCallback]
public override void Reset()
{
lastRebuildTime = 0D;
CustomRanges.Clear();
}
public override void OnSpawned(NetworkIdentity identity)
{
if (identity.TryGetComponent(out DistanceInterestManagementCustomRange custom))
CustomRanges[identity] = custom;
}
public override void OnDestroyed(NetworkIdentity identity)
{
CustomRanges.Remove(identity);
}
public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnectionToClient newObserver)