Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Recommended strategy for game that is playable both online and offline?

Discussion in 'Multiplayer' started by Jay-Pavlina, Jul 20, 2015.

  1. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    Is it safe to host a game as localhost for offline play? That would be the easiest solution because then I could use the same code offline and online. Would this work on all computers, mobile devices, and consoles?

    I wasn't sure if it is safe to use localhost in a released game, but I don't know that much about networking. If localhost is the recommended solution, which port should I use?

    I know I could just disable networking, but that makes the code more complicated because I can't use things like SyncLists that I'll be using while online. I was just wondering what the general recommendation is for this.
     
  2. 05cordie

    05cordie

    Joined:
    Sep 30, 2015
    Posts:
    8
    Hi Jay !
    Did you find any solution ? I'm asking exactly the same question... Trying to make an offline option in my online game...
    Thank you in advance
     
  3. Torigas

    Torigas

    Joined:
    Jan 1, 2014
    Posts:
    63
    Well you can start a localhost game and just keep everything as is. Maybe there's a flag to disable the functionality allowing people to join your game. Not a pretty solution but it might work.

    I wonder if there's an easier way besides making the networking part of your code so modular that you can just disable it, a feat for senior software architects, I assume.
     
  4. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Depends on how you define your word safe, whoever is running the host can get advantage by modifying the game (aka: cheat).
    go with high number ports, ports with low numbers require root/admin/special privileges to use, so as safe thing you can start from 5000.
    It's actually very easy to implement, here's a small implementation written on the fly so there might be some errors, but gives you the general idea on how it would work:
    Code (CSharp):
    1. public class MyManager : NetworkManager{
    2.   bool allowClients; //set this to true if you want to allow clients (online) and false if don't want clients (offline, or max number already connected).
    3.   //can't remember if it was Server or Host, but think there's both
    4.   public override OnHostConnect(NetworkConnection conn){
    5.     if(!allowClients)
    6.         conn.Disconnect();
    7.   }
    8. }
     
    Jay-Pavlina likes this.
  5. eelbaz

    eelbaz

    Joined:
    Feb 21, 2015
    Posts:
    19
    Would be really interested to hear other people's opinions on this