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.
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.
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:
public class MyManager : NetworkManager{
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).
//can't remember if it was Server or Host, but think there's both
public override OnHostConnect(NetworkConnection conn){
if(!allowClients)
conn.Disconnect();
}
}