From d09f5ef008cbf13703369a270249af37b60e3212 Mon Sep 17 00:00:00 2001 From: Robin Rolf Date: Tue, 30 Jan 2024 03:22:01 +0100 Subject: [PATCH] periodic rebuilds --- .../SpatialHashing/FastSpatialInterestManagement.cs | 13 ++++++++++++- .../InterestManagementTests_FastSpatialHashing.cs | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Components/InterestManagement/SpatialHashing/FastSpatialInterestManagement.cs b/Assets/Mirror/Components/InterestManagement/SpatialHashing/FastSpatialInterestManagement.cs index 61c12b3d5..3ee411c0b 100644 --- a/Assets/Mirror/Components/InterestManagement/SpatialHashing/FastSpatialInterestManagement.cs +++ b/Assets/Mirror/Components/InterestManagement/SpatialHashing/FastSpatialInterestManagement.cs @@ -13,6 +13,10 @@ public class FastSpatialInterestManagement : InterestManagementBase [Tooltip("The maximum range that objects will be visible at.")] public int visRange = 30; + [Tooltip("Rebuild all every 'rebuildInterval' seconds.")] + public float rebuildInterval = 1; + double lastRebuildTime; + // we use a 9 neighbour grid. // so we always see in a distance of 2 grids. // for example, our own grid and then one on top / below / left / right. @@ -62,7 +66,14 @@ internal void LateUpdate() // only on server if (!NetworkServer.active) return; - RebuildAll(); + // rebuild all spawned entities' observers every 'interval' + // this will call OnRebuildObservers which then returns the + // observers at grid[position] for each entity. + if (NetworkTime.localTime >= lastRebuildTime + rebuildInterval) + { + RebuildAll(); + lastRebuildTime = NetworkTime.localTime; + } } // When a new identity is spawned diff --git a/Assets/Mirror/Tests/Editor/InterestManagement/InterestManagementTests_FastSpatialHashing.cs b/Assets/Mirror/Tests/Editor/InterestManagement/InterestManagementTests_FastSpatialHashing.cs index 07970ab86..a36eeb7c1 100644 --- a/Assets/Mirror/Tests/Editor/InterestManagement/InterestManagementTests_FastSpatialHashing.cs +++ b/Assets/Mirror/Tests/Editor/InterestManagement/InterestManagementTests_FastSpatialHashing.cs @@ -41,6 +41,7 @@ public override void SetUp() aoi = holder.AddComponent(); aoi.visRange = 10; + aoi.rebuildInterval = -1; // rebuild every call :) // setup server aoi since InterestManagement Awake isn't called NetworkServer.aoi = aoi; }