Skip to content

Commit 32f4732

Browse files
fix: decoupling SceneLoaderWrapper and ConnectionStates [MTT-6242] (#830)
* removing SceneLoaderWrapper.AddOnSceneEventCallback and replacing it with registrations to NetworkManager callbacks
1 parent 0c98045 commit 32f4732

File tree

6 files changed

+39
-23
lines changed

6 files changed

+39
-23
lines changed

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Threading.Tasks;
3-
using Unity.Multiplayer.Samples.Utilities;
43
using UnityEngine;
54

65
namespace Unity.BossRoom.ConnectionManagement
@@ -68,8 +67,6 @@ internal async Task ConnectClientAsync()
6867
{
6968
throw new Exception("NetworkManager StartClient failed");
7069
}
71-
72-
SceneLoaderWrapper.Instance.AddOnSceneEventCallback();
7370
}
7471
catch (Exception e)
7572
{

Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections;
32
using Unity.BossRoom.Infrastructure;
43
using Unity.BossRoom.UnityServices.Lobbies;
54
using Unity.Multiplayer.Samples.BossRoom;
@@ -26,8 +25,6 @@ class HostingState : OnlineState
2625

2726
public override void Enter()
2827
{
29-
SceneLoaderWrapper.Instance.AddOnSceneEventCallback();
30-
3128
//The "BossRoom" server always advances to CharSelect immediately on start. Different games
3229
//may do this differently.
3330
SceneLoaderWrapper.Instance.LoadScene("CharSelect", useNetworkSceneManager: true);

Assets/Tests/Runtime/ConnectionManagementTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public override void Awake()
5555

5656
public override void Start() { }
5757

58-
public override void AddOnSceneEventCallback() { }
59-
6058
public override void LoadScene(string sceneName, bool useNetworkSceneManager, LoadSceneMode loadSceneMode = LoadSceneMode.Single) { }
6159
}
6260

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
1111
### Changed
1212
* Replaced our polling for lobby updates with a subscription to the new Websocket based LobbyEvents (#805). This saves up a significant amount of bandwidth usage to and from the service, since updates are infrequent in this game. Now clients and hosts only use up bandwidth on the Lobby service when it is needed. With polling, we used to send a GET request per client once every 2s. The responses were between ~550 bytes and 900 bytes, so if we suppose an average of 725 bytes and 100 000 concurrent users (CCU), this amounted to around 725B * 30 calls per minute * 100 000 CCU = 2.175 GB per minute. Scaling this to a month would get us 93.96 TB per month. In our case, since the only changes to the lobbies happen when a user connects or disconnects, most of that data was not necessary and can be saved to reduce bandwidth usage. Since the cost of using the Lobby service depends on bandwidth usage, this would also save money on an actual game.
1313
* Simplified reconnection flow by offloading responsibility to ConnectionMethod (#804). Now the ClientReconnectingState uses the ConnectionMethod it is configured with to handle setting up reconnection (i.e. reconnecting to the Lobby before trying to reconnect to the Relay server if it is using Relay and Lobby). It can now also fail early and stop retrying if the lobby doesn't exist anymore.
14-
* Replaced our custom pool implementation using queues with ObjectPool (#824)
15-
* Upgraded Boss Room to NGO 1.3.1 (#828) NetworkPrefabs inside NetworkManager's NetworkPrefabs list have been converted to NetworkPrefabsList ScriptableObject.
14+
* Replaced our custom pool implementation using queues with ObjectPool (#824)(#827)
15+
* Upgraded Boss Room to NGO 1.3.1 (#828) NetworkPrefabs inside NetworkManager's NetworkPrefabs list have been converted to NetworkPrefabsList ScriptableObject.
1616
* Upgraded Boss Room to NGO 1.4.0 (#829)
1717
* Profile names generated are now only 30 characters or under to fit Authentication Service requirements (#831)
1818

@@ -27,6 +27,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2727
* ClientConnectedState now inherits from OnlineState instead of the base ConnectionState (#801)
2828
* UpdateRunner now sends the right value for deltaTime when updating its subscribers (#805)
2929
* Inputs are better sanitized when entering IP address and port (#821). Now all invalid characters are prevented, and UnityTransport's NetworkEndpoint.TryParse is used to verify the validity of the IP address and port that are entered before making the join/host button interactable.
30+
* Decoupled SceneLoaderWrapper and ConnectionStates (#830). The OnServerStarted and OnClientStarted callbacks available in NGO 1.4.0 allows us to remove the need for an external method to initialize the SceneLoaderWrapper after starting a NetworkingSession.
3031

3132
## [2.0.4] - 2022-12-13
3233
### Changed

Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## [unreleased] - yyyy-mm-dd
44

5+
### Changed
6+
* Removed need for SceneLoaderWrapper.AddOnSceneEventCallback (#830). The OnServerStarted and OnClientStarted callbacks available in NGO 1.4.0 allows us to remove the need for an external method to initialize the SceneLoaderWrapper after starting a NetworkingSession.
7+
58
## [1.5.1] - 2022-12-13
69
### Changed
710
* Bumped RNSM to 1.1.0: Switched x axis units to seconds instead of frames now that it's available. This means adjusting the sample count to a lower value as well to 30 seconds, since the x axis was moving too slowly. (#788)

Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/SceneLoaderWrapper.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class SceneLoaderWrapper : NetworkBehaviour
2121

2222
bool IsNetworkSceneManagementEnabled => NetworkManager != null && NetworkManager.SceneManager != null && NetworkManager.NetworkConfig.EnableSceneManagement;
2323

24+
bool m_IsInitialized;
25+
2426
public static SceneLoaderWrapper Instance { get; protected set; }
2527

2628
public virtual void Awake()
@@ -39,32 +41,50 @@ public virtual void Awake()
3941
public virtual void Start()
4042
{
4143
SceneManager.sceneLoaded += OnSceneLoaded;
44+
NetworkManager.OnServerStarted += OnNetworkingSessionStarted;
45+
NetworkManager.OnClientStarted += OnNetworkingSessionStarted;
46+
NetworkManager.OnServerStopped += OnNetworkingSessionEnded;
47+
NetworkManager.OnClientStopped += OnNetworkingSessionEnded;
4248
}
4349

44-
public override void OnDestroy()
50+
void OnNetworkingSessionStarted()
4551
{
46-
SceneManager.sceneLoaded -= OnSceneLoaded;
47-
base.OnDestroy();
52+
// This prevents this to be called twice on a host, which receives both OnServerStarted and OnClientStarted callbacks
53+
if (!m_IsInitialized)
54+
{
55+
if (IsNetworkSceneManagementEnabled)
56+
{
57+
NetworkManager.SceneManager.OnSceneEvent += OnSceneEvent;
58+
}
59+
60+
m_IsInitialized = true;
61+
}
4862
}
4963

50-
public override void OnNetworkDespawn()
64+
void OnNetworkingSessionEnded(bool unused)
5165
{
52-
if (NetworkManager != null && NetworkManager.SceneManager != null)
66+
if (m_IsInitialized)
5367
{
54-
NetworkManager.SceneManager.OnSceneEvent -= OnSceneEvent;
68+
if (IsNetworkSceneManagementEnabled)
69+
{
70+
NetworkManager.SceneManager.OnSceneEvent -= OnSceneEvent;
71+
}
72+
73+
m_IsInitialized = false;
5574
}
5675
}
5776

58-
/// <summary>
59-
/// Initializes the callback on scene events. This needs to be called right after initializing NetworkManager
60-
/// (after StartHost, StartClient or StartServer)
61-
/// </summary>
62-
public virtual void AddOnSceneEventCallback()
77+
public override void OnDestroy()
6378
{
64-
if (IsNetworkSceneManagementEnabled)
79+
SceneManager.sceneLoaded -= OnSceneLoaded;
80+
if (NetworkManager != null)
6581
{
66-
NetworkManager.SceneManager.OnSceneEvent += OnSceneEvent;
82+
NetworkManager.OnServerStarted -= OnNetworkingSessionStarted;
83+
NetworkManager.OnClientStarted -= OnNetworkingSessionStarted;
84+
NetworkManager.OnServerStopped -= OnNetworkingSessionEnded;
85+
NetworkManager.OnClientStopped -= OnNetworkingSessionEnded;
6786
}
87+
base.OnDestroy();
6888
}
6989

7090
/// <summary>

0 commit comments

Comments
 (0)