How to cache ALL Instantiate events made by any player for all new players?

Hello! So I'm using "RoomOptions.CleanupCacheOnLeave = false;" to make sure all my instantiated objects stay behind when the instantiater leaves a room, but it seems the event cache for these instantiations are not kept along with the objects. Once the original instantiater has left, new players do not instantiate those objects, despite them still being around and visible for the players that are still in the room. I need all instantiated objects to show up for any new players that connect, no matter who instantiated them in the first place and even if they have left the room.

Am I missing something, or is this expected behavoir? If so, is there a way around it? I'm considering a workaround that would have non-master clients send an event to the current MasterClient so that they do the instantiating using InstantiateSceneObject, but this is rather messy for my use case so I'm hoping for a better solution!

Thanks!

Edit: Typos, text, added a question mark to the title! :)

Comments

  • Hi @Alex_P,

    I have checked this and can confirm, that I ended up with the same result. Unfortunately I can't tell you whether it's a bug or a wanted behaviour. However I prepared a workaround for you. Therefore navigate to the PhotonNetwork class and open it. You will find the following code at line 2460:

    SendInstantiateRaiseEventOptions.CachingOption = (sceneObject) ? EventCaching.AddToRoomCacheGlobal : EventCaching.AddToRoomCache;

    You can replace it with the following code snippet:
    if (sceneObject || !CurrentRoom.AutoCleanUp)
    {
        SendInstantiateRaiseEventOptions.CachingOption = EventCaching.AddToRoomCacheGlobal;
    }
    else
    {
        SendInstantiateRaiseEventOptions.CachingOption = EventCaching.AddToRoomCache;
    }
    Please note, that you would have to make these changes each time you update PUN.
  • Hey Christian! I had to update to the latest version to find that line of code, so I guess they're recently working on this stuff :) I found it at line 2455 and it does indeed seem to have resolved the issue, many thanks!

    Seeing as the line says "CleanupCacheOnLeave" this surely must be a bug! Do I need to submit a bug report, or is you already knowing about it enough? (Not meaning to be lazy, you just seem to know where you're going in the code so I thought you might be part of the project?)

    Cheers!
    Alex.
  • Hi @Christian_Simon,

    Just to mention, although your code change works well for online mode, I just realised I'm getting a null reference in offline mode. For now I fixed it just by using the original code whenever I'm running as offline...
    if (OfflineMode)
    {
    	SendInstantiateRaiseEventOptions.CachingOption = (sceneObject) ? EventCaching.AddToRoomCacheGlobal : EventCaching.AddToRoomCache;
    }
    else{
        if (sceneObject || !CurrentRoom.AutoCleanUp)
        {
            SendInstantiateRaiseEventOptions.CachingOption = EventCaching.AddToRoomCacheGlobal;
        }
        else
        {
            SendInstantiateRaiseEventOptions.CachingOption = EventCaching.AddToRoomCache;
        }
    }
    Not sure if that makes sense but it at least works :)

    Alex.
  • Realised it was because of the CurrentRoom reference so changed it to...
    if (CurrentRoom!=null && (sceneObject || !CurrentRoom.AutoCleanUp))
    {
        SendInstantiateRaiseEventOptions.CachingOption = EventCaching.AddToRoomCacheGlobal;
    }
    else
    {
        SendInstantiateRaiseEventOptions.CachingOption = EventCaching.AddToRoomCache;
    }