Skip to content

Godot Integration

This guide will walk you through integrating the GameLoop Funtico SDK into your Godot project. The SDK enables your game to interact with the Funtico platform for features like user authentication, leaderboards, and score saving.

Since Godot games exported for the web run in a browser environment, we’ll use JavaScript as a bridge between your GDScript code and the SDK. Don’t worry if you’re not familiar with JavaScript – we’ll provide all the code snippets you need.

Before your game can use the Funtico SDK, you need to include the SDK library in your HTML export. This is done by adding a script tag that loads the SDK from our Content Delivery Network (CDN).

You have two options for adding this script:

Section titled “Option A: Through Export Settings (Recommended)”
  1. Open your project in Godot
  2. Go to Project → Export
  3. Select your HTML5 export preset
  4. Find the HTML → Head Include field
  5. Add the following script tag:
<script src="https://funtico-frontend-js-sdk.pages.dev/funtico-sdk.min.js"></script>

Alternatively, you can add the script tag directly to your export_presets.cfg file in the project root.

Now we need to initialize the SDK when your game starts. This creates a connection between your game and the Funtico platform.

# Add this to your main game scene or a dedicated SDK manager script
func _ready():
# Initialize the Funtico SDK
var js_code = """
window.sdk = new FunticoSDK({
authClientId: 'your-client-id', // Replace with your actual client ID
env: 'sandbox' // Use 'production' for live games
});
"""
JavaScriptBridge.eval(js_code)

Before you can use SDK functions like getting user info or saving scores, you need to authenticate the user. This redirects the user to Funtico’s login page, and after successful authentication, they are redirected back to your game.

# Call this when user clicks a "Login" or "Play" button
func start_authentication():
var js_code = """
// Get the current page URL to redirect back to after login
var callbackUrl = window.location.origin + window.location.pathname;
// Start the authentication flow
window.sdk.signInWithFuntico(callbackUrl)
.catch(function(error) {
console.error('Authentication failed:', error);
});
"""
JavaScriptBridge.eval(js_code)
# User will be redirected to Funtico login page
# After successful login, they'll return to your game

The Funtico SDK uses asynchronous functions (also called Promises in JavaScript). This means when you call an SDK function, it doesn’t return a result immediately. Instead, it returns a Promise that will either:

  • Resolve with the requested data (success)
  • Reject with an error message (failure)

Implementation Differences Between Godot Versions

Section titled “Implementation Differences Between Godot Versions”

Godot 3.x and 4.x handle JavaScript differently:

  • Godot 4.x uses JavaScriptBridge with create_callback() and get_interface()
  • Godot 3.x uses JavaScript class and requires a different approach for callbacks
# Store callbacks as member variables to prevent garbage collection
var _onsuccess_callback
var _onerror_callback
func _ready():
# Initialize SDK first (see Step 2)
# ...
# Create callbacks that JavaScript can call
_onsuccess_callback = JavaScriptBridge.create_callback(_on_user_info_success)
_onerror_callback = JavaScriptBridge.create_callback(_on_user_info_error)
# Get the SDK interface and call getUserInfo
var sdk = JavaScriptBridge.get_interface("sdk")
sdk.getUserInfo().then(_onsuccess_callback).catch(_onerror_callback)
func _on_user_info_success(data):
# The SDK returns an array with user data as the first element
var user_info = data[0]
var username = user_info.username
var user_id = str(user_info.user_id)
print("User logged in: ", username)
print("User ID: ", user_id)
# Continue with your game logic
# For example: show main menu, load user progress, etc.
func _on_user_info_error(error):
print("Failed to get user info: ", error[0])
# Handle error - maybe show login screen or retry

Here are examples of commonly used SDK functions:

var _onsuccess_callback
var _onerror_callback
func submit_score(score: int):
_onsuccess_callback = JavaScriptBridge.create_callback(_on_score_submitted)
_onerror_callback = JavaScriptBridge.create_callback(_on_submit_error)
var sdk = JavaScriptBridge.get_interface("sdk")
var promise = sdk.saveScore(score)
promise.then(_onsuccess_callback).catch(_onerror_callback)
func _on_score_submitted(result):
print("Score submitted successfully!")
func _on_submit_error(error):
print("Failed to submit score: ", error[0])
IssueSolution
”sdk is not defined”Ensure the SDK script is included in your HTML export and initialization code runs first
Callbacks never fireIn Godot 4.x, make sure callbacks are stored as member variables, not local variables
”Cannot read property of null”The SDK hasn’t been initialized yet. Check your initialization code
Functions work in one browser but not anotherClear browser cache and ensure you’re using the latest SDK version