ConfigStore and ConfigEntry09 / 12

Reading game configuration

Separate loading JSON from validating and using it.

ConfigStore holds configurations prepared by the engine. To read the board dimensions, it needs the file ID and a decoder. ConfigEntry combines the ID and decoder so they do not have to be passed separately every time.

The BlockPuzzleConfigEntries.Board fragment from schemas.ts:

static readonly Board = new ConfigEntry(
  'json/configs/scenes/game/board.json',
  SchemaDecoder.object({
    width: SchemaDecoder.number({ integer: true, min: 1 }),
    height: SchemaDecoder.number({ integer: true, min: 1 }),
  }),
);

Obtaining a validated value in the service factory in scene.di.ts:

configs.get(
  BlockPuzzleConfigEntries.Board.fileId,
  BlockPuzzleConfigEntries.Board.decoder,
)

Here, configs is ConfigStore obtained through DI. The file must already have been loaded and placed in the store.

01
AssetManagerReads JSON from a file
02
bootstrapConfigBundlesPlaces JSON in ConfigStore
03
ConfigEntryConnects an ID and decoder
04
ConfigStore.getReturns validated dimensions to the service
Loading and validation are separate responsibilities.

assets.preload loads assets, while system.boot.configs.engineStartBundles declares which configuration bundles to prepare for ConfigStore at startup. sceneEnterBundles handles data needed only by a particular scene. Simply naming a bundle configs is not enough.

Settings and catalogs

SettingsEntry describes an individual key within a file, and SettingsService.read(entry) validates it with its own decoder. This reads author-defined settings; it does not save player progress.

DecodedConfigStore is useful when a project already has a type map by ID and a shared decoder resolver. Its getByEntry accepts a ConfigEntry. A direct ConfigStore.get is enough for one file, as used in the game.

Text styles are connected through layouts.textStylesConfigId and validated by TextStylesConfigDecoder. Audio settings have their own decoder. Components receive prepared settings without reading and validating the JSON again.

Sources ↗

Block Puzzle · scene.di.ts↗Configurations passed into service constructors.