exporting_pcks.rst 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. .. _doc_exporting_pcks:
  2. Exporting packs, patches, and mods
  3. ==================================
  4. Use cases
  5. ---------
  6. Oftentimes, one would like to add functionality to one's game after it has been
  7. deployed.
  8. Examples of this include...
  9. - Downloadable Content: the ability to add features and content to one's game.
  10. - Patches: the ability to fix a bug that is present in a shipped product.
  11. - Mods: grant other people the ability to create content for one's game.
  12. These tools help developers to extend their development beyond the initial
  13. release.
  14. Overview of PCK/ZIP files
  15. -------------------------
  16. Godot enables this via a feature called **resource packs** (PCK files,
  17. with the ``.pck`` extension, or ZIP files).
  18. **Advantages:**
  19. - incremental updates/patches
  20. - offer DLCs
  21. - offer mod support
  22. - no source code disclosure needed for mods
  23. - more modular project structure
  24. - users don't have to replace the entire game
  25. The first part of using them involves exporting and delivering the project to
  26. players. Then, when one wants to add functionality or content later on, they
  27. just deliver the updates via PCK/ZIP files to the users.
  28. PCK/ZIP files usually contain, but are not limited to:
  29. - scripts
  30. - scenes
  31. - shaders
  32. - models
  33. - textures
  34. - sound effects
  35. - music
  36. - any other asset suitable for import into the game
  37. The PCK/ZIP files can even be an entirely different Godot project, which the
  38. original game loads in at runtime.
  39. It is possible to load both PCK and ZIP files as additional packs at the same time.
  40. See :ref:`doc_exporting_projects_pck_versus_zip` for a comparison of the two formats.
  41. .. seealso::
  42. If you want to load loose files at runtime (not packed in a PCK or ZIP by Godot),
  43. consider using :ref:`doc_runtime_loading_and_saving` instead.
  44. This is useful for loading user-generated content that is not made with Godot,
  45. without requiring users to pack their mods into a specific file format.
  46. The downside of this approach is that it's less transparent to the game logic,
  47. as it will not benefit from the same resource management as PCK/ZIP files.
  48. Generating PCK files
  49. --------------------
  50. In order to pack all resources of a project into a PCK file, open the project
  51. and go to **Project > Export** and click on **Export PCK/ZIP**. Also, make sure
  52. to have an export preset selected while doing so.
  53. .. image:: img/export_pck.webp
  54. Another method would be to :ref:`export from the command line <doc_command_line_tutorial_exporting>`
  55. with ``--export-pack``. The output file must with a ``.pck`` or ``.zip``
  56. file extension. The export process will build that type of file for the
  57. chosen platform.
  58. .. note::
  59. If one wishes to support mods for their game, they will need their users to
  60. create similarly exported files. Assuming the original game expects a
  61. certain structure for the PCK's resources and/or a certain interface for
  62. its scripts, then either...
  63. 1. The developer must publicize documentation of these expected structures/
  64. interfaces, expect modders to install Godot Engine, and then also expect
  65. those modders to conform to the documentation's defined API when building
  66. mod content for the game (so that it will work). Users would then use
  67. Godot's built in exporting tools to create a PCK file, as detailed
  68. above.
  69. 2. The developer uses Godot to build a GUI tool for adding their exact API
  70. content to a project. This Godot tool must either run on a tools-enabled
  71. build of the engine or have access to one (distributed alongside or
  72. perhaps in the original game's files). The tool can then use the Godot
  73. executable to export a PCK file from the command line with
  74. :ref:`OS.execute() <class_OS_method_execute>`. The game itself shouldn't
  75. use a tool-build of the engine (for security), so it's best to keep
  76. the modding tool and game separate.
  77. Opening PCK or ZIP files at runtime
  78. -----------------------------------
  79. To load a PCK or ZIP file, one uses the ProjectSettings singleton. The following
  80. example expects a ``mod.pck`` file in the directory of the game's executable.
  81. The PCK or ZIP file contains a ``mod_scene.tscn`` test scene in its root.
  82. .. tabs::
  83. .. code-tab:: gdscript GDScript
  84. func _your_function():
  85. # This could fail if, for example, mod.pck cannot be found.
  86. var success = ProjectSettings.load_resource_pack(OS.get_executable_path().get_base_dir().path_join("mod.pck"))
  87. if success:
  88. # Now one can use the assets as if they had them in the project from the start.
  89. var imported_scene = load("res://mod_scene.tscn")
  90. .. code-tab:: csharp
  91. private void YourFunction()
  92. {
  93. // This could fail if, for example, mod.pck cannot be found.
  94. var success = ProjectSettings.LoadResourcePack(OS.get_executable_path().get_base_dir().path_join("mod.pck));
  95. if (success)
  96. {
  97. // Now one can use the assets as if they had them in the project from the start.
  98. var importedScene = (PackedScene)ResourceLoader.Load("res://mod_scene.tscn");
  99. }
  100. }
  101. .. warning::
  102. By default, if you import a file with the same file path/name as one you
  103. already have in your project, the imported one will replace it. This is
  104. something to watch out for when creating DLC or mods. You can solve this
  105. problem by using a tool that isolates mods to a specific mods subfolder.
  106. However, it is also a way of creating patches for one's own game. A PCK/ZIP
  107. file of this kind can fix the content of a previously loaded PCK/ZIP
  108. (therefore, the order in which packs are loaded matters).
  109. To opt out of this behavior, pass ``false`` as the second argument to
  110. :ref:`ProjectSettings.load_resource_pack() <class_ProjectSettings_method_load_resource_pack>`.
  111. .. note::
  112. For a C# project, you need to build the DLL and place it in the project directory first.
  113. Then, before loading the resource pack, you need to load its DLL as follows:
  114. ``Assembly.LoadFile("mod.dll")``
  115. Troubleshooting
  116. ^^^^^^^^^^^^^^^
  117. If you are loading a resource pack and are not noticing any changes, it may be
  118. due to the pack being loaded too late. This is particularly the case with menu
  119. scenes that may preload other scenes using
  120. :ref:`preload() <class_@GDScript_method_preload>`. This means that loading
  121. a pack in the menu will not affect the other scene that was already preloaded.
  122. To avoid this, you need to load the pack as early as possible.
  123. To do so, create a new :ref:`autoload <doc_singletons_autoload>` script and
  124. call :ref:`ProjectSettings.load_resource_pack() <class_ProjectSettings_method_load_resource_pack>`
  125. in the autoload script's ``_init()`` function, rather than ``_enter_tree()``
  126. or ``_ready()``.
  127. Summary
  128. -------
  129. This tutorial explains how to add mods, patches, or DLC to a game.
  130. The most important thing is to identify how one plans to distribute future
  131. content for their game and develop a workflow that is customized for that
  132. purpose. Godot should make that process smooth regardless of which route a
  133. developer pursues.