Have more questions? Join our Discord

Data files

A data file is JSON that the game logic and the client both read, such as a unit table or a card list. Keep one copy in /src/data/, and both halves read it.

Where data files go

  • Write each data file directly in /src/data/, ending in .json: /src/data/units.json.
  • Name it with lowercase letters, digits and _, starting with a letter or _. The game logic reads it by that name.
  • Each file holds one JSON value.
  • Data files count toward the build limits together with the .rs files: at most 200 files, 256 KiB per file and 2 MiB in total.

write_file, patch_file and rename_file refuse any other .json path under /src/data/ with UNBUILDABLE_RUST_SOURCE.

Read a data file in the game logic

boardweaver_sdk::data! reads /src/data/<name>.json when Boardweaver builds the game:

Form Gives
data!(units) the text of /src/data/units.json, a &'static str
data!(units as T) that file read as T with serde, a &'static T
use boardweaver_sdk::serde::Deserialize;
use std::collections::BTreeMap;

#[derive(Deserialize)]
#[serde(crate = "boardweaver_sdk::serde")]
pub struct Unit {
    pub name: String,
    pub health: u8,
}

pub fn units() -> &'static BTreeMap<String, Unit> {
    boardweaver_sdk::data!(units as BTreeMap<String, Unit>)
}
  • Write data!(name as T) once, in a function such as units() above, and call that function. Each place you write data!(name as T) reads the file the first time a call reaches it and keeps its own copy while the match runs.
  • Make T match the file. When it does not, the first call that reads it fails with GAME_FAILED, and the message names the file and the type. validate_code cannot see this, so play a match that reads every data file before you commit.
  • A change to a data file builds the game logic again on the next commit.
  • Read a value that never changes from a data file. Keep in the state only what changes during play: every viewer receives the state after every decision (The state).

Read a data file in the client

Import the file by a relative path or a /src/ path. The import is the JSON value the file holds:

import units from "./data/units.json";

The client's bundle includes every file it imports.

Text only the client shows, such as card descriptions, can live in any .json file under /src/ outside /src/data/, such as /src/client/text.json. Only files in /src/data/ count toward the build limits and build the game logic again when they change.

Errors

Error Means
bw::data-json A data file is not JSON. It points at the first line and column that does not read.
A Rust compiler error that names src/data/<name>.json data! names a file the game does not have.
UNBUILDABLE_RUST_SOURCE write_file, patch_file or rename_file was given a .json path in /src/data/ that is not directly in it, or that has capital letters or other characters.