Microsoft MakeCode

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!

1 Like

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

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