Skip to content

Game

Danger

These pages describe new requirements & functionality for which we are gathering developer feedback. Please share any concerns or comments through [email protected]. The SDK is subject to change. We will update as soon as possible with the confirmed version and a timeline for implementation.

The game module contains various functionality related to the game. After reading our SDK Introduction page for your engine, access the game module like this:

window.CrazyGames.SDK.game;
CrazySDK.Game;

Make sure to read the introduction page on setting up your project.

window.ConstructCrazySDK.game;
CrazyGames.Game
CrazyGames.Game
CrazySDK.game;

Game Settings

The game module contains a settings object, that can be accessed like this:

window.CrazyGames.SDK.game.settings;
The settings object contains:

  • disableChat - if true, the game should disable chat (if applicable). Read more about chat on multiplayer requirements page. Locally you can use ?disableChat=true to force this to true.
  • muteAudio - please disable the game audio if this is true. Locally you can use ?muteAudio=true to force this to true.

You can also register a listener which will be called each time the game settings change:

function listener(newSettings){
    console.log("Settings updated", newSettings);
}

// to add a listener
window.CrazyGames.SDK.game.addSettingsChangeListener(listener);

// to remove a listener
window.CrazyGames.SDK.game.removeSettingsChangeListener(listener);

CrazySDK.Game.Settings;
The settings object contains:

  • disableChat - if true, the game should disable chat (if applicable). Read more about chat on multiplayer requirements page. Locally you can use ?disableChat=true to force this to true.
  • muteAudio - please disable the game audio if this is true. Locally you can use ?muteAudio=true to force this to true.

Info

If your Unity game is submitted as Unity and not as HTML5, we handle audio muting automatically. In this case there is no need to work with muteAudio at all.

You can also register a listener which will be called each time the game settings change:

Action<GameSettings> settingsListener = (newSettings) =>
{
    Debug.Log("New game settings: " + newSettings.ToString());
};

// to add a listener
CrazySDK.Game.AddSettingsChangeListener(settingsListener);

// to remove a listener
CrazySDK.Game.RemoveSettingsChangeListener(settingsListener);

crazy_game_settings()
The settings object contains:

  • disableChat - if true, the game should disable chat (if applicable). Read more about chat on multiplayer requirements page.
window.ConstructCrazySDK.game.settings;

The settings object contains:

var game_settings = CrazyGames.Game.get_game_settings()
var game_settings = CrazyGames.Game.get_game_settings()
CrazySDK.game.settings;

Gameplay start/stop

We provide functions that enable us to track when and how users are playing your games. These can be used to ensure our site does not perform resource intensive actions while a user is playing.

The gameplay start function has to be called whenever the player starts playing or resumes playing after a break (game start, resume, revive, enter next level, ...). The first event is used to determine your game's initial loading size.

The gameplay stop function has to be called on every game break (entering a menu, ending level, pausing the game, ...) don't forget to call gameplay start when the gameplay resumes. Don't call this event when the user switches focus or leaves the game area (we handle this on our side).

You can call the methods like this:

window.CrazyGames.SDK.game.gameplayStart();
window.CrazyGames.SDK.game.gameplayStop();
CrazySDK.Game.GameplayStart();
CrazySDK.Game.GameplayStop();
crazy_game_gameplay_start()
crazy_game_gameplay_stop()
window.ConstructCrazySDK.game.gameplayStart();
window.ConstructCrazySDK.game.gameplayStop();
CrazyGames.Game.gameplay_start()
CrazyGames.Game.gameplay_stop()
CrazyGames.Game.gameplay_start()
CrazyGames.Game.gameplay_stop()
CrazySDK.game.gameplayStart();
CrazySDK.game.gameplayStop();

Game loading start/stop

We provide functions that enable us to track when and how long the loading of your game takes.

The loading start function has to be called whenever you start loading your game.

The loading stop function has to be called when the loading is complete and eventually the gameplay starts.

window.CrazyGames.SDK.game.loadingStart();
window.CrazyGames.SDK.game.loadingStop();

These calls are not supported for Unity, as loading is expected to be done through the Unity loader before the game starts.

crazy_game_loading_start()
crazy_game_loading_stop()

🟥 Not supported

These calls are not required for Godot, as loading is done via the Godot export.

CrazySDK.game.loadingStart();
CrazySDK.game.loadingStop();

Happy time

The happytime() method can be called on various player achievements (beating a boss, reaching a highscore, etc.). It makes the website celebrate (for example by launching some confetti). There is no need to call this when a level is completed, or an item is obtained.

Info

Use this feature sparingly, the celebration should remain a special moment.

window.CrazyGames.SDK.game.happytime();
CrazySDK.Game.HappyTime();
crazy_game_happytime()
window.ConstructCrazySDK.game.happytime();
CrazyGames.Game.happy_time()
CrazyGames.Game.happy_time()
CrazySDK.game.happytime();

Multiplayer features

This section describes the game specific SDK functionality supporting our Multiplayer Requirements. Refer to that page for additional context on mandatory/optional requirements.

Instant multiplayer

The game module contains the isInstantMultiplayer flag that indicates if you should direct the user into multiplayer mode, in a joinable location directly.

// this field was previously called isInstantJoin which is now deprecated,
// please use isInstantMultiplayer
window.CrazyGames.SDK.game.isInstantMultiplayer;
// this field was previously called IsInstantJoin which is now deprecated,
// please use IsInstantMultiplayer
CrazySDK.Game.IsInstantMultiplayer;
crazy_game_is_instant_multiplayer()
// returns a boolean
const instantMultiplayer = window.CrazyGames.SDK.game.isInstantMultiplayer;

The easiest way is to use it is when the SDK initializes:

const sdkElem = document.createElement("script");
sdkElem.type = "text/javascript";
sdkElem.src = "https://sdk.crazygames.com/Construct3CrazySDK-v3.js";
document.body.appendChild(sdkElem);
sdkElem.onload = function () {
    window.ConstructCrazySDK.init().then(() => {
        const instantMultiplayer =
            window.CrazyGames.SDK.game.isInstantMultiplayer;
        console.log("Instant multiplayer:", instantMultiplayer);

        if (instantMultiplayer) {
            // should instantly create a new room/lobby
        } else {
            runtime.goToLayout("NextLayout");
        }
    });
};

🟥 Not supported

CrazySDK.game.isInstantMultiplayer;

Room data

We define the room as a unique location where the user is playing or waiting in your game. Having these available on platform level allows us to improve the user experience through showing an invite button, platform notifications, status visualization, joining friends, listing other CrazyGames users in your room to make friends connections, ... .

To detect this, a unique room ID needs to be reported when the user enters a multiplayer room. Please don't forget to also call the leftRoom method after the user leaves the room.

Room ID should be unique

If your game supports multiple regions or servers, please ensure the reported room IDs are unique across them. This will prevent falsely displaying users from different regions/servers as being in the same room as you.

If the user is in a joinable location, please also include the isJoinable and inviteParams fields. Read more about joining/inviting on our multiplayer page.

You can find more about the inviteParams in the room join listener section.

window.CrazyGames.SDK.game.updateRoom({ roomId: "123" });
// or
window.CrazyGames.SDK.game.updateRoom({ roomId: "123", isJoinable: true, inviteParams: { roomId: "123", region: "eu" }})


// don't forget to call this when the player leaves the multiplayer room
window.CrazyGames.SDK.game.leftRoom();
CrazySDK.Game.UpdateRoom("your-room-id");

// don't forget to call this when the player leaves the multiplayer room
CrazySDK.Game.LeftRoom();

🟥 Not supported

🟥 Not supported

🟥 Not supported

🟥 Not supported

Room join listener

There are 2 ways to get the invite parameters:

  • Via the room join listener, which will be triggered if the user is already in game.
  • Via the inviteParams field, which will contain the initial invite parameters if the user is redirected to the game from an invite link, or from any other place on CrazyGames.
function listener(inviteParams){
    console.log("Join room listener called", inviteParams);
}

// to add a listener
window.CrazyGames.SDK.game.addJoinRoomListener(listener);

// to remove a listener
window.CrazyGames.SDK.game.removeJoinRoomListener(listener);

// on game start you can access the invite params like this
// inviteParams is `null` if the game wasn't started from an invite link
window.CrazyGames.SDK.game.inviteParams
private void Start()
{
    // don't forget to check CrazySDK.Game.InviteParams on game start
    // if it is not null, your game was already started from an invite link, and you should send the player to the correct room

    CrazySDK.Game.AddJoinRoomListener(JoinRoomListener);
}

private void OnDestroy()
{
    CrazySDK.Game.RemoveJoinRoomListener(JoinRoomListener);
}

private void JoinRoomListener(Dictionary<string, string> inviteParams)
{
    // send user to multiplayer room
}

🟥 Not supported

🟥 Not supported

🟥 Not supported

🟥 Not supported

This feature lets you share the CrazyGames version of your game to the players and invite them to join a multiplayer game. You can call invite link with a map of parameters that correspond to your game or game room. If your game only accepts players from the same region, you can add region as a parameter to the link. That way you can easily handle the scenario when users attempt to join from a different region.

const link = window.CrazyGames.SDK.game.inviteLink({
    roomId: 12345,
    param2: "value",
    param3: "value",
});
console.log("Invite link", link);
The invite link parameters can be retrieved with the help of the getInviteParam method, for example:

// returns either a string or null if the parameter is missing
window.CrazyGames.SDK.game.getInviteParam("roomId");

You can also access all invite parameters like this:

window.CrazyGames.SDK.game.inviteParams
inviteParams is null if the game wasn't started from an invite link.

var parameters = new Dictionary<string, string>();
parameters.Add("roomId", "1234");
var inviteLink = CrazySDK.Game.InviteLink(parameters);
We provide a helper if you want to automatically copy the invite link to the clipboard.

CrazySDK.Game.CopyToClipboard(inviteLink);

You can retrieve parameters passed through the invite link with GetInviteLinkParameter.

var roomId = CrazySDK.Game.GetInviteLinkParameter("roomId");
var inviteParams = { roomId: 12345 };
var link = crazy_game_invite_link(json_stringify(inviteParams));
show_debug_message("Invite link: " + link);

You can retrieve parameters passed through the invite link with crazy_game_get_invite_param.

var roomCode = crazy_game_get_invite_param("roomId");
if (roomCode != undefined) {
    show_debug_message("Joined room: " + string(roomCode));
}
localVars.inviteLink = await window.ConstructCrazySDK.game.inviteLink({
    roomId: 12345,
    param2: "value",
    param3: "value",
});

When a player joins through an invite link, you can get the room ID like this:

// it returns either a string or null if the parameter is missing
const roomId = window.ConstructCrazySDK.game.getInviteParam("roomId");

The easiest way is to insert the invite link room code detection is when the SDK initializes:

const sdkElem = document.createElement("script");
sdkElem.type = "text/javascript";
sdkElem.src = "https://sdk.crazygames.com/Construct3CrazySDK-v3.js";
document.body.appendChild(sdkElem);
sdkElem.onload = function () {
    window.ConstructCrazySDK.init().then(() => {
        const roomId = window.ConstructCrazySDK.game.getInviteParam("roomId");
        console.log("Room id:", roomId);

        if (roomId) {
            runtime.globalVars.roomId = roomId;
            runtime.goToLayout("RoomLayout");
        } else {
            runtime.goToLayout("NextLayout");
        }
    });
};
var invite_url = CrazyGames.Game.request_invite_url({"roomId": "1234"})

You can copy the invite link to clipboard like this:

CrazyGamesBridge.copy_to_clipboard(invite_url)

Retrieve parameters passed through the invite link:

var room_id = CrazyGames.Game.get_invite_link_param("roomId")
var invite_url = CrazyGames.Game.request_invite_url({"roomId": "1234"})

You can copy the invite link to clipboard like this:

CrazyGamesBridge.copy_to_clipboard(invite_url)

Retrieve parameters passed through the invite link:

var room_id = CrazyGames.Game.get_invite_link_param("roomId")
const inviteLink = await CrazySDK.game.inviteLink({ roomId: '123' });

Invite button

Deprecated

This feature is replaced by the Room Data functionality and will be deprecated.

This feature indicates that the user is in a multiplayer room and can be joined.

const link = window.CrazyGames.SDK.game.showInviteButton({
    roomId: 12345,
    param2: "value",
    param3: "value",
});
// the returned link looks the same as the link
// returned by the inviteLink method
console.log("Invite button link", link);

Make sure to hide the invite button when the user can't be joined anymore (e.g. the room is full, the game has started or the lobby was canceled).

window.CrazyGames.SDK.game.hideInviteButton();
var parameters = new Dictionary<string, string>();
parameters.Add("roomId", "1234");
// the returned link looks the same as the link
// returned by the InviteLink method
var inviteLink = CrazySDK.Game.ShowInviteButton(parameters);

Make sure to hide the invite button when the user can't be joined anymore (e.g. the room is full, the game has started or the lobby was canceled).

CrazySDK.Game.HideInviteButton();
// the returned link looks the same as the link
// returned by the crazy_game_invite_link method
var link_params = {
    roomId: 12345,
    param2: "value",
    param3: "value",
};
var link = crazy_game_show_invite_button(json_stringify(link_params));

Make sure to hide the invite button when the user can't be joined anymore (e.g. the room is full, the game has started or the lobby was canceled).

crazy_game_hide_invite_button()

🟥 Not supported

var link = CrazyGames.Game.show_invite_button({"roomId": "1234"})

Hide it when the room is no longer joinable:

CrazyGames.Game.hide_invite_button()
var link = CrazyGames.Game.show_invite_button({"roomId": "1234"})

Hide it when the room is no longer joinable:

CrazyGames.Game.hide_invite_button()
const link = CrazySDK.game.showInviteButton({
    roomId: 12345,
    param2: "value",
    param3: "value",
});
// the returned link looks the same as the link
// returned by the inviteLink method
console.log("Invite button link", link);

Make sure to hide the invite button when the user can't be joined anymore (e.g. the room is full, the game has started or the lobby was canceled).

CrazySDK.game.hideInviteButton();
Ask AI