Skip to content

Commit

Permalink
feat: Added Reset to Interest Management (#3008)
Browse files Browse the repository at this point in the history
* feat: Added Reset to Interest Management
- Used in Distance and Spatial to reset `lastRebuildTime`.

* Updated Template

* Update Assets/Mirror/Runtime/NetworkServer.cs

Co-authored-by: vis2k <info@noobtuts.com>
  • Loading branch information
MrGadget1024 and miwarnec committed Dec 2, 2021
1 parent 9862af3 commit 38c641b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
Expand Up @@ -20,6 +20,12 @@ int GetVisRange(NetworkIdentity identity)
return custom != null ? custom.visRange : visRange;
}

[ServerCallback]
public override void Reset()
{
lastRebuildTime = 0D;
}

public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnection newObserver)
{
int range = GetVisRange(identity);
Expand Down
Expand Up @@ -60,6 +60,12 @@ public override void OnRebuildObservers(NetworkIdentity identity, HashSet<Networ
grid.GetWithNeighbours(current, newObservers);
}

[ServerCallback]
public override void Reset()
{
lastRebuildTime = 0D;
}

// update everyone's position in the grid
// (internal so we can update from tests)
[ServerCallback]
Expand Down
2 changes: 2 additions & 0 deletions Assets/Mirror/Runtime/InterestManagement.cs
Expand Up @@ -25,6 +25,8 @@ void Awake()
else Debug.LogError($"Only one InterestManagement component allowed. {NetworkClient.aoi.GetType()} has been set up already.");
}

public virtual void Reset() {}

// Callback used by the visibility system to determine if an observer
// (player) can see the NetworkIdentity. If this function returns true,
// the network connection will be added as an observer.
Expand Down
9 changes: 8 additions & 1 deletion Assets/Mirror/Runtime/NetworkServer.cs
Expand Up @@ -60,17 +60,22 @@ static void Initialize()
if (initialized)
return;

initialized = true;
// Debug.Log($"NetworkServer Created version {Version.Current}");

//Make sure connections are cleared in case any old connections references exist from previous sessions
connections.Clear();

// reset Interest Management so that rebuild intervals
// start at 0 when starting again.
if (aoi != null) aoi.Reset();

// reset NetworkTime
NetworkTime.Reset();

Debug.Assert(Transport.activeTransport != null, "There was no active transport when calling NetworkServer.Listen, If you are calling Listen manually then make sure to set 'Transport.activeTransport' first");
AddTransportHandlers();

initialized = true;
}

static void AddTransportHandlers()
Expand Down Expand Up @@ -180,6 +185,8 @@ public static void Shutdown()
// we don't want to use those hooks after Shutdown anymore.
OnConnectedEvent = null;
OnDisconnectedEvent = null;

if (aoi != null) aoi.Reset();
}

// connections /////////////////////////////////////////////////////////
Expand Down
Expand Up @@ -78,6 +78,15 @@ public class #SCRIPTNAME# : InterestManagement
base.SetHostVisibility(identity, visible);
}

/// <summary>
/// Called by NetworkServer in Initialize and Shutdown
/// </summary>
[ServerCallback]
public override void Reset()
{
base.Reset();
}

[ServerCallback]
void Update()
{
Expand Down

0 comments on commit 38c641b

Please sign in to comment.