exporting_basics.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. .. _doc_exporting_basics:
  2. Exporting
  3. =========
  4. Overview
  5. --------
  6. Now that you have a working game, you probably want to share your success with
  7. others. However, it's not practical to ask your friends to download Godot
  8. just so they can open your project. Instead, you can *export* your project,
  9. converting it into a "package" that can be run by anyone.
  10. The way you export your game depends on what platform you are targeting. In
  11. this tutorial, you'll learn how to export the *Dodge the Creeps* game for a
  12. variety of platforms. First, however, we need to make some changes to the
  13. way the game works.
  14. .. note:: If you haven't made "Dodge the Creeps" yourself yet, please read
  15. :ref:`doc_your_first_2d_game` before continuing with this tutorial.
  16. Preparing the project
  17. ---------------------
  18. In *Dodge the Creeps*, we used keyboard controls to move the player's character.
  19. This is fine if your game is being played on a PC platform, but on a phone
  20. or tablet, you need to support touchscreen input. Because a click event can
  21. be treated the same as a touch event, we'll convert the game to a click-and-move
  22. input style.
  23. By default, Godot emulates mouse input from touch input. That means that if
  24. anything is coded to happen on a mouse event, touch will trigger it as well.
  25. Godot can also emulate touch input from mouse clicks, which we will need to be
  26. able to keep playing our game on our computer after we switch to touch input.
  27. In **Project > Project Settings**, under **Input Devices > Pointing**, enable
  28. **Emulate Touch From Mouse**.
  29. .. image:: img/export_touchsettings.png
  30. We also want to ensure that the game scales consistently on different-sized screens,
  31. so in the project settings go to **Display**, then click on **Window**. In the **Stretch**
  32. options, set **Mode** to ``2d`` and **Aspect** to ``keep``.
  33. Since we are already in the **Window** settings, we should also set under **Handheld**
  34. the **Orientation** to ``portrait``.
  35. .. image:: img/export_handheld_stretchsettings.png
  36. Next, we need to modify the ``Player.gd`` script to change the input method.
  37. We'll remove the key inputs and make the player move towards a "target" that's
  38. set by the touch (or click) event.
  39. Here is the full script for the player, with comments noting what we've
  40. changed:
  41. .. tabs::
  42. .. code-tab:: gdscript GDScript
  43. extends Area2D
  44. signal hit
  45. export var speed = 400
  46. var screen_size
  47. # Add this variable to hold the clicked position.
  48. var target = Vector2()
  49. func _ready():
  50. hide()
  51. screen_size = get_viewport_rect().size
  52. func start(pos):
  53. position = pos
  54. # Initial target is the start position.
  55. target = pos
  56. show()
  57. $CollisionShape2D.disabled = false
  58. # Change the target whenever a touch event happens.
  59. func _input(event):
  60. if event is InputEventScreenTouch and event.pressed:
  61. target = event.position
  62. func _process(delta):
  63. var velocity = Vector2()
  64. # Move towards the target and stop when close.
  65. if position.distance_to(target) > 10:
  66. velocity = target - position
  67. # Remove keyboard controls.
  68. # if Input.is_action_pressed("ui_right"):
  69. # velocity.x += 1
  70. # if Input.is_action_pressed("ui_left"):
  71. # velocity.x -= 1
  72. # if Input.is_action_pressed("ui_down"):
  73. # velocity.y += 1
  74. # if Input.is_action_pressed("ui_up"):
  75. # velocity.y -= 1
  76. if velocity.length() > 0:
  77. velocity = velocity.normalized() * speed
  78. $AnimatedSprite.play()
  79. else:
  80. $AnimatedSprite.stop()
  81. position += velocity * delta
  82. # We still need to clamp the player's position here because on devices that don't
  83. # match your game's aspect ratio, Godot will try to maintain it as much as possible
  84. # by creating black borders, if necessary.
  85. # Without clamp(), the player would be able to move under those borders.
  86. position.x = clamp(position.x, 0, screen_size.x)
  87. position.y = clamp(position.y, 0, screen_size.y)
  88. if velocity.x != 0:
  89. $AnimatedSprite.animation = "walk"
  90. $AnimatedSprite.flip_v = false
  91. $AnimatedSprite.flip_h = velocity.x < 0
  92. elif velocity.y != 0:
  93. $AnimatedSprite.animation = "up"
  94. $AnimatedSprite.flip_v = velocity.y > 0
  95. func _on_Player_body_entered( body ):
  96. hide()
  97. emit_signal("hit")
  98. $CollisionShape2D.set_deferred("disabled", true)
  99. .. code-tab:: csharp
  100. using Godot;
  101. using System;
  102. public class Player : Area2D
  103. {
  104. [Signal]
  105. public delegate void Hit();
  106. [Export]
  107. public int Speed = 400;
  108. private Vector2 _screenSize;
  109. // Add this variable to hold the clicked position.
  110. private Vector2 _target;
  111. public override void _Ready()
  112. {
  113. Hide();
  114. _screenSize = GetViewport().Size;
  115. }
  116. public void Start(Vector2 pos)
  117. {
  118. Position = pos;
  119. // Initial target us the start position.
  120. _target = pos;
  121. Show();
  122. GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
  123. }
  124. // Change the target whenever a touch event happens.
  125. public override void _Input(InputEvent @event)
  126. {
  127. if (@event is InputEventScreenTouch eventMouseButton && eventMouseButton.Pressed)
  128. {
  129. _target = (@event as InputEventScreenTouch).Position;
  130. }
  131. }
  132. public override void _Process(float delta)
  133. {
  134. var velocity = new Vector2();
  135. // Move towards the target and stop when close.
  136. if (Position.DistanceTo(_target) > 10)
  137. {
  138. velocity = _target - Position;
  139. }
  140. // Remove keyboard controls.
  141. //if (Input.IsActionPressed("ui_right"))
  142. //{
  143. // velocity.x += 1;
  144. //}
  145. //if (Input.IsActionPressed("ui_left"))
  146. //{
  147. // velocity.x -= 1;
  148. //}
  149. //if (Input.IsActionPressed("ui_down"))
  150. //{
  151. // velocity.y += 1;
  152. //}
  153. //if (Input.IsActionPressed("ui_up"))
  154. //{
  155. // velocity.y -= 1;
  156. //}
  157. var animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
  158. if (velocity.Length() > 0)
  159. {
  160. velocity = velocity.Normalized() * Speed;
  161. animatedSprite.Play();
  162. }
  163. else
  164. {
  165. animatedSprite.Stop();
  166. }
  167. Position += velocity * delta;
  168. // We still need to clamp the player's position here because on devices that don't
  169. // match your game's aspect ratio, Godot will try to maintain it as much as possible
  170. // by creating black borders, if necessary.
  171. // Without clamp(), the player would be able to move under those borders.
  172. Position = new Vector2(
  173. x: Mathf.Clamp(Position.x, 0, _screenSize.x),
  174. y: Mathf.Clamp(Position.y, 0, _screenSize.y)
  175. );
  176. if (velocity.x != 0)
  177. {
  178. animatedSprite.Animation = "walk";
  179. animatedSprite.FlipV = false;
  180. animatedSprite.FlipH = velocity.x < 0;
  181. }
  182. else if(velocity.y != 0)
  183. {
  184. animatedSprite.Animation = "up";
  185. animatedSprite.FlipV = velocity.y > 0;
  186. }
  187. }
  188. public void OnPlayerBodyEntered(PhysicsBody2D body)
  189. {
  190. Hide(); // Player disappears after being hit.
  191. EmitSignal("Hit");
  192. GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
  193. }
  194. }
  195. Setting a main scene
  196. --------------------
  197. The main scene is the one that your game will start in. For this
  198. *Dodge the Creeps* example, in
  199. **Project -> Project Settings -> Application -> Run**, set **Main Scene**
  200. to ``Main.tscn`` by clicking the folder icon and selecting it.
  201. Export templates
  202. ----------------
  203. To export the project, you need to download the *export templates* from the
  204. http://godotengine.org/download. These templates are optimized versions of the engine
  205. without the editor pre-compiled for each platform. You can also
  206. download them in Godot by clicking on **Editor -> Manage Export Templates**:
  207. .. image:: img/export_template_menu.png
  208. .. note::
  209. If you've downloaded Godot from
  210. `Steam <https://store.steampowered.com/app/404790/Godot_Engine/>`__,
  211. export templates are already included. Therefore, you don't need to download
  212. them using the **Manage Export Templates** dialog.
  213. In the window that appears, you can click **Download** to get the template
  214. version that matches your version of Godot.
  215. .. image:: img/export_template_manager.png
  216. .. note::
  217. Export templates are bound to a specific Godot version. If you upgrade
  218. Godot, you must download templates that match the new version.
  219. Export presets
  220. --------------
  221. Next, you can configure the export settings by clicking on **Project -> Export**.
  222. Create a new export preset by clicking **Add...** and selecting a platform. You
  223. can make as many presets as you like with different settings.
  224. .. image:: img/export_presets_window.png
  225. At the bottom of the window are two buttons. **Export PCK/ZIP** only creates
  226. a packed version of your project's data. This doesn't include an executable
  227. so the project can't be run on its own.
  228. The second button, **Export Project**, creates a complete executable version
  229. of your game, such as an ``.apk`` for Android or an ``.exe`` for Windows.
  230. In the **Resources** and **Features** tabs, you can customize how the game is
  231. exported for each platform. We can leave those settings alone for now.
  232. Exporting by platform
  233. ---------------------
  234. In this section, we'll walk through the process for each platform,
  235. including any additional software or requirements you'll need.
  236. PC (Linux/macOS/Windows)
  237. ~~~~~~~~~~~~~~~~~~~~~~~~
  238. Exporting PC platforms works the same across the three supported operating
  239. systems. Open the export window and click **Add...** to create the preset(s) you
  240. want to make. Then click **Export Project** and choose a name and destination
  241. folder. Choose a location *outside* of your project folder.
  242. Click **Save** and the engine will build the export files.
  243. .. note::
  244. When exporting for macOS, if you export from a macOS computer, you'll end up
  245. with a ``.dmg`` file, while using Linux or Windows produces a ``.zip``. In
  246. either case, the compressed file contains a macOS ``.app`` that you can
  247. double-click and run.
  248. .. note::
  249. On Windows, if you want your exported executable to have a different icon
  250. than the default one, you need to change it manually. See
  251. :ref:`doc_changing_application_icon_for_windows`.
  252. Android
  253. ~~~~~~~
  254. .. tip::
  255. Mobile devices come with a wide variety of capabilities. In most cases,
  256. Godot's default settings will work, but mobile development is sometimes more
  257. art than science, and you may need to do some experimenting and searching
  258. for help in order to get everything working.
  259. Before you can export your project for Android, you must download the following
  260. software:
  261. * Android SDK: https://developer.android.com/studio/
  262. * Open JDK (**version 8 is required**, more recent versions won't work): https://adoptopenjdk.net/index.html
  263. When you run Android Studio for the first time, click on **Configure -> SDK Manager**
  264. and install **Android SDK Platform Tools**. This installs the ``adb``
  265. command-line tool that Godot uses to communicate with your device.
  266. Next, create a debug keystore by running the following command on your
  267. system's command line:
  268. .. code-block:: shell
  269. keytool -keyalg RSA -genkeypair -alias androiddebugkey -keypass android -keystore debug.keystore -storepass android -dname "CN=Android Debug,O=Android,C=US" -validity 9999
  270. Click on *Editor -> Editor Settings* in Godot and select the *Export/Android*
  271. section. Here, you need to set the paths to the Android SDK applications on
  272. your system and the location of the keystore you just created.
  273. .. image:: img/export_editor_android_settings.png
  274. Now you're ready to export. Click on **Project -> Export** and add a preset
  275. for Android (see above). Select the newly added Android preset. Under **Options**,
  276. go to **Screen** and set **Orientation** to **Portrait**. If your game is in
  277. landscape mode (i.e. the window width in pixels is greater than the window height),
  278. leave this on **Landscape**.
  279. Click the **Export Project** button and Godot will build an APK you can download
  280. on your device. To do this on the command line, use the following:
  281. .. code-block:: shell
  282. adb install dodge.apk
  283. .. note:: Your device may need to be in *developer mode*. Consult your
  284. device's documentation for details.
  285. If your system supports it, connecting a compatible Android device will cause
  286. a **One-click Deploy** button to appear in Godot's playtest button area:
  287. .. image:: img/export_android_oneclick.png
  288. Clicking this button builds the APK and copies it onto your device in one step.
  289. iOS
  290. ~~~
  291. .. note::
  292. To build your game for iOS, you must have a computer running macOS with
  293. Xcode installed.
  294. Before exporting, there are some settings that you *must* complete for the project
  295. to export successfully. First, the **App Store Team Id**, which you can find by
  296. logging in to your Apple developer account and looking in the **Membership** section.
  297. You must also provide icons and splash screen images as shown below:
  298. .. image:: img/export_ios_settings.png
  299. Click **Export Project** and select a destination folder.
  300. Once you have successfully exported the project, you'll find the following
  301. folders and files have been created in your selected location:
  302. .. image:: img/export_xcode_project_folders.png
  303. You can now open the project in Xcode and build the project for iOS.
  304. The Xcode build procedure is beyond the scope of this tutorial.
  305. See https://help.apple.com/xcode/mac/current/#/devc8c2a6be1
  306. for more information.
  307. HTML5 (web)
  308. ~~~~~~~~~~~
  309. Click **Export Project** on the HTML5 preset. We don't need to change any
  310. of the default settings.
  311. When the export is complete, you'll have a folder containing the following
  312. files:
  313. .. image:: img/export_web_files.png
  314. Viewing the ``.html`` file in your browser lets you play the game. However, you
  315. can't open the file directly. Instead, it needs to be served by a web server. If
  316. you don't have one set up on your computer, you can search online to find
  317. suggestions for your specific OS.
  318. Point your browser at the URL where you've placed the HTML file. You may have
  319. to wait a few moments while the game loads before you see the start screen.
  320. .. image:: img/export_web_example.png
  321. The console window beneath the game tells you if anything goes wrong. You can
  322. disable it by disabling **Export With Debug** in the final file dialog that appears
  323. when you export the project.
  324. .. image:: img/export_web_export_with_debug_disabled.png
  325. .. note::
  326. While WebAssembly is supported in all major browsers, it is still an
  327. emerging technology and you may find some things that don't work. Make sure
  328. you have updated your browser to the most recent version, and report any
  329. bugs you find on the
  330. `Godot GitHub repository <https://github.com/godotengine/godot/issues>`_.