Skip to main content

Saving Data

WASM-4 supports saving up to 1024 raw bytes of data to persist between sessions.

For the web runtime, the disk is saved into the browser's localStorage and tied to the domain. The localStorage key is chosen when bundling to be unique and not conflict with other games served from the same domain.

For the native runtime, the disk is saved as a file in the same directory as the game.

In the case that a game is updated and its disk layout was changed, it's up to the developer to handle it. A simple method would be to prefix the disk data with a version number and when it doesn't match, either reset the current disk or migrate it to the new format.

Writing Data to Disk#

To save, use diskw(). It takes a source data pointer along with a byte length.

For example, to write a 32 bit integer with the value 1337 to disk:

// First we need to store the value somewhere in memory to get a pointerconst ptr = memory.data(sizeof<i32>());store<i32>(ptr, 1337);
w4.diskw(ptr, sizeof<i32>());

Reading Data from Disk#

Reading is similar, using diskr(). It takes a destination pointer along with a byte length.

For example, to read a 32 bit integer from disk:

const ptr = memory.data(sizeof<i32>());w4.diskr(ptr, sizeof<i32>());
const gameData = load<i32>(ptr);