Work in progress

The content of this page was not yet updated for Godot 4.1 and may be outdated. If you know how to improve this page or you can confirm that it’s up to date, feel free to open a pull request.

Plugins for iOS

Godot provides StoreKit, GameCenter, iCloud services and other plugins. They are using same model of asynchronous calls explained below.

ARKit and Camera access are also provided as plugins.

Latest updates, documentation and source code can be found at Godot iOS plugins repository

Accessing plugin singletons

To access plugin functionality, you first need to check that the plugin is exported and available by calling the Engine.has_singleton() function, which returns a registered singleton.

Here’s an example of how to do this in GDScript:

  1. var in_app_store
  2. var game_center
  3. func _ready():
  4. if Engine.has_singleton("InAppStore"):
  5. in_app_store = Engine.get_singleton("InAppStore")
  6. else:
  7. print("iOS IAP plugin is not available on this platform.")
  8. if Engine.has_singleton("GameCenter"):
  9. game_center = Engine.get_singleton("GameCenter")
  10. else:
  11. print("iOS Game Center plugin is not available on this platform.")

Asynchronous methods

When requesting an asynchronous operation, the method will look like this:

  1. Error purchase(Variant params);

The parameter will usually be a Dictionary, with the information necessary to make the request, and the call will have two phases. First, the method will immediately return an Error value. If the Error is not ‘OK’, the call operation is completed, with an error probably caused locally (no internet connection, API incorrectly configured, etc). If the error value is ‘OK’, a response event will be produced and added to the ‘pending events’ queue. Example:

  1. func on_purchase_pressed():
  2. var result = in_app_store.purchase({ "product_id": "my_product" })
  3. if result == OK:
  4. animation.play("busy") # show the "waiting for response" animation
  5. else:
  6. show_error()
  7. # put this on a 1 second timer or something
  8. func check_events():
  9. while in_app_store.get_pending_event_count() > 0:
  10. var event = in_app_store.pop_pending_event()
  11. if event.type == "purchase":
  12. if event.result == "ok":
  13. show_success(event.product_id)
  14. else:
  15. show_error()

Remember that when a call returns OK, the API will always produce an event through the pending_event interface, even if it’s an error, or a network timeout, etc. You should be able to, for example, safely block the interface waiting for a reply from the server. If any of the APIs don’t behave this way it should be treated as a bug.

The pending event interface consists of two methods:

  • get_pending_event_count() Returns the number of pending events on the queue.

  • Variant pop_pending_event() Pops the first event from the queue and returns it.

Store Kit

Implemented in Godot iOS InAppStore plugin.

The Store Kit API is accessible through the InAppStore singleton. It is initialized automatically.

The following methods are available and documented below:

  1. Error purchase(Variant params)
  2. Error request_product_info(Variant params)
  3. Error restore_purchases()
  4. void set_auto_finish_transaction(bool enable)
  5. void finish_transaction(String product_id)
  6. and the pending events interface:
  7. ::
  8. int get_pending_event_count()
  9. Variant pop_pending_event()

purchase

Purchases a product ID through the Store Kit API. You have to call finish_transaction(product_id) once you receive a successful response or call set_auto_finish_transaction(true) prior to calling purchase(). These two methods ensure the transaction is completed.

Parameters

Takes a dictionary as a parameter, with one field, product_id, a string with your product ID. Example:

  1. var result = in_app_store.purchase({ "product_id": "my_product" })

Response event

The response event will be a dictionary with the following fields:

On error:

  1. {
  2. "type": "purchase",
  3. "result": "error",
  4. "product_id": "the product ID requested",
  5. }

On success:

  1. {
  2. "type": "purchase",
  3. "result": "ok",
  4. "product_id": "the product ID requested",
  5. }

request_product_info

Requests the product info on a list of product IDs.

Parameters

Takes a dictionary as a parameter, with a single product_ids key to which a string array of product IDs is assigned. Example:

  1. var result = in_app_store.request_product_info({ "product_ids": ["my_product1", "my_product2"] })

Response event

The response event will be a dictionary with the following fields:

  1. {
  2. "type": "product_info",
  3. "result": "ok",
  4. "invalid_ids": [ list of requested IDs that were invalid ],
  5. "ids": [ list of IDs that were valid ],
  6. "titles": [ list of valid product titles (corresponds with list of valid IDs) ],
  7. "descriptions": [ list of valid product descriptions ],
  8. "prices": [ list of valid product prices ],
  9. "localized_prices": [ list of valid product localized prices ],
  10. }

restore_purchases

Restores previously made purchases on user’s account. This will create response events for each previously purchased product ID.

Response event

The response events will be dictionaries with the following fields:

  1. {
  2. "type": "restore",
  3. "result": "ok",
  4. "product_id": "product ID of restored purchase",
  5. }

set_auto_finish_transaction

If set to true, once a purchase is successful, your purchase will be finalized automatically. Call this method prior to calling purchase().

Parameters

Takes a boolean as a parameter which specifies if purchases should be automatically finalized. Example:

  1. in_app_store.set_auto_finish_transaction(true)

finish_transaction

If you don’t want transactions to be automatically finalized, call this method after you receive a successful purchase response.

Parameters

Takes a string product_id as an argument. product_id specifies what product to finalize the purchase on. Example:

  1. in_app_store.finish_transaction("my_product1")

Game Center

Implemented in Godot iOS GameCenter plugin.

The Game Center API is available through the GameCenter singleton. It has the following methods:

  1. Error authenticate()
  2. bool is_authenticated()
  3. Error post_score(Variant score)
  4. Error award_achievement(Variant params)
  5. void reset_achievements()
  6. void request_achievements()
  7. void request_achievement_descriptions()
  8. Error show_game_center(Variant params)
  9. Error request_identity_verification_signature()

and the pending events interface:

  1. int get_pending_event_count()
  2. Variant pop_pending_event()

authenticate

Authenticates a user in Game Center.

Response event

The response event will be a dictionary with the following fields:

On error:

  1. {
  2. "type": "authentication",
  3. "result": "error",
  4. "error_code": the value from NSError::code,
  5. "error_description": the value from NSError::localizedDescription,
  6. }

On success:

  1. {
  2. "type": "authentication",
  3. "result": "ok",
  4. "player_id": the value from GKLocalPlayer::playerID,
  5. }

post_score

Posts a score to a Game Center leaderboard.

Parameters

Takes a dictionary as a parameter, with two fields:

  • score a float number

  • category a string with the category name

Example:

  1. var result = game_center.post_score({ "score": 100, "category": "my_leaderboard", })

Response event

The response event will be a dictionary with the following fields:

On error:

  1. {
  2. "type": "post_score",
  3. "result": "error",
  4. "error_code": the value from NSError::code,
  5. "error_description": the value from NSError::localizedDescription,
  6. }

On success:

  1. {
  2. "type": "post_score",
  3. "result": "ok",
  4. }

award_achievement

Modifies the progress of a Game Center achievement.

Parameters

Takes a Dictionary as a parameter, with 3 fields:

  • name (string) the achievement name

  • progress (float) the achievement progress from 0.0 to 100.0 (passed to GKAchievement::percentComplete)

  • show_completion_banner (bool) whether Game Center should display an achievement banner at the top of the screen

Example:

  1. var result = award_achievement({ "name": "hard_mode_completed", "progress": 6.1 })

Response event

The response event will be a dictionary with the following fields:

On error:

  1. {
  2. "type": "award_achievement",
  3. "result": "error",
  4. "error_code": the error code taken from NSError::code,
  5. }

On success:

  1. {
  2. "type": "award_achievement",
  3. "result": "ok",
  4. }

reset_achievements

Clears all Game Center achievements. The function takes no parameters.

Response event

The response event will be a dictionary with the following fields:

On error:

  1. {
  2. "type": "reset_achievements",
  3. "result": "error",
  4. "error_code": the value from NSError::code,
  5. }

On success:

  1. {
  2. "type": "reset_achievements",
  3. "result": "ok",
  4. }

request_achievements

Request all the Game Center achievements the player has made progress on. The function takes no parameters.

Response event

The response event will be a dictionary with the following fields:

On error:

  1. {
  2. "type": "achievements",
  3. "result": "error",
  4. "error_code": the value from NSError::code,
  5. }

On success:

  1. {
  2. "type": "achievements",
  3. "result": "ok",
  4. "names": [ list of the name of each achievement ],
  5. "progress": [ list of the progress made on each achievement ],
  6. }

request_achievement_descriptions

Request the descriptions of all existing Game Center achievements regardless of progress. The function takes no parameters.

Response event

The response event will be a dictionary with the following fields:

On error:

  1. {
  2. "type": "achievement_descriptions",
  3. "result": "error",
  4. "error_code": the value from NSError::code,
  5. }

On success:

  1. {
  2. "type": "achievement_descriptions",
  3. "result": "ok",
  4. "names": [ list of the name of each achievement ],
  5. "titles": [ list of the title of each achievement ],
  6. "unachieved_descriptions": [ list of the description of each achievement when it is unachieved ],
  7. "achieved_descriptions": [ list of the description of each achievement when it is achieved ],
  8. "maximum_points": [ list of the points earned by completing each achievement ],
  9. "hidden": [ list of booleans indicating whether each achievement is initially visible ],
  10. "replayable": [ list of booleans indicating whether each achievement can be earned more than once ],
  11. }

show_game_center

Displays the built in Game Center overlay showing leaderboards, achievements, and challenges.

Parameters

Takes a Dictionary as a parameter, with two fields:

  • view (string) (optional) the name of the view to present. Accepts “default”, “leaderboards”, “achievements”, or “challenges”. Defaults to “default”.

  • leaderboard_name (string) (optional) the name of the leaderboard to present. Only used when “view” is “leaderboards” (or “default” is configured to show leaderboards). If not specified, Game Center will display the aggregate leaderboard.

Examples:

  1. var result = show_game_center({ "view": "leaderboards", "leaderboard_name": "best_time_leaderboard" })
  2. var result = show_game_center({ "view": "achievements" })

Response event

The response event will be a dictionary with the following fields:

On close:

  1. {
  2. "type": "show_game_center",
  3. "result": "ok",
  4. }