exporting_for_dedicated_servers.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. .. _doc_exporting_for_dedicated_servers:
  2. Exporting for dedicated servers
  3. ===============================
  4. If you want to run a dedicated server for your project on a machine that doesn't
  5. have a GPU or display server available, you'll need to use a server build of Godot.
  6. Platform support
  7. ----------------
  8. - **Linux:** `Download an official Linux server binary <https://godotengine.org/download/server>`__.
  9. To compile a server binary from source, follow instructions in
  10. :ref:`doc_compiling_for_x11`.
  11. - **macOS:** :ref:`Compile a server binary from source for macOS <doc_compiling_for_osx>`.
  12. - **Windows:** There is no dedicated server build for Windows yet. As an alternative,
  13. you can use the ``--no-window`` command-line argument to prevent Godot from
  14. spawning a window. Note that even with the ``--no-window`` command-line argument,
  15. you'll need to have OpenGL support available on the Windows machine.
  16. If your project uses C#, you'll have to use a Mono-enabled server binary.
  17. "Headless" versus "server" binaries
  18. -----------------------------------
  19. The `server download page <https://godotengine.org/download/server>`__
  20. offers two kinds of binaries with several differences.
  21. - **Server:** Use this one for running dedicated servers. It does not contain
  22. editor functionality, and is therefore smaller and more
  23. optimized.
  24. - **Headless:** This binary contains editor functionality and is intended to be
  25. used for exporting projects. This binary *can* be used to run dedicated
  26. servers, but it's not recommended as it's larger and less optimized.
  27. Exporting a PCK file
  28. --------------------
  29. There are two ways to export a project for a server:
  30. - Create a Linux/X11 export preset, define a custom Release export template
  31. that points to the server binary then export the project as usual.
  32. - Export a PCK file only, preferably from a Linux/X11 export preset.
  33. Both methods should result in identical output. The text below describes the PCK
  34. file approach.
  35. Once you've downloaded a server binary, you should export a PCK file containing
  36. your project data. After creating the export preset, click **Export PCK/ZIP** at
  37. the bottom of the Export dialog then choose a destination path.
  38. The **Export With Debug** checkbox in the file dialog has no bearing on the
  39. final PCK file, so you can leave it as-is.
  40. See :ref:`doc_exporting_projects` for more information.
  41. .. note::
  42. If you're exporting the project from a headless editor, call the headless
  43. editor with the `--export-pack` option while in the project folder to export
  44. only a PCK file.
  45. .. note::
  46. The PCK file will include resources not normally needed by the server, such
  47. as textures and sounds. This means the PCK file will be larger than it could
  48. possibly be. Support for stripping unneeded resources from a PCK for server
  49. usage is planned in a future Godot release.
  50. On the bright side, this allows the same PCK file to be used both by a
  51. client and dedicated server build. This can be useful if you want to ship a
  52. single archive that can be used both as a client and dedicated server.
  53. Preparing the server distribution
  54. ---------------------------------
  55. After downloading or compiling a server binary, you should now place it in the
  56. same folder as the PCK file you've exported. The server binary should have the
  57. same name as the PCK (excluding the extension). This lets Godot detect and use
  58. the PCK file automatically. If you want to start a server with a PCK that has a
  59. different name, you can specify the path to the PCK file using the
  60. ``--main-pack`` command-line argument::
  61. ./godot-server --main-pack my_project.pck
  62. Starting the dedicated server
  63. -----------------------------
  64. If both your client and server are part of the same Godot project, you will have
  65. to add a way to start the server directly using a command-line argument. This
  66. can be done by adding the following code snippet in your main scene (or a
  67. singleton)'s ``_ready()`` method::
  68. if "--server" in OS.get_cmdline_args():
  69. # Run your server startup code here...
  70. # Using this check, you can start a dedicated server by running
  71. # a Godot binary (headless or not) with the `--server` command-line argument.
  72. pass
  73. Alternatively, you can make the dedicated server always start up if a headless
  74. or server binary is detected::
  75. # Note: Feature tags are case-sensitive! It's "Server", not "server".
  76. if OS.has_feature("Server"):
  77. # Run your server startup code here...
  78. # Note that using this check may break unit testing scripts when
  79. # running them with headless or server binaries.
  80. pass
  81. If your client and server are separate Godot projects, your server should most
  82. likely be configured in a way where running the main scene starts a server
  83. automatically.
  84. Next steps
  85. ----------
  86. On Linux, to make your dedicated server restart after a crash or system reboot,
  87. you can
  88. `create a systemd service <https://medium.com/@benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6>`__.
  89. This also lets you view server logs in a more convenient fashion, with automatic
  90. log rotation provided by systemd.
  91. If you have experience with containers, you could also look into wrapping your
  92. dedicated server in a `Docker <https://www.docker.com/>`__ container. This way,
  93. it can be used more easily in an automatic scaling setup (which is outside the
  94. scope of this tutorial).