Exporting

Overview

Now that you have a working game, you probably want to share your success with others. However, it’s not practical to ask your friends to download Godot just so they can open your project. Instead, you can export your project, converting it into a “package” that can be run by anyone.

The way you export your game depends on what platform you are targeting. In this tutorial, you’ll learn how to export the “Dodge the Creeps” game for a variety of platforms. First, however, we need to make some changes to the way the game works.

Note

If you haven’t made “Dodge the Creeps” yourself yet, please read Your first game before continuing with this tutorial.

Preparing the project

In “Dodge the Creeps” we used keyboard controls to move the player’s character. This is fine if your game is being played on a PC platform, but on a phone or tablet, you need to support touchscreen input. Because a click event can be treated the same as a touch event, we’ll convert the game to a click-and-move input style.

By default Godot emulates mouse input from touch input. That means if anything is coded to happen on a mouse event, touch will trigger it as well. Godot can also emulate touch input from mouse clicks, which we will need to be able to keep playing our game on our computer after we switch to touch input. In the “Project Settings” under Input Devices and Pointing, set Emulate Touch From Mouse to “On”.

../../_images/export_touchsettings.png

We also want to ensure that the game scales consistently on different-sized screens, so in the project settings go to Display, then click on Window. In the Stretch options, set Mode to “2d” and Aspect to “keep”.

Since we are already in the Window settings, we should also set under Handheld the Orientation to “portrait”.

../../_images/export_handheld_stretchsettings.png

Next, we need to modify the Player.gd script to change the input method. We’ll remove the key inputs and make the player move towards a “target” that’s set by the touch (or click) event.

Here is the full script for the player, with comments noting what we’ve changed:

GDScript

C#

  1. extends Area2D
  2. signal hit
  3. export var speed = 400
  4. var screen_size
  5. # Add this variable to hold the clicked position.
  6. var target = Vector2()
  7. func _ready():
  8. hide()
  9. screen_size = get_viewport_rect().size
  10. func start(pos):
  11. position = pos
  12. # Initial target is the start position.
  13. target = pos
  14. show()
  15. $CollisionShape2D.disabled = false
  16. # Change the target whenever a touch event happens.
  17. func _input(event):
  18. if event is InputEventScreenTouch and event.pressed:
  19. target = event.position
  20. func _process(delta):
  21. var velocity = Vector2()
  22. # Move towards the target and stop when close.
  23. if position.distance_to(target) > 10:
  24. velocity = target - position
  25. # Remove keyboard controls.
  26. # if Input.is_action_pressed("ui_right"):
  27. # velocity.x += 1
  28. # if Input.is_action_pressed("ui_left"):
  29. # velocity.x -= 1
  30. # if Input.is_action_pressed("ui_down"):
  31. # velocity.y += 1
  32. # if Input.is_action_pressed("ui_up"):
  33. # velocity.y -= 1
  34. if velocity.length() > 0:
  35. velocity = velocity.normalized() * speed
  36. $AnimatedSprite.play()
  37. else:
  38. $AnimatedSprite.stop()
  39. position += velocity * delta
  40. # We still need to clamp the player's position here because on devices that don't
  41. # match your game's aspect ratio, Godot will try to maintain it as much as possible
  42. # by creating black borders, if necessary.
  43. # Without clamp(), the player would be able to move under those borders.
  44. position.x = clamp(position.x, 0, screen_size.x)
  45. position.y = clamp(position.y, 0, screen_size.y)
  46. if velocity.x != 0:
  47. $AnimatedSprite.animation = "walk"
  48. $AnimatedSprite.flip_v = false
  49. $AnimatedSprite.flip_h = velocity.x < 0
  50. elif velocity.y != 0:
  51. $AnimatedSprite.animation = "up"
  52. $AnimatedSprite.flip_v = velocity.y > 0
  53. func _on_Player_body_entered( body ):
  54. hide()
  55. emit_signal("hit")
  56. $CollisionShape2D.set_deferred("disabled", true)
  1. using Godot;
  2. using System;
  3. public class Player : Area2D
  4. {
  5. [Signal]
  6. public delegate void Hit();
  7. [Export]
  8. public int Speed = 400;
  9. private Vector2 _screenSize;
  10. // Add this variable to hold the clicked position.
  11. private Vector2 _target;
  12. public override void _Ready()
  13. {
  14. Hide();
  15. _screenSize = GetViewport().Size;
  16. }
  17. public void Start(Vector2 pos)
  18. {
  19. Position = pos;
  20. // Initial target us the start position.
  21. _target = pos;
  22. Show();
  23. GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
  24. }
  25. // Change the target whenever a touch event happens.
  26. public override void _Input(InputEvent @event)
  27. {
  28. if (@event is InputEventScreenTouch eventMouseButton && eventMouseButton.Pressed)
  29. {
  30. _target = (@event as InputEventScreenTouch).Position;
  31. }
  32. }
  33. public override void _Process(float delta)
  34. {
  35. var velocity = new Vector2();
  36. // Move towards the target and stop when close.
  37. if (Position.DistanceTo(_target) > 10)
  38. {
  39. velocity = _target - Position;
  40. }
  41. // Remove keyboard controls.
  42. //if (Input.IsActionPressed("ui_right"))
  43. //{
  44. // velocity.x += 1;
  45. //}
  46. //if (Input.IsActionPressed("ui_left"))
  47. //{
  48. // velocity.x -= 1;
  49. //}
  50. //if (Input.IsActionPressed("ui_down"))
  51. //{
  52. // velocity.y += 1;
  53. //}
  54. //if (Input.IsActionPressed("ui_up"))
  55. //{
  56. // velocity.y -= 1;
  57. //}
  58. var animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
  59. if (velocity.Length() > 0)
  60. {
  61. velocity = velocity.Normalized() * Speed;
  62. animatedSprite.Play();
  63. }
  64. else
  65. {
  66. animatedSprite.Stop();
  67. }
  68. Position += velocity * delta;
  69. // We still need to clamp the player's position here because on devices that don't
  70. // match your game's aspect ratio, Godot will try to maintain it as much as possible
  71. // by creating black borders, if necessary.
  72. // Without clamp(), the player would be able to move under those borders.
  73. Position = new Vector2(
  74. x: Mathf.Clamp(Position.x, 0, _screenSize.x),
  75. y: Mathf.Clamp(Position.y, 0, _screenSize.y)
  76. );
  77. if (velocity.x != 0)
  78. {
  79. animatedSprite.Animation = "walk";
  80. animatedSprite.FlipV = false;
  81. animatedSprite.FlipH = velocity.x < 0;
  82. }
  83. else if(velocity.y != 0)
  84. {
  85. animatedSprite.Animation = "up";
  86. animatedSprite.FlipV = velocity.y > 0;
  87. }
  88. }
  89. public void OnPlayerBodyEntered(PhysicsBody2D body)
  90. {
  91. Hide(); // Player disappears after being hit.
  92. EmitSignal("Hit");
  93. GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
  94. }
  95. }

Setting a main scene

The main scene is the one that your game will start in. In Project -> Project Settings -> Application -> Run, set Main Scene to “Main.tscn” by clicking the folder icon and selecting it.

Export templates

In order to export, you need to download the export templates from the http://godotengine.org/download. These templates are optimized versions of the engine without the editor pre-compiled for each platform . You can also download them in Godot by clicking on Editor -> Manage Export Templates:

../../_images/export_template_menu.png

In the window that appears, you can click “Download” to get the template version that matches your version of Godot.

../../_images/export_template_manager.png

Note

If you upgrade Godot, you must download templates that match the new version or your exported projects may not work correctly.

Export presets

Next, you can configure the export settings by clicking on Project -> Export.

Create a new export preset by clicking “Add…” and selecting a platform. You can make as many presets as you like with different settings.

../../_images/export_presets_window.png

At the bottom of the window are two buttons. “Export PCK/ZIP” only creates a packed version of your project’s data. This doesn’t include an executable so the project can’t be run on its own.

The second button, “Export Project”, creates a complete executable version of your game, such as an .apk for Android or an .exe for Windows.

In the “Resources” and “Features” tabs, you can customize how the game is exported for each platform. We can leave those settings alone for now.

Exporting by platform

In this section, we’ll walk through the process for each platform, including any additional software or requirements you’ll need.

PC (Linux/macOS/Windows)

Exporting PC platforms works the same across the three supported operating systems. Open the export window and click “Add..” to create the preset(s) you want to make. Then click “Export Project” and choose a name and destination folder. Choose a location outside of your project folder.

Click “Save” and the engine will build the export files.

Note

When exporting for macOS, if you export on a macOS computer, you’ll end up with a .dmg file, while using Linux or Windows produces a .zip. In either case, the compressed file contains a macOS .app that you can double-click and run.

Note

On Windows, if you want your exported executable to have a different icon than the default one, you need to change it manually. See: Changing application icon for Windows.

Android

Tip

Mobile devices come with a wide variety of capabilities. In most cases, Godot’s default settings will work, but mobile development is sometimes more art than science, and you may need to do some experimenting and searching for help in order to get everything working.

Before you can export your project for Android, you must download the following software:

When you run Android Studio for the first time, click on Configure -> SDK Manager and install “Android SDK Platform Tools”. This installs the adb command-line tool that Godot uses to communicate with your device.

Next, create a debug keystore by running the following command on your system’s command line:

  1. keytool -keyalg RSA -genkeypair -alias androiddebugkey -keypass android -keystore debug.keystore -storepass android -dname "CN=Android Debug,O=Android,C=US" -validity 9999

Click on Editor -> Editor Settings in Godot and select the Export/Android section. Here, you need to set the paths to the Android SDK applications on your system and the location of the keystore you just created.

../../_images/export_editor_android_settings.png

Now you’re ready to export. Click on Project -> Export and add a preset for Android (see above). Select the Android Presets and under Options go to Screen and set Orientation to “Portrait”.

Click the “Export Project” button and Godot will build an APK you can download on your device. To do this on the command line, use the following:

  1. adb install dodge.apk

Note

Your device may need to be in developer mode. Consult your device’s documentation for details.

If your system supports it, connecting a compatible Android device will cause a “One-click Deploy” button to appear in Godot’s playtest button area:

../../_images/export_android_oneclick.png

Clicking this button builds the APK and copies it onto your device in one step.

iOS

Note

In order to build your game for iOS, you must have a computer running macOS with Xcode installed.

Before exporting, there are some settings that you must complete for the project to export successfully. First, the “App Store Team Id”, which you can find by logging in to your Apple developer account and looking in the “Membership” section.

You must also provide icons and splash screen images as shown below:

../../_images/export_ios_settings.png

Click “Export Project” and select a destination folder.

Once you have successfully exported the project, you’ll find the following folders and files have been created in your selected location:

../../_images/export_xcode_project_folders.png

You can now open the project in Xcode and build the project for iOS. Xcode build procedure is beyond the scope of this tutorial. See https://help.apple.com/xcode/mac/current/#/devc8c2a6be1 for more information.

HTML5 (web)

Click “Export Project” on the HTML5 preset. We don’t need to change any of the default settings.

When the export is complete, you’ll have a folder containing the following files:

../../_images/export_web_files.png

Viewing the .html file in your browser lets you play the game. However, you can’t open the file directly, it needs to be served by a web server. If you don’t have one set up on your computer, you can search online to find suggestions for your specific OS.

Point your browser at the URL where you’ve placed the html file. You may have to wait a few moments while the game loads before you see the start screen.

../../_images/export_web_example.png

The console window beneath the game tells you if anything goes wrong. You can disable it by setting “Export With Debug” off when you export the project.

../../_images/export_web_export_with_debug_disabled.png

Note

While WASM is supported in all major browsers, it is still an emerging technology and you may find some things that don’t work. Make sure you have updated your browser to the most recent version, and report any bugs you find at the Godot GitHub repository.