Experimental YouTube Playables Godot Wrapper

With this wrapper you can access the YouTube Playables SDK in Godot with provided .gd. The wrapper has a .js SDK connector and a .gd file to help speed up your development. There is also a sample project available that shows how you can use these scripts in your project.

Godot zip Packages can be downloaded from our Playables sample repo.

Usage

  1. Extract GoogleYTGameWrapper-godot.zip in your project folder, where you want these scripts to reside. This zip contains two files:
    • YTGameWrapper.gd: used as a bridge to the YT Playables Javascript SDK allowing a path to call the SDK from in game.
    • YTGameSDK.js: used to connect to the YT Playables SDK. For this example we will place this file in the root of your project. You can change this, but it requires updating your Head Include location.
    • There is also a full project sample version available for download from our Playables sample repo. For this version use the zip named GoogleYTGameWrapper-godot-with-sample.zip.
  2. Add a new Autoload Global for YTGameWrapper.gd script that was unzipped. This can be set under Project -> Project Settings... -> Globals. Select the YTGameWrapper.gd file in the Path input box and add YTGameWrapper in for the Node Name input box. Finally, select + Add.
    • This will make the script globally available in your code, allowing you to call YTGameWrapper as needed.
  3. Verify your Godot project Platform is set to export for Web. This setting is found in the Export tab, select Project then Export....
  4. In the Export panel add a new Preset by selecting Add... then select Web. Once created, add this code into the Head Include setting box.
<script src="https://www.youtube.com/game_api/v1">// Load YT Game API code</script>
<script src="YTGameSDK.js"></script>
  1. Export your web project in the Export panel by selecting Export Project... and selecting the build location.
  2. Once your project is built, navigate to the build folder and copy the YTGameSDK.js file into this folder and make sure to include it when you submit your build.
  3. Verify you are following technical requirements for your project and be sure to use the Playables test suite.

Examples

This section has some examples of how to use the .gd wrapper, it is not the full list of available APIs. For the full list of available APIs, refer to the YouTube Playables SDK. This also assumes that your project is setup as described in the Usage section.

sendScore

This example shows an implementation of sendScore(int points) in .gd:

var current_game_score: int = 0

...

func add_score(scoreAdded: int):
    current_game_score += scoreAdded
    YTGameWrapper.send_score(current_game_score)

onPause

This is an example of how a game can listen to Pause events coming from YT Playables, to pause its engine when needed:

func _ready() -> void:
    # hook up on game paused
    YTGameWrapper.game_paused.connect(self._on_game_paused)
    # hook up on game resumed and game save data received events
    YTGameWrapper.game_resumed.connect(self._on_game_resumed)

func _on_game_paused():
    # Pause your game logic, audio, etc.
    get_tree().paused = true
    
func _on_game_resumed():
    # Resume your game logic, audio, etc.
    get_tree().paused = false

saveData

This is an example of how to use saveData, sending it to YT Playables SDK:

var current_game_score: int = 0
var time_elapsed: float = 0.0

...

func save_game_data():
    YTGameWrapper.save_data('{"BestScore": "' + str(current_game_score) + '","BestTime": "' +str(time_elapsed)+ '"}')

loadData

This is an example of how to use loadData, sending it to YT Playables SDK:

func _ready() -> void:
    # hook up game load data received event
    YTGameWrapper.load_data_received.connect(self._on_load_data)
    # Request to load data when the game starts
    YTGameWrapper.load_data()

func _on_load_data(save_data_string: String):
    if not save_data_string.is_empty():
        # Add logic to parse the save_data_string and restore game state
        print("Loaded data: ", save_data_string)

requestInterstitialAd

This is an example of how to use requestInterstitialAd, indicating it is a good time to show an interstitial Ad, if available. For the best results, make this call during a break in gameplay, for example, at the end of a level.

func set_game_over(didWin: bool):
    YTGameWrapper.request_interstitial_ad()

    # logic for game over

How to break up assets in your Godot project (Lazy Loading)

One of the main problems developers have highlighted when using Godot is staying under the individual file size requirements and the total bundle size requirements.

Lazy loading of assets is a great optimization you can make for your project as you can load assets, levels, and data as they are needed. Our certification team may waive the overall file size restrictions if this is properly done, as your full game won't be loaded up front, but as a user navigates your product.

To help with proper loading, Godot offers Exporting packs, patches, and mods (English link). We suggest generating .pck files that are loaded as needed.

See the full YT Playables API reference.