question

yasha avatar image
yasha asked

OnServerAddPlayer(NetworkConnection conn) Never Called in Network Manager - Unity

Ok I'm new to Playfab and am really struggling to get two players spawned into a room in Unity. I have a matchmaking system with tickets working, and everything up to GetMatch works GREAT. Thing is, I can't find any documentation online or instructions on what to do after GetMatch to get the players spawned in a room.

I have a custom Network Manager where I want to add player prefabs to a scene, and have those prefabs be moveable and have that movement represented on all clients - so I know the two are in the same room and connected. Thing is, I have no idea what to do. I'm assuming OnServerAddPlayer(NetworkConnection conn) is the function we override and is where we spawn the players locally & on server, but I've had no luck with calling it.
I'm trying to run the unity debugger on the client side to stop when OnServerAddPlayer is run, but it's never triggered. Maybe it only runs on the server-side and we never see it? I have no way of knowing if it's actually being triggered. Here's my code for that function:

 public override void OnServerAddPlayer(NetworkConnection conn)
    {
        // add player at correct spawn position
        Transform start = numPlayers == 0 ? pos1 : pos2;
        GameObject player = Instantiate(playerPrefab, start.position, start.rotation);
        player.name = $"Player({numPlayers})";
        NetworkServer.AddPlayerForConnection(conn, player);
        spawner.CmdSpawnPlayer();
    }


My intention is to spawn the player, and tell the server to spawn the player prefab. I haven't had any luck, so I assumed that I needed to maybe get the player to tell the server to spawn the object (hence CmdSpawnPlayer).

The player prefabs have network identities and NetworkTransforms on them and do not have the server checkbox ticked.

I've also noticed that ClientScene is adding players, and looking at the comments above it, it says that NetworkManager.OnServerAddPlayer should be the next thing called. Again - idk if it's actually calling it, because it's not breakpointing on my client, and I'm not seeing anything from the server.

I'm kinda just trying a shotgun approach and hoping something makes the player prefab appear because I have a real hard time reading documentation and nobody I can find has footage on what to do with the GetMatch data to spawn players. I'm so stuck because nobody else is doing exactly what I'm doing - DapperDino has a Youtube video on everything up to this step following GetMatch, so AFAIK nobody has a good explanation as to the exact steps we do after Matchmaking is complete and the match is got.

Can somebody help me out?

Matchmaking
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image
Seth Du answered

Based on your description, I don’t see which Multiplayer Server(MPS) solution you are using. If you have implemented PlayFab Multiplayer Server 2.0, with auto-allocation enabled, after GetMatch is called, the IP and port address will be returned in the callback result, which is used for player to establish the connection.

The basic idea of a multiplayer server is to collect information (like actions of a player has done), and distribute/sync it with all connected players via socket connection. I think you may need to let Unity network manager (Mirror) to sync with server side states and get players data to respawn the players in the scene.

However, I am not expert on it as PlayFab only provides the infrastructure of MPS, you may navigate to Unity Mirror forum to find dedicated support. In addition, I highly recommend checking the open source Mirror samples on vis2k/Mirror: #1 Open Source Unity Networking Library (github.com)

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

yasha avatar image yasha commented ·

Ok - so how does the player "establish the connection" after they have the port and IP? Because I can fetch those. I just don't know what the player needs to do to connect. I think it's already working, but ClientScene.AddPlayer doesn't trigger the server to spawn prefabs as I'd wanted.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ yasha commented ·

I am not sure of server-side logic. Usually the client should initialize a socket connection, please refer to: Connecting clients to game servers - PlayFab | Microsoft Docs

0 Likes 0 ·
ree13su avatar image
ree13su answered

you might be overriding the onServerAddPlayer behavior with a custom handler like

NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnect);

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Berk Unal avatar image
Berk Unal answered

Hey @Yasha, did you find the solution for spawning players after getmatch? I have the exact problem and I've been looking for a solution over a week now.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Joe Swindell avatar image
Joe Swindell answered

OnServerAddPlayer is actually called from CLIENT side OnClientConnect when you call NetworkClient.AddPlayer(); inside of OnClientConnect()

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.