Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Does Netcode for GameObjects have 'offline mode'?

Discussion in 'Netcode for GameObjects' started by Lurking-Ninja, Nov 30, 2021.

  1. Playing around with NfG trying to calculate if it works for me or not to develop a small tactical RPG with 1-4 players per playthrough. Obviously with Relay and Lobby and all.
    Everything looks promising, except for one thing. I couldn't find anything which would resemble Photon's 'offline mode', where basically they feed back all the RPCs and everything into the same game, essentially render the game to single player and all connected players are the same local player.
    This means we don't need to architect our game code around these two modes, everything can go through the usual RPC calls and all normal synchronization, the final game looks like a connected game, but in reality it's a single player mode, where the local player can play alone or even steer all the "connected" characters.

    Am I just missing something painstakingly obvious or is it not a feature? If it is not a feature, is it possible to have it as a feature? This essentially cut the complexity of all connected code in half since we don't need to check for game mode constantly and making branches for them.
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    You can just start the NetworkManager in host mode and use an IP based transport like UnityTransport. That way you are hosting a single player session on your own where no one can join.
     
    Volshar likes this.
  3. BigzCramillet

    BigzCramillet

    Joined:
    Mar 31, 2020
    Posts:
    9
    Hello,
    Regarding this solution, is it possible to set the Unity Transport Protocol Type from Relay Unity Transport to Unity Transport or is there a way to set the Network Transport at runtime and have two different transports configured on a NetworkManager object?

    Thanks for your help!
     
    TomiBary likes this.
  4. BigzCramillet

    BigzCramillet

    Joined:
    Mar 31, 2020
    Posts:
    9
    Should I take that as a no ? :D
     
  5. GilbertoBitt

    GilbertoBitt

    Joined:
    May 27, 2013
    Posts:
    111
    well why not try it? this is somethings I'm curious about too.
     
  6. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Yes this is possible. I suggest having a look at our Boss Room sample. Inside that project we are using multiple transports and we switch them at runtime depending on which connection method the player chooses in the UI.
     
    NelsonChristensen likes this.
  7. DrViJ

    DrViJ

    Joined:
    Feb 9, 2013
    Posts:
    158
    But what about webgl? I would like to implement a single player mode on WebGL? Host mode is not available there, could you please give an advice how handle this?
     
  8. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    437
    I think you've got your answer here
     
    DrViJ likes this.
  9. Maxim

    Maxim

    Joined:
    Aug 17, 2009
    Posts:
    37
    Hello,
    I have a co-op game with Steam Facepunch network transport (Netcode for GameObjects). Where one player is a host.
    How should I make offline mode?
    Is it possible to switch NetworkManager transport from Facepunch to UnityTransport(IP) and vise versa in play mode?
    Or should I use 2 Network managers and switch between them? It has NetworkManager.Singleton. Only one NetworkManager should be enabled at a time.
    Maybe use some TransportSwitcher set to NetworkManager' NetworkTransport (will it even work)?
    public class TransportSwitcher : NetworkTransport
    {
    [SerializeField] private NetworkTransport[] _networkTransports;
    private NetworkTransport _currentTransport;
    private void Awake()
    {
    SetTransport(0);
    }
    public void SetTransport(int v)
    {
    _currentTransport = _networkTransports[v];
    }
    ...
    NetworkTransport override methods using _currentTransport
    ...
    }

    And switch it before StartHost().
     
    Last edited: Oct 11, 2023
  10. DoubleIsLoveDoubleIsLife

    DoubleIsLoveDoubleIsLife

    Joined:
    Nov 29, 2014
    Posts:
    32
    In my projects I use prefabs to switch the transport. When the transport is switched (for example, from Steam to single-player), I destroy the old transport GO, instantiate the relevant prefab and just assign the Transport component to NetworkManager.Singleton.NetworkConfig.NetworkTransport.

    Seems to have worked without any issues so far.

    Example:
    Code (CSharp):
    1.         private void StartTransport(bool? useSteamOverride = null)
    2.         {
    3.             if (useSteamOverride ?? UseSteam) {
    4.                 StartFacepunchTransport();
    5.             } else {
    6.                 StartUnityTransport();
    7.             }
    8.  
    9.             void StartFacepunchTransport()
    10.             {
    11.                 if(TransportType == Transports.Facepunch)
    12.                     return;
    13.                 if(currentTransport != null) {
    14.                     Destroy(currentTransport);
    15.                 }
    16.  
    17.                 GameObject transportGO = Instantiate(facepunchTransportPrefab, NetworkManager.Singleton.transform);
    18.                 FacepunchTransport transport = transportGO.GetComponent<FacepunchTransport>();
    19.                 NetworkManager.Singleton.NetworkConfig.NetworkTransport = transport;
    20.                 currentTransport = transportGO;
    21.                 TransportType = Transports.Facepunch;
    22.             }
    23.  
    24.             void StartUnityTransport()
    25.             {
    26.                 if(TransportType == Transports.Unity)
    27.                     return;
    28.                 if(currentTransport != null) {
    29.                     Destroy(currentTransport);
    30.                 }
    31.  
    32.                 GameObject transportGO = Instantiate(unityTransportPrefab, NetworkManager.Singleton.transform);
    33.                 UnityTransport transport = transportGO.GetComponent<UnityTransport>();
    34.                 NetworkManager.Singleton.NetworkConfig.NetworkTransport = transport;
    35.                 currentTransport = transportGO;
    36.                 TransportType = Transports.Unity;
    37.             }
    38.         }