Storing Persistent Data

It's possible to store a small amount of data on the user's device that can be retrieved between different views of the same code. This functionality is similar to that of cookies on the web and can be used in lots of ways, for instance:

  • Remembering the user's name so they don't have to re-type it every time they zap your code
  • Keeping track of the user's highest score in the game between plays
  • Remembering which areas of an experience a user has unlocked

The stored data is specific to each zapcode - it's not possible to retrieve data stored in one zapcode from a different zapcode.

Every time the Preview button in ZapWorks Studio is used it provides a new zapcode for testing with. This means that data stored during a preview of a zap will not be accessible the next time the Preview button is pressed.

Storing and Retrieving

Data can be stored using Z.device like this:"username", "Janet");

The first argument is the "key". Use the same key to retrieve the data in the future:

Z.device.retrieve("username", function(data) {
    console.log(data);  // Outputs Janet

The retrieve function takes two parameters, the first is the "key" of the data to retrieve, and the second is a function that will be called with the retrieved data as its argument.

If retrieve is called but there is no previously stored data for the supplied key then undefined will be passed as the argument to the function:

Z.device.retrieve("username", function(data) {
    if (data === undefined) {
        console.log("Hello stranger!");
    } else {
        console.log("Hello, " + data);

Supported Data Types

While the example so far has stored a string it's possible to store more complex arrays and dictionaries of basic JavaScript types (e.g. strings, numbers and nested arrays and dictionaries):"userdata", {
    username: "Janet",
    email: "",
    scores: [1, 5, 12]

// Later on...

Z.device.retrieve("userdata", function(data) {
    if (data !== undefined) {
        console.log("Hello, " + data['username']);

In general any data that can be converted to JSON can be stored and retrieved.

More Information 

zapcode branded_zapcode i