exporting_for_dedicated_servers.rst 6.3 KB

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