How to save the game when room is destroyed ?

Hello, I'm trying to create a persistent realtime game using PhotonServer, but I'm having trouble saving data when the room is destroyed:
I have a script that converts all important data to a binary file and saves it to a database.
When the room is created, it loads the binary backup file from the database and instantiates everything correctly.

My problem is that I don't know how to call my script to save my data when the last player leaves the room.
I tried using the "OnLeftRoom" and "OnApplicationQuit" callbacks, but the room no longer exists when they are called, so I cannot generate the backup file.

Currently, my solution is to save the game every 10 minutes while the room is open, but I would like to save my game every time the last player leaves the room.

How could I do that? Could I somehow write a server plugin to save my datas ?


  • Hi @Marolles,

    Thank you for choosing Photon!

    If you are using self-hosted Photon Server then the best option to have control over this is via a server plugin. You can get inspiration from WebHooks plugin as its source code is available in Photon Server Plugins SDK. Easiest way is to save room data when the room is closed (OnCloseGame) and load it when room is created (OnCreateGame).
    If you are using PUN or Unity SDK to connect to Photon Cloud then you could make use of WebHooks. See Persistence Guide.
  • I'm using self-hosted Photon Server, so I'll take a look at the server plugins
    Thank you for your help and the fast answer !
Sign In or Register to comment.