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.
Demo game
We also created a demo game that showcases various multiplayer features from our SDK. You can download the source code from here.
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 room information 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, ... .
The room contains the following data:
roomId- unique identifier for this room. If your game supports multiple regions, please ensure the roomId you report is unique across the regions, for example by joining the actual room id with the region id.isJoinable- allows the current player to invite other players or be joined by other playersinviteParams- these will be passed to other players who accept an invitation, or join this player. Read more about theinviteParamsin the room join listener section.
// the player joins a room
window.CrazyGames.SDK.game.updateRoom({ roomId: "123eu" });
// the room is now open, the current player can invite other players or be joined by other players
// the inviteParams are just an example, you may have other parameters required to join a specific room
window.CrazyGames.SDK.game.updateRoom({ isJoinable: true, inviteParams: { roomName: "123", region: "eu" }});
// the room is full and no more players can joinon
window.CrazyGames.SDK.game.updateRoom({ isJoinable: false});
// the player left the room
window.CrazyGames.SDK.game.leftRoom();
// you can always mix more parameters, for example if the player joins a room and the room is already joinable
window.CrazyGames.SDK.game.updateRoom({ roomId: "123eu", isJoinable: true, inviteParams: { roomName: "123", region: "eu" }});
// the player joins a room
CrazySDK.Game.UpdateRoom(new UpdateRoomInput() { RoomId = "123eu" });
// the room is now open, the current player can invite other players or be joined by other players
// the inviteParams are just an example, you may have other parameters required to join a specific room
CrazySDK.Game.UpdateRoom(
new UpdateRoomInput()
{
IsJoinable = true,
InviteParams = new() { { "roomName", "123" }, { "region", "eu" } },
}
);
// the room is full and no more players can joinon
CrazySDK.Game.UpdateRoom(new UpdateRoomInput() { IsJoinable = false });
// the player left the room
CrazySDK.Game.LeftRoom();
// you can always mix more parameters, for example if the player joins a room and the room is already joinable
CrazySDK.Game.UpdateRoom(
new UpdateRoomInput()
{
RoomId = "123eu",
IsJoinable = true,
InviteParams = new() { { "roomName", "1234" }, { "region", "eu" } },
}
);
🟥 Not supported
🟥 Not supported
🟥 Not supported
🟥 Not supported
Room join listener
When the user tries to join their friends via an invite notification, invite link or friends drawer, there are 2 possible scenarios:
- The user is already in game. In this case the room join listener will be triggered.
- The user is redirected to the game page, and the game has to load. Use the
inviteParamsin this case.
// don't forget to check window.CrazyGames.SDK.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
function listener(inviteParams){
// send the user to the multiplayer room
}
// to add a listener
window.CrazyGames.SDK.game.addJoinRoomListener(listener);
// to remove a listener
window.CrazyGames.SDK.game.removeJoinRoomListener(listener);
private void JoinRoomListener(Dictionary<string, string> inviteParams)
{
// send the user to the multiplayer room
}
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);
}
🟥 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({
roomName: 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("roomName");
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("roomName", "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({
roomName: 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 roomName = window.ConstructCrazySDK.game.getInviteParam("roomName");
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 roomName = window.ConstructCrazySDK.game.getInviteParam("roomName");
console.log("Room id:", roomName);
if (roomName) {
runtime.globalVars.roomName = roomName;
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({
roomName: 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("roomName", "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({
roomName: 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).