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 PUN.

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.

Photon user objects destroyed on disconnect

Afterlife1707
2021-05-30 07:41:26

I want to stop photon from destroying the user instantiated gameobjects when the user disconnects so that on reconnect they are still there.

Comments

Tobias
2021-05-31 15:59:20

This is tricky, as there will be two versions of the object while the player is not connected: The one on said player's client and the one in the room. While someone is away, the object is put under control of the Master Client, so it may move and you have to somehow rearrange which state of the object is the correct one, when someone rejoins.

Overall, the RoomOptions.CleanupCacheOnLeave setting should be the one you need to not destroy objects on leave.

Afterlife1707
2021-06-01 17:11:38

I did use the RoomOptions.CleanupCacheOnLeave. Still when the player disconnected, the gameobjects got destroyed. I'm able to reconnect using reconnectandjoin(). These are the room option settings:
byte maxPlayers = (byte)MaxPlayersPerRoom;
string roomName = "Room" + Random.Range(0, 10000);
RoomOptions roomOptions = new RoomOptions();
roomOptions.PlayerTtl = 60000;
roomOptions.EmptyRoomTtl = 60000;
roomOptions.MaxPlayers = maxPlayers;
roomOptions.PublishUserId = true;
roomOptions.IsOpen = true;
roomOptions.IsVisible = true;
roomOptions.CleanupCacheOnLeave = false;
PhotonNetwork.CreateRoom(roomName, roomOptions);

Tobias
2021-06-03 09:15:35

Which version of PUN 2 do you use?
Will try to repro and provide some help.

Tobias
2021-06-03 09:31:10

So, PhotonHandler.OnLeftRoom() will call LocalCleanupAnythingInstantiated unconditionally, which does the cleaning up.

The idea was to clean up objects in case you want to rejoin the room. Then, lingering objects would clash with their "originals" from within the room and you'd need to clarify which ones to keep, where everything is and who has control. When you rejoin the room, the objects get re-instantiated again and this would clash.

So, you can modify the PhotonHandler to not clean up. Then you'd have to clean up before you rejoin (if you do).

We could change the behavior for this but are unsure if we want to introduce this breaking change...

Let me know if this helps you find all necessary code to make your variant work.

Afterlife1707
2021-06-03 14:26:22

I'm using PUN 2.24 Photon lib: 4.1.4.7. I will look into what you said, thanks.

Tobias
2021-06-09 10:38:10

I would update to the latest PUN 2, by the way. Avoids hunting down bugs that may be fixed more recently.

Afterlife1707
2021-07-28 14:27:50

Hi @Tobias, coming back to this issue as I was busy with other parts of this project, is there a way to stop calling LocalCleanupAnythingInstantiated, as you mentioned it does the cleaning up part?

MrVentures
2022-05-21 16:39:49

Hi I was facing a similar issue and this forum post was instrumental in helping me get on the right track to finding a solution. Here is a video where I walkthrough how I ultimately solved this problem for anyone else interested: https://www.youtube.com/watch?v=f4FEs1agWVw

Back to top