The Photon Forum
is Closed Permanently.

After many dedicated years of service, we have made the decision to retire our Forum and switch to read-only: we´ve saved the best to last! Your search result can be found below. Plus, we offer support via these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on Fusion.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to Disconnect player from room in a proper way.

Xenix
2023-04-14 16:08:36

Hey,

I have a room created with:

where mode is

GameMode.AutoHostOrClient

When the game is finished then the player can leave room. There are two cases:

  • Host is leaving the room, then room should be terminated.

  • Client is leaving the room, then room should be still alive.

In both cases disconnected player/players should be despawned on both host and corresponding client (so if I disconnect as a Client then on my side there should be no players, on Host side should be one player left, if I disconnect as a Host then both sides should have no players).

Closest I've got is:

and this

but it works in half in just one case - client requested to disconnect and client player is removed from server (but on client side there are still 2 players visible). When host disconnect everything is falling apart.

How to do this in a proper way?

Comments

WARdd
2023-04-15 10:32:31

This approach I think doesn't make sense, a client doesn't need to request the host if they're allowed to leave, unless there is some sort of early-leaving penalty you need to square with the server. But more likely the player should be able to quit at any time and leave the server to figure out the details, especially since players can just lose a connection for whatever reason.

The way I do it I have a function to despawn a player with some logic like

if (isSelf)

Runner.Shutdown();

So if the player wants to leave they despawn their own player and the Runner shutdown gets triggered.

Depending on how your project is structured you can just directly call Runner.Shutdown().

Then all the code to open the menu and so on is in my NetworkLauncher that has INetworkRunnerCallbacks. The code should be in

public void OnShutdown(NetworkRunner runner, ShutdownReason shutdownReason) {

..

}

That way you can correctly go back to the menu no matter for what reason the match is being shut down, which can be a lot of different reasons.

Runner.Disconnect I only use in the opposite situation; as a back up when trying to kick a player. Basically I use a custom RPC that tells a client to leave for a specific reason. If the client is still there after n seconds the server just forcefully disconnects them with Runner.Disconnect

Xenix
2023-04-17 07:10:05

Thanks, works great!

Back to top