exporting_for_dedicated_servers.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. .. _doc_exporting_for_dedicated_servers:
  2. Exporting for dedicated servers
  3. ===============================
  4. .. highlight:: none
  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 run Godot with the ``headless``
  7. display server and ``Dummy`` :ref:`audio driver <class_ProjectSettings_property_audio/driver/driver>`.
  8. Since Godot 4.0, this can be done by running a Godot binary on any platform with
  9. the ``--headless`` command line argument, or running a project exported as
  10. dedicated server. You do not need to use a specialized server binary anymore,
  11. unlike Godot 3.x.
  12. Editor versus export template
  13. -----------------------------
  14. It is possible to use either an editor or export template (debug or release)
  15. binary in headless mode. Which one you should use depends on your use case:
  16. - **Export template:** Use this one for running dedicated servers. It does not
  17. contain editor functionality, and is therefore smaller and more optimized.
  18. - **Editor:** This binary contains editor functionality and is intended to be
  19. used for exporting projects. This binary *can* be used to run dedicated
  20. servers, but it's not recommended as it's larger and less optimized.
  21. Export approaches
  22. -----------------
  23. There are two ways to export a project for a server:
  24. - Create a separate export preset for the platform that will host the server, then
  25. export your project as usual.
  26. - Export a PCK file only, preferably for the platform that matches the platform
  27. that will host the server. Place this PCK file in the same folder as an export
  28. template binary, rename the binary to have the same name as the PCK (minus the
  29. file extension), then run the binary.
  30. Both methods should result in identical output. The rest of the page will focus
  31. on the first approach.
  32. See :ref:`doc_exporting_projects` for more information.
  33. .. _doc_exporting_for_dedicated_servers_exporting_project:
  34. Exporting a project for a dedicated server
  35. ------------------------------------------
  36. If you export a project as usual when targeting a server, you will notice that
  37. the PCK file is just as large as for the client. This is because it includes all
  38. resources, including those the server doesn't need (such as texture data).
  39. Additionally, headless mode won't be automatically used; the user will have to
  40. specify ``--headless`` to make sure no window spawns.
  41. Many resources such as textures can be stripped from the PCK file to greatly
  42. reduce its size. Godot offers a way to do this for textures and materials in a way
  43. that preserves references in scene or resource files (built-in or external).
  44. To begin doing so, make sure you have a dedicated export preset for your server,
  45. then select it, go to its **Resources** tab and change its export mode:
  46. .. figure:: img/exporting_for_dedicated_servers_export_mode.webp
  47. :align: center
  48. :alt: Choosing the **Export as dedicated server** export mode in the export preset
  49. Choosing the **Export as dedicated server** export mode in the export preset
  50. When this export mode is chosen, the ``dedicated_server`` feature tag is
  51. automatically added to the exported project.
  52. .. note::
  53. If you do not wish to use this
  54. export mode but still want the feature tag, you can write the name
  55. ``dedicated_server`` in the **Features** tab of the export preset.
  56. This will also force ``--headless`` when running the exported project.
  57. After selecting this export mode, you will be presented with a list of resources
  58. in the project:
  59. .. figure:: img/exporting_for_dedicated_servers_export_resources.webp
  60. :align: center
  61. :alt: Choosing resources to keep, keep with stripped visuals or remove
  62. Choosing resources to keep, keep with stripped visuals or remove
  63. Ticking a box allows you to override options for the specified file or folder.
  64. Checking boxes does **not** affect which files are exported; this is done by the
  65. options selected for each checkbox instead.
  66. Files within a checked folder will automatically use the parent's option by
  67. default, which is indicated by the **(Inherited)** suffix for the option name
  68. (and the option name being grayed out). To change the option for a file whose
  69. option is currently inherited, you must tick the box next to it first.
  70. - **Strip Visuals:** Export this resource, with visual files (textures and materials)
  71. replaced by placeholder classes. Placeholder classes store the image size
  72. (as it's sometimes used to position elements in a 2D scene), but nothing else.
  73. - **Keep:** Export this resource as usual, with visual files intact.
  74. - **Remove:** The file is not included in the PCK. This is useful to ignore
  75. scenes and resources that only the client needs. If you do so, make sure the
  76. server doesn't reference these client-only scenes and resources in any way.
  77. The general recommendation is to use **Strip Visuals** whenever possible, unless
  78. the server needs to access image data such as pixels' colors. For example, if
  79. your server generates collision data based on an image's contents, you need to
  80. use **Keep** for that particular image.
  81. .. tip::
  82. To check the file structure of your exported PCK, use the **Export
  83. PCK/ZIP...** button with a ``.zip`` file extension, then open the resulting
  84. ZIP file in a file manager.
  85. .. warning::
  86. Be careful when using the **Remove** mode, as scenes/resources that reference
  87. a removed file will no longer be able to load successfully.
  88. If you wish to remove specific resources but make the scenes still be able
  89. to load without them, you'll have to remove the reference in the scene file
  90. and load the files to the nodes' properties using ``load()`` in a script.
  91. This approach can be used to strip resources that Godot doesn't support
  92. replacing with placeholders yet, such as audio.
  93. Removing textures is often what makes the greatest impact on the PCK size,
  94. so it is recommended to stick with **Strip Visuals** at first.
  95. With the above options used, a PCK for the client (which exports all resources
  96. normally) will look as follows:
  97. ::
  98. .
  99. ├── .godot
  100. │ ├── exported
  101. │ │ └── 133200997
  102. │ │ └── export-78c237d4bfdb4e1d02e0b5f38ddfd8bd-scene.scn
  103. │ ├── global_script_class_cache.cfg
  104. │ ├── imported
  105. │ │ ├── map_data.png-ce840618f399a990343bfc7298195a13.ctex
  106. │ │ ├── music.ogg-fa883da45ae49695a3d022f64e60aee2.oggvorbisstr
  107. │ │ └── sprite.png-7958af25f91bb9dbae43f35388f8e840.ctex
  108. │ └── uid_cache.bin
  109. ├── client
  110. │ ├── music.ogg.import
  111. │ └── sprite.png.import
  112. ├── server
  113. │ └── map_data.png.import
  114. ├── test
  115. │ └── scene.gd
  116. └── unused
  117. │ └── development_test.gd
  118. ├── project.binary
  119. ├── scene.gd
  120. ├── scene.tscn.remap
  121. The PCK's file structure for the server will look as follows:
  122. ::
  123. .
  124. ├── .godot
  125. │ ├── exported
  126. │ │ └── 3400186661
  127. │ │ ├── export-78c237d4bfdb4e1d02e0b5f38ddfd8bd-scene.scn
  128. │ │ ├── export-7958af25f91bb9dbae43f35388f8e840-sprite.res # Placeholder texture
  129. │ │ └── export-fa883da45ae49695a3d022f64e60aee2-music.res
  130. │ ├── global_script_class_cache.cfg
  131. │ ├── imported
  132. │ │ └── map_data.png-ce840618f399a990343bfc7298195a13.ctex
  133. │ └── uid_cache.bin
  134. ├── client
  135. │ ├── music.ogg.import
  136. │ └── sprite.png.import # Points to placeholder texture
  137. └── server
  138. │ └── map_data.png.import
  139. ├── project.binary
  140. ├── scene.gd
  141. ├── scene.tscn.remap
  142. Starting the dedicated server
  143. -----------------------------
  144. If both your client and server are part of the same Godot project, you will have
  145. to add a way to start the server directly using a command-line argument.
  146. If you :ref:`exported the project <doc_exporting_for_dedicated_servers_exporting_project>`
  147. using the **Export as dedicated server** export mode (or have added
  148. ``dedicated_server`` as a custom feature tag), you can use the ``dedicated_server``
  149. feature tag to detect whether a dedicated server PCK is being used:
  150. .. tabs::
  151. .. code-tab:: gdscript
  152. # Note: Feature tags are case-sensitive.
  153. if OS.has_feature("dedicated_server"):
  154. # Run your server startup code here...
  155. pass
  156. .. code-tab:: csharp
  157. // Note: Feature tags are case-sensitive.
  158. if (OS.HasFeature("dedicated_server"))
  159. {
  160. // Run your server startup code here...
  161. }
  162. If you also wish to host a server when using the built-in ``--headless`` command
  163. line argument, this can be done by adding the following code snippet in your
  164. main scene (or an autoload)'s ``_ready()`` method:
  165. .. tabs::
  166. .. code-tab:: gdscript
  167. if DisplayServer.get_name() == "headless":
  168. # Run your server startup code here...
  169. #
  170. # Using this check, you can start a dedicated server by running
  171. # a Godot binary (editor or export template) with the `--headless`
  172. # command-line argument.
  173. pass
  174. .. code-tab:: csharp
  175. using System.Linq;
  176. if (DisplayServer.GetName() == "headless")
  177. {
  178. // Run your server startup code here...
  179. //
  180. // Using this check, you can start a dedicated server by running
  181. // a Godot binary (editor or export template) with the `--headless`
  182. // command-line argument.
  183. }
  184. If you wish to use a custom command line argument, this can be done by adding
  185. the following code snippet in your main scene (or an autoload)'s ``_ready()``
  186. method:
  187. .. tabs::
  188. .. code-tab:: gdscript
  189. if "--server" in OS.get_cmdline_user_args():
  190. # Run your server startup code here...
  191. #
  192. # Using this check, you can start a dedicated server by running
  193. # a Godot binary (editor or export template) with the `--server`
  194. # command-line argument.
  195. pass
  196. .. code-tab:: csharp
  197. using System.Linq;
  198. if (OS.GetCmdlineUserArgs().Contains("--server"))
  199. {
  200. // Run your server startup code here...
  201. //
  202. // Using this check, you can start a dedicated server by running
  203. // a Godot binary (editor or export template) with the `--server`
  204. // command-line argument.
  205. }
  206. It's a good idea to add at least one of the above command-line arguments to
  207. start a server, as it can be used to test server functionality from the command
  208. line without having to export the project.
  209. If your client and server are separate Godot projects, your server should most
  210. likely be configured in a way where running the main scene starts a server
  211. automatically.
  212. Next steps
  213. ----------
  214. On Linux, to make your dedicated server restart after a crash or system reboot,
  215. you can
  216. `create a systemd service <https://medium.com/@benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6>`__.
  217. This also lets you view server logs in a more convenient fashion, with automatic
  218. log rotation provided by systemd. When making your project hostable as a systemd service,
  219. you should also enable the ``application/run/flush_stdout_on_print``
  220. project setting. This way, journald (the systemd logging service) can collect
  221. logs while the process is running.
  222. If you have experience with containers, you could also look into wrapping your
  223. dedicated server in a `Docker <https://www.docker.com/>`__ container. This way,
  224. it can be used more easily in an automatic scaling setup (which is outside the
  225. scope of this tutorial).