Working with Source Code (Type Script)

Accessing game data during runtime is possible by utilizing the generated source code.

This section provides examples using default class names, but it is possible to customize class names during the source code generation process. Additionally, this customization allows to avoid naming collisions with existing code.

Loading Game Data

The following Type Script code creates GameData class and loads your game data into memory.

import { GameData } from './';
import { Formatters } from './formatters';

// Node.js
import { readFileSync } from 'fs';
const gameDataStream = readFileSync(gameDataFilePath);

// Blob or File
const gameDataStream = gameDataFileBlob.arrayBuffer();

// XMLHttpRequest (XHR)
// gameDataRequest.responseType -> "arraybuffer"
const gameDataStream = gameDataRequest.response;

const gameData = new GameData(gameDataStream, {
  format: Formatters.GameDataFormat.Json

The content of gameDataStream could be published game data or original database file (.gdjs or .gdmp).

Accessing Documents

You can access your documents as a list:

let heroes = gameData.heroesAll; // all heroes from all documents -> readonly Hero[]
let heroes = gameData.heroesRoot; // heroes only from root collection -> readonly Hero[]

Or you can access specific documents by their Id or Unique properties:

let hero = gameData.heroesAll.find(heroId); // -> Hero | undefined
let hero = gameData.heroesAll.withOtherKey('Name').find(heroName); // -> Hero | undefined

Settings schemas are accessed by name:

let resetTime = gameData.lootSettings.resetTime; // -> TimeSpan


Formulas inherit the Function type and can be invoked as-is or with invoke method:

var reward = gameData.lootSettings.rewardFormula()  // -> number
// or
var reward = gameData.lootSettings.rewardFormula.invoke()  // -> number

Formula’s parameters are passed as arguments of invoke method.

See also