How do you manage your network state?

Options
Hello!
How do you manage your network state?
I'm currently developing a card game in unity with PhotonUnityNetworking 2 and PhotonRealtime
The project is gonna be failed due to my inability to make persistent multiplayer gameplay
I often encounter disconnects and desynchronizations of my game state

Consider next flow to get in game:
- Create a table (Room)
- Wait till other players join (4 player overall)
- Start game screen and share game events between players

First of all. How do you connect? Imagine that I'm in main menu. I'm pressing "Tables list" button and want
to see all opened tables at the moment. I need to be in lobby. Ok, I just call `PhotonNetwork.ConnectUsingSettings()`
but wait! What if I am already connected? Ok, check if `PhotonNetwork.IsConnectedAndReady` is true. I am connected and ready to see tables. What if I forgot to leave previous room and in some room now? I check if `PhotonNetwork.InRoom` is set to false, ok, it's already 2 checks I need to do to make sure I can list all current tables (why cached rooms was removed from the latest and I need to do some magic with Dictionary by myself?). Let's assume I see tables and entering one. I see a list of players
that pressed "I'm ready" button (Keeping that in room properties? I need to see count of "ready" players in lobby). Pressing "Ready" and send an event to master client to add me in properties. 4 players are ready - it's time to play. Game screen is shown and master makes its things to make it work. Suddenly I got disconnect because of connection fail - I restored my connection and want to get back into the game. I call `PhotonNetwork.ReconnectAndRejoin()` and this command often fails due to different reasons (Not connected - connect master server first, OpRoomJoin failed, etc..) and a lot of checks and callbacks I need to do again - game experience is failed.

I expect my controllers to be hard-binded to network context.
I made a wrapper that turns any photon callback/hook into a task with try/catch and timeout just in case (Makes something like `await PhotonManager.EnsureInLobby()`)
I made that wrapper twice for 4 month from scratch.

Is my way of thinking about network-programming wrong?

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @Chernikov,

    The project is gonna be failed due to my inability to make persistent multiplayer gameplay
    I often encounter disconnects and desynchronizations of my game state


    Sorry to hear this and I hope it won't come to that.
    Net code (unexpected disconnects, game state synchronization, etc.) can be challenging.

    Is my way of thinking about network-programming wrong?
    "Pessimist coding" or "defensive programming" is not a bad practice.
    I think you are mixing multiple issues into one and want to handle everything at once.
    Instead, define and fix each problem separately.

    How do you connect?
    [...]
    What if I am already connected?
    [...]
    What if I forgot to leave previous room and in some room now?

    I think you are not making use of the callbacks properly.
    No need to use the extra checks if you use the callbacks and put your code inside them.

    Did you finish our "PUN Basics Tutorial"?

    why cached rooms was removed from the latest and I need to do some magic with Dictionary by myself?
    This is a design decision. Ask yourself if you really need rooms listing or quick match (random matchmaking using filters)?

    Keeping that in room properties?
    Yes that's one way to do it.
  • Chernikov
    Options
    Sorry to hear this and I hope it won't come to that.
    Net code (unexpected disconnects, game state synchronization, etc.) can be challenging.

    If you try to connect/reconnect without internet with OfflineMode = false it will throw an error so I had to make a ping operation before using photon's connect function

    I think you are not making use of the callbacks properly.
    No need to use the extra checks if you use the callbacks and put your code inside them.

    Why no need. So, imagine you had a disconnect. What NetworkingClientState or PeerState is now? Sometimes it's `Disconnecting`, sometimes `Disconnected` and you have to check or even manually change PeerState variable is weird.

    This is a design decision. Ask yourself if you really need rooms listing or quick match (random matchmaking using filters)?

    I need to list available rooms to choose in what room I want to play
  • Chernikov
    Options
    Also InRoom can be true. but CurrentRoom is null
  • Chernikov
    Options
    Lobby cannot be simulated in offline mode, so I need to hack around it
  • Chernikov
    Options
    It would be better if we just made a game using http server
  • Tobias
    Options
    It would be better if we just made a game using http server.

    I'm sorry you see it like that. Maybe that makes more sense for your case.

    If you try to connect/reconnect without internet with OfflineMode = false it will throw an error so I had to make a ping operation before using photon's connect function

    The error is logged. The callback should be: OnDisconnected(ExceptionOnConnect).

    Lobby cannot be simulated in offline mode, so I need to hack around it.

    You're right. There is no callback for OnJoinedLobby(). We can add that, if it (still) helps.
    Of course, there is never going to be any room listed, as you are offline.

    Also InRoom can be true. but CurrentRoom is null.

    I would like to get a repro case for this to fix it. It's unclear how this happens.

    I need to list available rooms to choose in what room I want to play.

    Once there are hundreds of rooms, the list becomes only spam to a user.
    The server can find a room on the client's behalf with JoinRandomRoom and you don't have to load the list.

    Sometimes it's `Disconnecting`, sometimes `Disconnected`.

    A repro case would help mend the issue. Also if we knew which PUN version you are actually now working with.

    persistent multiplayer gameplay

    Do you mean persistent in the sense of long running and or async gameplay?