Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiplayerService.CreateOrJoinSessionAsync sometimes returns before SceneManager is ready #3146

Open
afarchy opened this issue Dec 2, 2024 · 3 comments
Labels
docs Investigating Issue is currently being investigated type:support Questions or other support

Comments

@afarchy
Copy link

afarchy commented Dec 2, 2024

Description

I'm hitting an error here that I don't understand in joining a distributed authority session, where after the session joins, the SceneManager is null.

  • This doesn't always happen.
  • Never seen this in editor, only on device. We're using Meta Quest 2/3.
var options = new SessionOptions()
{
    Name = sessionName,
    MaxPlayers = 64,
}.WithDistributedAuthorityNetwork();

_currentSession = await MultiplayerService.Instance.CreateOrJoinSessionAsync(sessionName, options);

if (NetworkManager.Singleton.SceneManager == null)
{
    Debug.LogError("Why is this happening?");
}

Reproduce Steps

  1. See code above

Actual Outcome

NetworkManager.Singleton.SceneManager is null.

Expected Outcome

NetworkManager.Singleton.SceneManager is not null.

Environment

  • OS: Meta Quest 2/3 (Android)
  • Unity Version: 6000.0.23f1
  • Netcode Version: 2.1.1
  • Netcode Commit: [e.g. https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/commit/d218b2992ddb98f2eb1a06b9085724ca33352865]
@afarchy afarchy added stat:awaiting triage Status - Awaiting triage from the Netcode team. type:bug Bug Report labels Dec 2, 2024
@NoelStephensUnity
Copy link
Collaborator

@afarchy
I would recommend subscribing to the NetworkManager.OnClientStarted action and accessing the NetworkManager.SceneManager at that point in time. When you have joined a session it doesn't mean that the NetworkManager has been started just yet as there could be an asynchronous task pending before that happens (which can sometimes complete at the same time or like the very next frame or so).

You might find this ExtendedNetworkManager from the v2.0.0 examples folder useful.

Typically, I recommend two paths to creating session connection related scripts:

  • Create MonoBehaviour component that is placed on the same GameObject as the NetworkManager and just get the NetworkManager component within the MonoBehaviour component's awake method and assign it to a local property. The down-side (sort of) is that it requires that you access the NetworkManager properties and methods via the local property. This is was the approach used in the distributed authority quick start example.
  • Derive from NetworkManager and add your connection code to that derived class. The up-side is that you are not accessing or assigning any property (i.e. like the ExtendedNetworkManager example above).

However, the key to making sure the NetworkManager is fully initialized, for both of these approaches, is to wait for the NetworkManager.OnClientStarted to be invoked.

Let me know if this helps resolve your issue?

@NoelStephensUnity NoelStephensUnity added type:support Questions or other support stat:awaiting response Status - Awaiting response from author. and removed type:bug Bug Report labels Dec 9, 2024
@afarchy
Copy link
Author

afarchy commented Dec 16, 2024

Thanks @NoelStephensUnity, so far OnClientStarted is working. Is there a reason MultiplayerService.Instance.CreateOrJoinSessionAsync doesn't wait on it before returning the session? Either way, some docs/example on when it's appropriate to access SceneManager might be good. The docs just mention waiting until NetworkManager is 'started'. https://docs-multiplayer.unity3d.com/netcode/current/basics/scenemanagement/using-networkscenemanager/

@NoelStephensUnity NoelStephensUnity added docs Investigating Issue is currently being investigated and removed stat:awaiting response Status - Awaiting response from author. stat:awaiting triage Status - Awaiting triage from the Netcode team. labels Dec 16, 2024
@NoelStephensUnity
Copy link
Collaborator

NoelStephensUnity commented Dec 16, 2024

@afarchy
We are looking at a few possibilities including adding some additional checks within the Multiplayer SDK. However, until that is determined I will see what we can do to provide some pointers within the documentation.
👍

As a side note, when the CreateOrJoinSessionAsync task is completed it doesn't always mean that NetworkManager has finished starting. It means the service session has been created, the account has been connected, and there could be some edge cases (as you have discovered) where the async task could report having finished before NetworkManager has been started (order of operations/call-stack).

This some information provided in the Distributed Authority Quick Start under the Connection Setup section at the end of the example script...but it only talks about spawning objects and not the NetworkSceneManager.

I will make a note to add additional information on that page and to include the same kind of information on the page your linked above.

However, I think it would make sense to not have the async task report that it is finished until the NetworkManager has been started =and= the client has been approved (at the NGO level) for the connection (i.e. OnClientConnected).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Investigating Issue is currently being investigated type:support Questions or other support
Projects
None yet
Development

No branches or pull requests

2 participants