exporting_basics.rst 15 KB

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