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:

var game_data: i32 = 1337;// Store the gamedata as little-endian bytes.var bits = game_data as u32;var buffer: [4]u8 = [    ((bits >> 24) & 0xFF) as u8,    ((bits >> 16) & 0xFF) as u8,    ((bits >> 8) & 0xFF) as u8,    (bits & 0xFF) as u8,];// Write the bytes to disk.diskw(buffer, |buffer|);

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:

var game_data: i32;// Read little-endian bytes from disk.var buffer: [4]u8 = [0, 0, 0, 0];diskr(buffer, |buffer|);var bits = (buffer[0] as u32 << 24) | (buffer[1] as u32 << 16)    | (buffer[2] as u32 << 8) | (buffer[3] as u32);game_data = bits as i32;