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:
Make sure to read the introduction page on setting up your project.
Game Settings
The game module contains a settings object, that can be accessed like this:
disableChat- iftrue, the game should disable chat (if applicable). Read more about chat on multiplayer requirements page. Locally you can use?disableChat=trueto force this to true.muteAudio- please disable the game audio if this is true. Locally you can use?muteAudio=trueto force this to true.
You can also register a listener which will be called each time the game settings change:
disableChat- iftrue, the game should disable chat (if applicable). Read more about chat on multiplayer requirements page. Locally you can use?disableChat=trueto force this to true.muteAudio- please disable the game audio if this is true. Locally you can use?muteAudio=trueto 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:
disableChat- iftrue, the game should disable chat (if applicable). Read more about chat on multiplayer requirements page.
The settings object contains:
disableChat- iftrue, the game should disable the chat. Read more about chat on multiplayer requirements page.
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:
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.
These calls are not supported for Unity, as loading is expected to be done through the Unity loader before the game starts.
🟥 Not supported
These calls are not required for Godot, as loading is done via the Godot export.
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.
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.
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
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.
🟥 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
inviteParamsfield, 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
Invite link
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);
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:
inviteParams isnull 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);
You can retrieve parameters passed through the invite link with GetInviteLinkParameter.
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.
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");
}
});
};
You can copy the invite link to clipboard like this:
Retrieve parameters passed through the invite link:
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).
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).
// 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).
🟥 Not supported
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).