Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Online vs. Offline Data

Discussion in 'Netcode for GameObjects' started by moose0847, Jan 23, 2023.

  1. moose0847

    moose0847

    Joined:
    Dec 26, 2019
    Posts:
    33
    I'm currently working on a game that can be played online or offline, and I'd like to support simple drop-in, drop-out multiplayer that doesn't require a new game to be started. For example you can have a player join in the middle of the game, and then leave later on without requiring a restart. In the past, I've always done this by having an "offline" variable, and then a "networked" version of that variable that gets synced. For example I might have "_currentState", and "_netCurrentState", with a function that writes to whichever is needed.

    However, this is always an error prone approach, and I inevitably spend time tracking down bugs that result from essentially having 2 copies of the same data. To make this simple, I'd like to basically use NetworkVariable<T> for both offline and online, and just have these get initialized when switching to online mode. However, when I do this and play the game in offline mode, I'm getting a lot of warnings and errors that have to do with the network game not running.

    Has anyone found a good solution to this problem?
     
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @moose0847 , the approach I follow in this case is to make your "Host" mode your singleplayer mode.

    This means that your game instance would be both the server and the client when playing single player.

    Does this help?
     
  3. moose0847

    moose0847

    Joined:
    Dec 26, 2019
    Posts:
    33
    Yes this helps! I've just decided to make the game inherently networked, even if it's just a local game.
     
    RikuTheFuffs likes this.