Quick tutorial on the `settings` namespace

@Momoro asked me a question in a different thread:

Are you planning on making a “save / load file” extension for Arcade? I really need this :smiley:

@mmoskal reminded me of the new settings namespace:

We actually now have ‘settings’ namespace. You can store up to 16k of data there. This works on all hardware and in the simulator.

I totally forgot about the new settings namespace, Michał. Thanks for the reminder to get in there and check it out! I noticed there wasn’t any documentation on it (or, more accurately, I didn’t find any in the MakeCode Arcade documentation), so I thought I’d share a little bit of information on it.

@Momoro, you can access those functions in JavaScript. (I don’t believe there is a Blocks interface for that namespace quite yet.) The settings namespace essentially gives you a way to access something known as a dictionary, where you store and retrieve information using a key string. Here are some of the more important functions in that namespace:

settings.readString(key)
settings.writeString(key, value)
settings.readNumber(key)
settings.writeNumber(key, value)

So, for example, let’s say you want to ask players for their names and ages. You could do something like this:

playerName = game.askForString('What is your name?')
playerAge = game.askForNumber('How old are you?')
settings.writeString('Name', playerName)
settings.writeNumber('Age', playerAge)

Then, somewhere else in your game, you could read that information:

playerName = settings.readString('Name')
playerAge = settings.readNumber('Age')

Here’s a more complete example, then, with these ideas in mind:

let playerName: string = null
let playerAge: number = 0

writePlayerInfo()
readPlayerInfo()
showPlayerInfo()

function writePlayerInfo() {
    playerName = game.askForString('What is your name?')
    playerAge = game.askForNumber('How old are you?')
    settings.writeString('Name', playerName)
    settings.writeNumber('Age', playerAge)
}

function readPlayerInfo() {
    playerName = settings.readString('Name')
    playerAge = settings.readNumber('Age')
}

function showPlayerInfo() {
    game.splash('Hello, ' + playerName + ', age ' + playerAge)
}

Run the game once as-is, so that the game can store the appropriate settings. Then, comment-out or delete the line that calls the writePlayerInfo() function, and re-run the game. Notice that the game does not prompt the player for information, but the game still remembers the information from the previous run.

There are additional functions in that namespace to store things like buffers and arrays. Use the editor’s built-in IntelliSense to explore.

If you have any questions, feel free to ask!

2 Likes

Sorry for not responding for so long, I am so happy with the settings feature in MakeCode Arcade :slight_smile:

1 Like

Thank you for discovering it, i’ve made an extension to allow manage settings with blocks. I’m posting it in few minutes

1 Like

Nice! (I’ve been busy, may not post much…)

Thanks for a great writeup, @AlexK and for the extension, @EnteroPositivo!

Would anyone of you de facto experts have an opinion on why the settings namespace exhibits strange behaviour and not working/not writing the storage files on the first launches on a Raspberry Pi?:

Made some further discoveries and hoping that it might be looked at some time:

Keep up all the great work!

Finally fixed for Raspberry Pis by GitHub’s @nopid!

1 Like

I had mentioned a few days ago that I’d update this thread, now that the settings namespace is supported in Blocks.

To add the necessary blocks, install the settings-blocks extension in your project.

The extension adds a new drawer to the toolbox, called settings. In this post, I’ll focus on the four blocks at the top of the drawer.

As in my original post, let’s say you want to ask players for their names and ages. You could do something like this:

Then, somewhere else in your game, you could read that information:

Here’s a more complete example, then, with these ideas in mind:

You can play with the code here:

Run the game once so that the game can store the appropriate settings. Then, run it again, and notice that the game does not prompt you for your name. The information is saved in storage that is associated with the game, and so the information is available even after the game ends.

As with JavaScript, the settings drawer has additional functions to store other things, like arrays.

Have fun!

1 Like