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:

;; game data (1337 stored in little-endian format)(data (i32.const 0x2000) "\39\05\00\00")
;; write 4 bytes from address 0x2000(call $diskw (i32.const 0x2000) (i32.const 4))

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:

;; Read 4 bytes into memory at address 0x2000.(call $diskr (i32.const 0x2000) (i32.const 4))