Skip to main content

Game variables

Overview

Game global variables allow you to control game content without updating the game itself. Each variable can be assigned a value in any available language. It will be loaded in the player's preferred language, or by default taken from English, if in another language the value is empty. To add a new variable, go to the control panel in the section Game variables - Add variable.

Some examples of using variables:

  • Dynamic values. Any value in the game that you would like to change at any time. For example, for the sake of experiment or correction of the balance of the game.
  • Download Content. Add levels to the game, change maps, add heroes, equipment, story chapters.
  • Translations. Upload files with translations in different languages of the variable and provide instant corrections and additions to translations. This applies not only to text, but also to images, video and audio.
  • Events. You will be able to control the events and, with the help of languages, include the desired events in the desired translation. For example, New Year in Russian, Christmas in English. As well as local holidays.

Fetch variables

+0-1 Request

By default, project variables are loaded automatically on the first request to the service. However, if they contain a lot of data and you want to control the download yourself, then you can turn off the Load variables on game boot property and call the load method yourself.

// waiting for the variables
await gp.variables.fetch();

Events:

// Successful fetched
gp.variables.on('fetch', () => console.info('variables available'));
// Event on error
gp.variables.on('error:fetch', (error) => console.error(error));

Getting the value of a variable FREE:

// Getting a variable
const dialogs = gp.variables.get("dialogs");

Existence check FREE:

// Checking if a variable exists or contains a value
if (gp.variables.has("newYearEvent")) {
// mygame.enableNewYearEvent();
}

Variable type check FREE:

// Checking the type of a variable
// data - string
// stats - number
// flag - boolean
// doc_html - text in html format
// image - image link
// file - file link
if (gp.variables.type("lastNews") === "doc_html") {
$newsContainer.innerHtml = gp.variables.get("lastNews");
}

Example with getting a file from a variable FREE:

const levelsUrl = gp.variables.get("levels");
// Retrieving the content of a file through the file module
const levels = await gp.files.loadContent(levelsUrl);
// myGame.setLevels(JSON.parse(levels));

Stay in Touch

Other documents of this chapter available Here. To get started, welcome to the Tutorials chapter.

GamePush Community Telegram: @gs_community.

For your suggestions e-mail: [email protected]

We Wish you Success!