|
1 | | -:article_outdated: True |
2 | | - |
3 | 1 | .. _doc_exporting_for_dedicated_servers: |
4 | 2 |
|
5 | 3 | Exporting for dedicated servers |
6 | 4 | =============================== |
7 | 5 |
|
8 | 6 | If you want to run a dedicated server for your project on a machine that doesn't |
9 | | -have a GPU or display server available, you'll need to use a server build of Godot. |
10 | | - |
11 | | -Platform support |
12 | | ----------------- |
13 | | - |
14 | | -- **Linux:** `Download an official Linux server binary <https://godotengine.org/download/server>`__. |
15 | | - To compile a server binary from source, follow instructions in |
16 | | - :ref:`doc_compiling_for_linuxbsd`. |
17 | | -- **macOS:** :ref:`Compile a server binary from source for macOS <doc_compiling_for_macos>`. |
18 | | -- **Windows:** There is no dedicated server build for Windows yet. As an alternative, |
19 | | - you can use the ``--no-window`` command-line argument to prevent Godot from |
20 | | - spawning a window. Note that even with the ``--no-window`` command-line argument, |
21 | | - you'll need to have OpenGL support available on the Windows machine. |
| 7 | +have a GPU or display server available, you'll need run Godot with the ``headless`` |
| 8 | +display server and ``Dummy`` :ref:`audio driver <class_ProjectSettings_property_audio/driver/driver>`. |
22 | 9 |
|
23 | | -If your project uses C#, you'll have to use a Mono-enabled server binary. |
| 10 | +Since Godot 4.0, this can be done by running a Godot binary on any platform with |
| 11 | +the ``--headless`` command line argument, or running a project exported as |
| 12 | +dedicated server. You do not need to use a specialized server binary anymore, |
| 13 | +unlike Godot 3.x. |
24 | 14 |
|
25 | | -"Headless" versus "server" binaries |
26 | | ------------------------------------ |
| 15 | +Editor versus export template |
| 16 | +----------------------------- |
27 | 17 |
|
28 | | -The `server download page <https://godotengine.org/download/server>`__ |
29 | | -offers two kinds of binaries with several differences. |
| 18 | +It is possible to use either an editor or export template (debug or release) |
| 19 | +binary in headless mode. Which one you should use depends on your use case: |
30 | 20 |
|
31 | | -- **Server:** Use this one for running dedicated servers. It does not contain |
32 | | - editor functionality, and is therefore smaller and more |
33 | | - optimized. |
34 | | -- **Headless:** This binary contains editor functionality and is intended to be |
| 21 | +- **Export template:** Use this one for running dedicated servers. It does not |
| 22 | + contain editor functionality, and is therefore smaller and more optimized. |
| 23 | +- **Editor:** This binary contains editor functionality and is intended to be |
35 | 24 | used for exporting projects. This binary *can* be used to run dedicated |
36 | 25 | servers, but it's not recommended as it's larger and less optimized. |
37 | 26 |
|
38 | | -Exporting a PCK file |
39 | | --------------------- |
| 27 | +Export approaches |
| 28 | +----------------- |
40 | 29 |
|
41 | 30 | There are two ways to export a project for a server: |
42 | 31 |
|
43 | | -- Create a Linux/X11 export preset, define a custom Release export template |
44 | | - that points to the server binary then export the project as usual. |
45 | | -- Export a PCK file only, preferably from a Linux/X11 export preset. |
46 | | - |
47 | | -Both methods should result in identical output. The text below describes the PCK |
48 | | -file approach. |
| 32 | +- Create a separate export preset for the platform that will host the server, then |
| 33 | + export your project as usual. |
| 34 | +- Export a PCK file only, preferably for the platform that matches the platform |
| 35 | + that will host the server. Place this PCK file in the same folder as an export |
| 36 | + template binary, rename the binary to have the same name as the PCK (minus the |
| 37 | + file extension), then run the binary. |
49 | 38 |
|
50 | | -Once you've downloaded a server binary, you should export a PCK file containing |
51 | | -your project data. After creating the export preset, click **Export PCK/ZIP** at |
52 | | -the bottom of the Export dialog then choose a destination path. |
53 | | -The **Export With Debug** checkbox in the file dialog has no bearing on the |
54 | | -final PCK file, so you can leave it as-is. |
| 39 | +Both methods should result in identical output. The rest of the page will focus |
| 40 | +on the first approach. |
55 | 41 |
|
56 | 42 | See :ref:`doc_exporting_projects` for more information. |
57 | 43 |
|
58 | | -.. note:: |
| 44 | +.. _doc_exporting_for_dedicated_servers_exporting_project: |
| 45 | + |
| 46 | +Exporting a project for a dedicated server |
| 47 | +------------------------------------------ |
| 48 | + |
| 49 | +If you export a project as usual when targeting a server, you will notice that |
| 50 | +the PCK file is just as large as for the client. This is because it includes all |
| 51 | +resources, including those the server doesn't need (such as texture data). |
| 52 | +Additionally, headless mode won't be automatically used; the user will have to |
| 53 | +specify ``--headless`` to make sure no window spawns. |
| 54 | + |
| 55 | +Many resources such as textures can be stripped from the PCK file to greatly |
| 56 | +reduce its size. Godot offers a way to do this for textures and materials in a way |
| 57 | +that preserves references in scene or resource files (built-in or external). |
59 | 58 |
|
60 | | - If you're exporting the project from a headless editor, call the headless |
61 | | - editor with the ``--export-pack`` option while in the project folder to export |
62 | | - only a PCK file. |
| 59 | +To begin doing so, make sure you have a dedicated export preset for your server, |
| 60 | +then select it, go to its **Resources** tab and change its export mode: |
| 61 | + |
| 62 | +.. figure:: img/exporting_for_dedicated_servers_export_mode.webp |
| 63 | + :align: center |
| 64 | + :alt: Choosing the **Export as dedicated server** export mode in the export preset |
| 65 | + |
| 66 | + Choosing the **Export as dedicated server** export mode in the export preset |
| 67 | + |
| 68 | +When this export mode is chosen, the ``dedicated_server`` feature tag is |
| 69 | +automatically added to the exported project. |
63 | 70 |
|
64 | 71 | .. note:: |
65 | 72 |
|
66 | | - The PCK file will include resources not normally needed by the server, such |
67 | | - as textures and sounds. This means the PCK file will be larger than it could |
68 | | - possibly be. Support for stripping unneeded resources from a PCK for server |
69 | | - usage is planned in a future Godot release. |
| 73 | + If you do not wish to use this |
| 74 | + export mode but still want the feature tag, you can write the name |
| 75 | + ``dedicated_server`` in the **Features** tab of the export preset. |
| 76 | + This will also force ``--headless`` when running the exported project. |
| 77 | + |
| 78 | +After selecting this export mode, you will be presented with a list of resources |
| 79 | +in the project: |
| 80 | + |
| 81 | +.. figure:: img/exporting_for_dedicated_servers_export_resources.webp |
| 82 | + :align: center |
| 83 | + :alt: Choosing resources to keep, keep with stripped visuals or remove |
70 | 84 |
|
71 | | - On the bright side, this allows the same PCK file to be used both by a |
72 | | - client and dedicated server build. This can be useful if you want to ship a |
73 | | - single archive that can be used both as a client and dedicated server. |
| 85 | + Choosing resources to keep, keep with stripped visuals or remove |
74 | 86 |
|
75 | | -Preparing the server distribution |
76 | | ---------------------------------- |
| 87 | +Ticking a box allows you to override options for the specified file or folder. |
| 88 | +Checking boxes does **not** affect which files are exported; this is done by the |
| 89 | +options selected for each checkbox instead. |
77 | 90 |
|
78 | | -After downloading or compiling a server binary, you should now place it in the |
79 | | -same folder as the PCK file you've exported. The server binary should have the |
80 | | -same name as the PCK (excluding the extension). This lets Godot detect and use |
81 | | -the PCK file automatically. If you want to start a server with a PCK that has a |
82 | | -different name, you can specify the path to the PCK file using the |
83 | | -``--main-pack`` command-line argument:: |
| 91 | +Files within a checked folder will automatically use the parent's option by |
| 92 | +default, which is indicated by the **(Inherited)** suffix for the option name |
| 93 | +(and the option name being grayed out). To change the option for a file whose |
| 94 | +option is currently inherited, you must tick the box next to it first. |
84 | 95 |
|
85 | | - ./godot-server --main-pack my_project.pck |
| 96 | +- **Strip Visuals:** Export this resource, with visual files (textures and materials) |
| 97 | + replaced by placeholder classes. Placeholder classes store the image size |
| 98 | + (as it's sometimes used to position elements in a 2D scene), but nothing else. |
| 99 | +- **Keep:** Export this resource as usual, with visual files interact. |
| 100 | +- **Remove:** The file is not included in the PCK. This is useful to ignore |
| 101 | + scenes and resources that only the client needs. If you do so, make sure the |
| 102 | + server doesn't reference these client-only scenes and resources in any way. |
| 103 | + |
| 104 | +The general recommendation is to use **Strip Visuals** whenever possible, unless |
| 105 | +the server needs to access image data such as pixels' colors. For example, if |
| 106 | +your server generates collision data based on an image's contents, you need to |
| 107 | +use **Keep** for that particular image. |
| 108 | + |
| 109 | +.. tip:: |
| 110 | + |
| 111 | + To check the file structure of your exported PCK, use the **Export |
| 112 | + PCK/ZIP...** button with a ``.zip`` file extension, then open the resulting |
| 113 | + ZIP file in a file manager. |
86 | 114 |
|
87 | 115 | .. warning:: |
88 | 116 |
|
89 | | - Make sure the aforementioned ``godot-server`` has the executable permission |
90 | | - defined by running ``chmod +x godot-server`` (if the binary is called |
91 | | - ``godot-server``). Otherwise, you may get a "command not found" or |
92 | | - "permission denied" error message when trying to run the Godot server |
93 | | - binary. |
| 117 | + Be careful when using the **Remove** mode, as scenes/resources that reference |
| 118 | + a removed file will no longer be able to load successfully. |
| 119 | + |
| 120 | + If you wish to remove specific resources but make the scenes still be able |
| 121 | + to load without them, you'll have to remove the reference in the scene file |
| 122 | + and load the files to the nodes' properties using ``load()`` in a script. |
| 123 | + This approach can be used to strip resources that Godot doesn't support |
| 124 | + replacing with placeholders yet, such as audio. |
| 125 | + |
| 126 | + Removing textures is often what makes the greatest impact on the PCK size, |
| 127 | + so it is recommended to stick with **Strip Visuals** at first. |
| 128 | + |
| 129 | +With the above options used, a PCK for the client (which exports all resources |
| 130 | +normally) will look as follows: |
| 131 | + |
| 132 | +.. highlight:: none |
| 133 | + |
| 134 | +:: |
| 135 | + |
| 136 | + . |
| 137 | + ├── .godot |
| 138 | + │ ├── exported |
| 139 | + │ │ └── 133200997 |
| 140 | + │ │ └── export-78c237d4bfdb4e1d02e0b5f38ddfd8bd-scene.scn |
| 141 | + │ ├── global_script_class_cache.cfg |
| 142 | + │ ├── imported |
| 143 | + │ │ ├── map_data.png-ce840618f399a990343bfc7298195a13.ctex |
| 144 | + │ │ ├── music.ogg-fa883da45ae49695a3d022f64e60aee2.oggvorbisstr |
| 145 | + │ │ └── sprite.png-7958af25f91bb9dbae43f35388f8e840.ctex |
| 146 | + │ └── uid_cache.bin |
| 147 | + ├── client |
| 148 | + │ ├── music.ogg.import |
| 149 | + │ └── sprite.png.import |
| 150 | + ├── server |
| 151 | + │ └── map_data.png.import |
| 152 | + ├── test |
| 153 | + │ └── scene.gd |
| 154 | + └── unused |
| 155 | + │ └── development_test.gd |
| 156 | + ├── project.binary |
| 157 | + ├── scene.gd |
| 158 | + ├── scene.tscn.remap |
| 159 | + |
| 160 | +The PCK's file structure for the server will look as follows: |
| 161 | + |
| 162 | +.. highlight:: none |
| 163 | + |
| 164 | +:: |
| 165 | + |
| 166 | + . |
| 167 | + ├── .godot |
| 168 | + │ ├── exported |
| 169 | + │ │ └── 3400186661 |
| 170 | + │ │ ├── export-78c237d4bfdb4e1d02e0b5f38ddfd8bd-scene.scn |
| 171 | + │ │ ├── export-7958af25f91bb9dbae43f35388f8e840-sprite.res # Placeholder texture |
| 172 | + │ │ └── export-fa883da45ae49695a3d022f64e60aee2-music.res |
| 173 | + │ ├── global_script_class_cache.cfg |
| 174 | + │ ├── imported |
| 175 | + │ │ └── map_data.png-ce840618f399a990343bfc7298195a13.ctex |
| 176 | + │ └── uid_cache.bin |
| 177 | + ├── client |
| 178 | + │ ├── music.ogg.import |
| 179 | + │ └── sprite.png.import # Points to placeholder texture |
| 180 | + └── server |
| 181 | + │ └── map_data.png.import |
| 182 | + ├── project.binary |
| 183 | + ├── scene.gd |
| 184 | + ├── scene.tscn.remap |
94 | 185 |
|
95 | 186 | Starting the dedicated server |
96 | 187 | ----------------------------- |
97 | 188 |
|
98 | 189 | If both your client and server are part of the same Godot project, you will have |
99 | | -to add a way to start the server directly using a command-line argument. This |
100 | | -can be done by adding the following code snippet in your main scene (or a |
101 | | -singleton)'s ``_ready()`` method: |
| 190 | +to add a way to start the server directly using a command-line argument. |
| 191 | + |
| 192 | +If you :ref:`exported the project <doc_exporting_for_dedicated_servers_exporting_project>` |
| 193 | +using the **Export as dedicated server** export mode (or have added |
| 194 | +``dedicated_server`` as a custom feature tag), you can use the ``dedicated_server`` |
| 195 | +feature tag to detect whether a dedicated server PCK is being used: |
102 | 196 |
|
103 | 197 | .. tabs:: |
104 | 198 | .. code-tab:: gdscript |
105 | 199 |
|
106 | | - if "--server" in OS.get_cmdline_args(): |
| 200 | + # Note: Feature tags are case-sensitive. |
| 201 | + if OS.has_feature("dedicated_server"): |
107 | 202 | # Run your server startup code here... |
| 203 | + pass |
| 204 | + |
| 205 | + .. code-tab:: csharp |
| 206 | + |
| 207 | + // Note: Feature tags are case-sensitive. |
| 208 | + if (OS.HasFeature("dedicated_server")) |
| 209 | + { |
| 210 | + // Run your server startup code here... |
| 211 | + } |
| 212 | + |
| 213 | +If you also wish to host a server when using the built-in ``--headless`` command |
| 214 | +line argument, this can be done by adding the following code snippet in your |
| 215 | +main scene (or an autoload)'s ``_ready()`` method: |
| 216 | + |
| 217 | +.. tabs:: |
| 218 | + .. code-tab:: gdscript |
| 219 | + |
| 220 | + if "--headless" in OS.get_cmdline_args(): |
| 221 | + # Run your server startup code here... |
| 222 | + # |
108 | 223 | # Using this check, you can start a dedicated server by running |
109 | | - # a Godot binary (headless or not) with the `--server` command-line argument. |
| 224 | + # a Godot binary (editor or export template) with the `--headless` |
| 225 | + # command-line argument. |
110 | 226 | pass |
111 | 227 |
|
112 | 228 | .. code-tab:: csharp |
113 | 229 |
|
114 | 230 | using System.Linq; |
115 | 231 |
|
116 | | - if (OS.GetCmdlineArgs().Contains("--server")) |
| 232 | + if (OS.GetCmdlineArgs().Contains("--headless")) |
117 | 233 | { |
118 | 234 | // Run your server startup code here... |
| 235 | + // |
119 | 236 | // Using this check, you can start a dedicated server by running |
120 | | - // a Godot binary (headless or not) with the `--server` command-line argument. |
| 237 | + // a Godot binary (editor or export template) with the `--headless` |
| 238 | + // command-line argument. |
121 | 239 | } |
122 | 240 |
|
123 | | -Alternatively, you can make the dedicated server always start up if a headless |
124 | | -or server binary is detected: |
| 241 | +If you wish to use a custom command line argument, this can be done by adding |
| 242 | +the following code snippet in your main scene (or an autoload)'s ``_ready()`` |
| 243 | +method: |
125 | 244 |
|
126 | 245 | .. tabs:: |
127 | 246 | .. code-tab:: gdscript |
128 | 247 |
|
129 | | - # Note: Feature tags are case-sensitive! It's "server", not "Server". |
130 | | - if OS.has_feature("server"): |
| 248 | + if "--server" in OS.get_cmdline_user_args(): |
131 | 249 | # Run your server startup code here... |
132 | | - # Note that using this check may break unit testing scripts when |
133 | | - # running them with headless or server binaries. |
| 250 | + # |
| 251 | + # Using this check, you can start a dedicated server by running |
| 252 | + # a Godot binary (editor or export template) with the `--server` |
| 253 | + # command-line argument. |
134 | 254 | pass |
135 | 255 |
|
136 | 256 | .. code-tab:: csharp |
137 | 257 |
|
138 | | - // Note: Feature tags are case-sensitive! It's "server", not "Server". |
139 | | - if (OS.HasFeature("server")) |
| 258 | + using System.Linq; |
| 259 | + |
| 260 | + if (OS.GetCmdlineUserArgs().Contains("--server")) |
140 | 261 | { |
141 | 262 | // Run your server startup code here... |
142 | | - // Note that using this check may break unit testing scripts when |
143 | | - // running them with headless or server binaries. |
| 263 | + // |
| 264 | + // Using this check, you can start a dedicated server by running |
| 265 | + // a Godot binary (editor or export template) with the `--server` |
| 266 | + // command-line argument. |
144 | 267 | } |
145 | 268 |
|
| 269 | +It's a good idea to add at least one of the above command-line arguments to |
| 270 | +start a server, as it can be used to test server functionality from the command |
| 271 | +line without having to export the project. |
| 272 | + |
146 | 273 | If your client and server are separate Godot projects, your server should most |
147 | 274 | likely be configured in a way where running the main scene starts a server |
148 | 275 | automatically. |
|
0 commit comments