exporting.rst 15 KB

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