Unity based dedicated server? No graphics, console/terminal instead?

My goal: to make a Unity based dedicated multiplayer server, with minimum bloat, such as turning off the renderer, and spawning a console window for text output.

My progress thus far: Running a unity application with the command line arguments “-batchmode -nographics” will effectively prevent Unity from opening a window (this works both with the editor, and a compiled Unity application). To spawn a console window, I am using unmanaged code on a “manager” GameObject as follows:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;

public class GameManager : MonoBehaviour {
    [DllImport("kernel32.dll")]
    private static extern bool AllocConsole();
    private static extern IntPtr GetStdHandle(UInt32 nStdHandle);
    private static extern void SetStdHandle(UInt32 nStdHandle, IntPtr handle);
    private const UInt32 StdOutputHandle = 0xFFFFFFF5;
    private TextWriter writer;

    // Use this for initialization
    void Start () {
        DontDestroyOnLoad(this);
        StartConsole();
        WriteLine("Console Ready");
    }
  
    // Update is called once per frame
    void Update () {
      
    }

    void StartConsole() {
        AllocConsole();
        // stdout's handle seems to always be equal to 7
        IntPtr defaultStdout = new IntPtr(7);
        IntPtr currentStdout = GetStdHandle(StdOutputHandle);
        if (currentStdout != defaultStdout)
            // reset stdout
            SetStdHandle(StdOutputHandle, defaultStdout);
        // reopen stdout
        writer = new StreamWriter(Console.OpenStandardOutput())
        { AutoFlush = true };
        Console.SetOut(writer);
    }

    void WriteLine(string line) {
        writer.WriteLine(line);
    }
}

When I run this scene in the editor, a console window is created, but then Unity crashes. Same with a standalone compiled application. It crashes before displaying the text “Console Ready”.

  1. Does anyone have any ideas or tips on what I may be doing wrong with this?

  2. Is there perhaps a different (possibly better) approach? Aside from networking, I still need some form of scene management and physics for the server to properly do game logic. It would be great if I could use just the parts of Unity that are essential to running a server, for maximum efficiency.

Ehhhh… just select “headless” in the Linux build options and it doesn’t include any of the graphics stuff in your server. In mine I wrote a separate logging system that outputs independent of the player.log, it is for server status information, etc. If you want to watch it in real time, that is what “tail -f” is for.

If you’re trying to make your server a Windows or Mac application, you’re already giving up on your “minimum bloat” requirement.

2 Likes

I will give that a shot. I have Linux Mint on Oracle Virtualbox. Still, a Windows solution is good for maximizing user friendliness for the user base, making the product more readily available?

What? No. The server app is for running on a server. I don’t have any actual facts to back this up, but I’d bet a pizza there are far more Linux servers than Windows servers on the internet, as well as more people comfortable using the former than the latter.

That depends on what your user base is. Back when we were playing Minecraft in the office, it was very convenient to be able to boot a server on our normal workstation, and then connect over LAN. If your game’s server is something a gang of friends can boot and join over IP/LAN, it’s a lot more convenient if it can run on any OS.

If it’s a dedicated server setup (like having your own TF2 server with a custom ruleset), you can assume people will have a dedicated box/cloud server to run that on, and supporting non-linux setups becomes a lot less important.

1 Like

Fully agree, but in this case, I would also suggest your server have a UI (and quite likely, be the very same app as your main game — with simply a “host a game” mode).