Game
The game
module contains various functionality related to the game. It can be accessed 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.
disableChat
- iftrue
, the game should disable chat (if applicable). Read more about chat on multiplayer requirements page.
Not supported
The settings object contains:
disableChat
- iftrue
, the game should disable the chat. Read more about chat on multiplayer requirements page.
Not supported
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.
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
Instant multiplayer
The game module contains the isInstantMultiplayer
flag. For multiplayer games, if isInstantMultiplayer
is true, you should instantly create a new room/lobby for the user. Read more about this on the multiplayer requirements page.
Not supported
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
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:
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
.
In the following example, we first create struct args
which holds various key-value pairs, which you can modify to suit your needs. And then we convert args
into a JSON string using json_stringify();
to pass into the function, and store the invite link in the variable invite_link
which you can use as needed.
args = {
roomId: 12345,
gameMode:"pvp",
duration: 60,
};
args_json = json_stringify( args );
invite_link = crazy_invite_link( args_json );
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.
You can retrieve any parameter from the invite link with the following code:
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");
}
});
};
Not supported
Invite button
This feature allows you to display a button in the game footer, that opens a popup containing the invite link. The returned link is similar to the link returned from Invite link.
The invite button should only be used to invite players to a multiplayer gaming session. Please avoid using it for other use cases, such as a Share button for example, as this may lead to delayed submission check or even game rejection. Read more on multiplayer requirements
You can show the invite button like this:
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).
Not supported
Not supported
Not supported