OfflineMode and player.ID

Options
I'm currently working on implementing a single-player offline mode for my game. I'm having an issue very similar to http://forum.exitgames.com/viewtopic.php?f=17&t=3188. My problem is that my PhotonNetwork.player.ID changes from 1 to -1 after loading a scene.

I have two scenes - a Main Menu which handles connecting to Photon, region selection and room creation. Here, I use OnCreatedRoom() together with PhotonNetwork.LoadLevel to load into...

...my Gameplay scene, which has a NetworkManager object/script that handles all of the global Photon networking while in-game. The NetworkManager inherits Photon.MonoBehaviour.

Menu Scene
1. Player clicks button to launch offline campaign
2. If connected, PhotonNetwork.Disconnect() is called.
At this point, PhotonNetwork.player.ID = -1
3. Once safely disconnected (or if not connected), set PhotonNetwork.offlineMode=true
At this point, PhotonNetwork.player.ID = 1
4. Create a room
At this point, PhotonNetwork.player.ID = 1
5. In OnCreatedRoom(), use PhotonNetwork.LoadLevel to load Gameplay Scene
At this point, PhotonNetwork.player.ID = 1

Gameplay Scene
1. NetworkManager.Awake() is the very first entry point (first in script execution order)
At this point, PhotonNetwork.player.ID = -1
PhotonNetwork.offlineMode is still true

So, as soon as the new scene is loaded, the player ID is reset to -1. I haven't been able to track down where in the PUN code this is being reset. Obviously this then causes problems with ViewIDs, as they are allocated starting at -1 * Max Views = -1000 and throws errors when I use PhotonNetwork.Destroy().

Is there something I need to watch out for when loading a new scene with offlineMode=true? I'm going to keep digging to see if I can figure out what's up.

I'm using latest PUN version 1.26.1

Comments

  • Tobias
    Options
    I will have to try to reproduce and fix this. I should have some time to look into it tomorrow but I can't exactly promise something.
    It doesn't sound extremely hard to fix.
  • Tobias
    Options
    I don't really see where/how the player's ID should be reset to -1. A simple loading of a level shouldn't have that effect.
    When I enable offline mode in the Worker Demo, it will load a scene when creating a room and an Awake() method now logs the player.ID. It looks ok.
    I can't reproduce the issue, so I need a minimal project from you to check.
    Mail to developer@exitgames.com and refer to this topic pls.
  • GarethIW
    Options
    Thanks for checking this for me, Tobias. I'm assuming it's something in my game. I have sometime tonight so I'll keep digging and I'll put a test project together to attempt to reproduce.
  • GarethIW
    Options
    Okay, I've figured out where my issue lies - it's an order of execution problem:

    My code:
    1. I call PhotonNetwork.Disconnect()
    2. In OnDisconnectedFromPhoton():
    - Set PhotonNetwork.offlineMode
    - Create a room
    3. In OnCreatedRoom():
    - Load level

    My problem is due to this portion of code in NetworkingPeer.Disconnect (added annotations):
    public override void Disconnect()
        {
            ...
    
            this.State = global::PeerState.Disconnecting;
            base.Disconnect();
    
    <--- OnDisconnectedFromPhoton() is called at this point
    
            this.LeftRoomCleanup();   <----- LeftRoomCleanup() sets player.ID to -1
            this.LeftLobbyCleanup();
        }
    

    So, my code to set offline mode and create a room is in the OnDisconnectedFromPhoton() callback, but after that event is called, LeftRoomCleanup() is called which re-sets ID to -1.

    I'll find a workaround with an extra delay or something so that I'm not setting offline mode directly in OnDisconnectedFromPhoton. Just thought I'd post my findings in case this causes anyone else issues down the line (probably unlikely!)
  • Tobias
    Options
    Thanks for analyzing this case. I will look into handling it better.
    I was wondering if it's LeftRoomCleanup() but couldn't see how that's called offline.
    I think Join/Create will have to set the actorID to 1 when offline. That makes sense anyways.
  • Tobias
    Options
    It should be safe to remove the 2 calls at the end of NetworkingPeer.Disconnect().
    Both are called by the base.Disconnect() logic, so they are not needed. Actually, they could affect the state negatively, as seen here.

    I refactored calling LeftRoomCleanup and LeftLobbyCleanup for the next release. This should help in offline mode, too.