Join A Random Session

Options

Hi, I'm reading the doc about Fusion matchmaking API and I would like to do a matchmaking with "Join A Random Session" with "AutoHostOrClient", so I used this script:


public async Task StartPlayer(NetworkRunner runner)

  {

    if (!runner)

      runner = gameObject.AddComponent<NetworkRunner>();

    runner.ProvideInput = true;


    var result = await runner.StartGame(new StartGameArgs()

    {

      GameMode = GameMode.AutoHostOrClient,

      PlayerCount = 2,

      SessionName = "TestRoom",

      Scene = SceneManager.GetSceneByName("Scene2").buildIndex,

      SceneManager = gameObject.AddComponent<NetworkSceneManagerDefault>()

    });


    if (result.Ok)

    {

      SceneManager.LoadScene("Scene2");

    }

    else

    {

      Debug.LogError($"Failed to Start: {result.ShutdownReason}");

    }

  }


but after the scene is loaded I don't understand how to establish the connection because the Network runner is not maintained and OnPlayerJoined() is not called.

Should I create a new runner and a new StartGame()? 

Or did I do something wrong in StartPlayer()?


Just one more question, if I wanted to do another random search with "AutoHostOrClient" but with another search, for another different type of game mode, how would I go about it? Because in the doc I only read how to do host or client separately.

Thank you.

Comments

  • Isaac_Augusto
    edited November 2022
    Options

    Hi,

    You need to load the scene with Runner.SetActiveScene(). If you load the scene with the basic unity loading, your NetworkObjects will not be registered and thus, not valid.

     if I wanted to do another random search with "AutoHostOrClient" but with another search, for another different type of game mode, how would I go about it?

    Not sure i followed. If you're talking about random join or "host" in shared mode, just setting the GameMode as shared would already do that.

    I would suggest you to take a look at our samples to understand better the Fusion workflow.

    This one is a good start: https://doc.photonengine.com/en-us/fusion/current/game-samples/fusion-asteroids

    -----

    Isaac Augusto

    Photon Fusion Team

  • Lele
    Options

    Thanks for the answer, anyway sorry if I explained badly, but I solved it anyway.

    One last thing, if you can help me, as the scene change, the players were spawning in the wrong scene, as i used "OnPlayerJoined" so i created a new Spawned() in the correct scene, but this causes me several problems


    public override void Spawned()

        {

          if (Runner.LocalPlayer.PlayerId == 1)

            Runner.Spawn(playerPrefab1, position1, rotation1, Runner.LocalPlayer);


          else if (Runner.LocalPlayer.PlayerId == 0)

            Runner.Spawn(playerPrefab2, position2, rotation2, Runner.LocalPlayer);

        }


    As you can see, i have to spawn 2 different prefabs, in different positions, so i used the id to differentiate them. But unlike "OnPlayerJoined" only the first player is spawned, while the second one is not. Also, when the player is spawned the "interpolated" property of the rigidbody compontent becomes "none", and it receives no input signal.

    Sorry for all these questions and thanks again.