# Quick tutorial on the \`settings\` namespace

**URL:** <https://forum.makecode.com/t/quick-tutorial-on-the-settings-namespace/1013>\
**Category:** Arcade\
**Tags:** tutorial\
**Created:** [January 10, 2020, 2:23am UTC](https://forum.makecode.com/t/quick-tutorial-on-the-settings-namespace/1013 "2020-01-10T02:23:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [January 10, 2020, 2:23am UTC](https://forum.makecode.com/t/quick-tutorial-on-the-settings-namespace/1013/1 "2020-01-10T02:23:03Z")

</div>

@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 😃

@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](https://arcade.makecode.com/docs)), 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:

```typescript
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:

```typescript
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:

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

```

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

```typescript
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!

---

<div class="post-metadata">

**Author:** ![Momoro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/momoro/32/520_2.png) [@Momoro](https://forum.makecode.com/u/Momoro)\
**Post date:** [January 27, 2020, 3:19am UTC](https://forum.makecode.com/t/quick-tutorial-on-the-settings-namespace/1013/2 "2020-01-27T03:19:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EnteroPositivo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/enteropositivo/32/213_2.png) [@EnteroPositivo](https://forum.makecode.com/u/EnteroPositivo)\
**Post date:** [February 12, 2020, 9:33pm UTC](https://forum.makecode.com/t/quick-tutorial-on-the-settings-namespace/1013/3 "2020-02-12T21:33:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Momoro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/momoro/32/520_2.png) [@Momoro](https://forum.makecode.com/u/Momoro)\
**Post date:** [April 2, 2020, 7:01am UTC](https://forum.makecode.com/t/quick-tutorial-on-the-settings-namespace/1013/4 "2020-04-02T07:01:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [August 9, 2021, 6:54pm UTC](https://forum.makecode.com/t/quick-tutorial-on-the-settings-namespace/1013/5 "2021-08-09T18:54:51Z")

</div>

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?:

> <https://github.com/microsoft/pxt-settings-blocks/issues/2>
>
> Hi,
> 
> There is some peculiar behaviour with the settings namespace/extension wh…en launched the first time (natively - .elf) on a RPi:
> https://github.com/Vegz78/McAirpos/issues/14#issuecomment-888664595
> 
> It's still inconclusive, but preliminary tests suggests that maybe it might have something to do with write permissions inside the .elf.data folders that each game creates on the first launch, but on subsequent loading of the same game it no longer requires 777 permissions to write the storage files(755 is sufficient and the process owner is user \_pi\_).
> 
> Does anyone have enough detailed knowledge on the inner workings of the settings namespace to suggest a reason for or remedy for this behaviour?
> 
> Or maybe someone could test on MS' official MakeCode Arcade image with uf2 files to check if the same happens on the pi in this environment? Appreciate any help and insight!

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [August 18, 2021, 12:41am UTC](https://forum.makecode.com/t/quick-tutorial-on-the-settings-namespace/1013/6 "2021-08-18T00:41:37Z")

</div>

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

> <https://github.com/microsoft/pxt-arcade/issues/3812>
>
> \*\*Describe the bug\*\*
> When using the \`settings.exists("string")\` property from t…he \_settings namespace\_ inside an \`if\` statement, the MakeCode Arcade program terminates with \_\*\*Segmentation fault\*\*\_ when compiled for and run natively as \_.elf\_ file on a Raspberry Pi. 
> 
> More precisely, the program terminates on the \`settings.exists("scores")\` property only if this settings file does not exist yet, but it works if the file already exists.\<br\>\<br\>
> 
> \*\*To Reproduce\*\*
> Steps to reproduce the behavior:
> 1. Go to 'https://arcade.makecode.com/#pub:\_dH1asvKAaUF8?nolocalhost=1&compile=rawELF&hw=rpi'
> 2. Click on 'Download' to download \_.elf\_ file
> 3. \`chmod 755 arcade-blocks-leaderboard\_ns.elf\`
> 4. Execute game with \`./arcade-blocks-leaderboard\_ns.elf\` or using \[McAirpos\](https://github.com/Vegz78/McAirpos)
> 5. After entering the first leader board entry, see error:
> \[1442173983\] GC block 16376b @ 0x20000000
> \[0\] runtime starting, pid=8664...
> \[6\] FB: DRM emulated at 1920x1080 1920x1080 bpp=32
> \[12\] FB: DRM emulated at 1920x1080 1920x1080 bpp=32 7680
> \[16\] init keys
> \[16\] read config: /sd/arcade.cfg 
> \[21\] SCAN\_CODES=/dev/input/event4
> \[25\] BTN\_LEFT=30
> \[28\] BTN\_RIGHT=32
> \[32\] BTN\_UP=17
> \[35\] BTN\_DOWN=31
> \[39\] BTN\_A=29
> \[42\] BTN\_B=42
> \[48\] BTN\_LEFT2=105
> \[51\] BTN\_RIGHT2=106
> \[54\] BTN\_UP2=103
> \[57\] BTN\_DOWN2=108
> \[60\] BTN\_A2=100
> \[63\] BTN\_B2=57
> \[66\] BTN\_RESET=59
> \[69\] BTN\_EXIT=1
> \[73\] BTN\_MENU=60
> \[76\] config done
> \[86\] sx=8 sy=9 ox=320 oy=0 32=1
> \[89\] fbuf=0xb5c29000 sz:8294400
> \[101\] loop
> \[336\] GC block 16376b @ 0x20004000
> \[343\] GC block 16376b @ 0x20008000
> \[1336\] PCM name: 'default'
> \[1342\] PCM state: PREPARED
> \[4358\] GC block 16376b @ 0x2000c000
> \*\*Segmentation fault\*\*\<br\>\<br\>
> 
> \*\*Expected behavior\*\*
> The MakeCode Arcade program should continue execution when encountering the \`settings.exists("string")\` property.
> 
> \*\*Screenshots\*\*
> !\[Screenshot 2021-08-17 at 22 43 34\](https://user-images.githubusercontent.com/49032025/129802585-fc67155f-f79f-493b-ba89-91e555890f00.png)
> 
> \*\*Desktop (please complete the following information):\*\*
> - OS'es run on: \[Raspberry Pi OS Jessi, Buster and Buster, on RPi 3B, RPi 3B and RPi4 B, respectively\]
> - Browsers compiled and downloaded from: \[various Chrome based browsers on MacOS and updated Win10\]
> \<br\>
> 
> \*\*Additional context\*\*
> The problem seems to be with the \_settings namespace\_ itself in pxt-arcade and NOT with the \[settings extension\](https://github.com/microsoft/pxt-settings-blocks)
> 
> It works inside the web simulator in the MakeCode Arcade editor on all browsers and OS'es.
> 
> The above none-working example is based on @riknoll's \[example from the MakeCode forum\](https://forum.makecode.com/t/save-settings-for-everyone/5932/2) which also is NOT working:
> https://arcade.makecode.com/#pub:46238-89332-69341-89890?nolocalhost=1&compile=rawELF&hw=rpi
> !\[Screenshot 2021-08-17 at 22 46 47\](https://user-images.githubusercontent.com/49032025/129802496-1efbd727-bc7e-4da6-83f0-e3b9c21d5b7a.png)
> 
> Here is a solution with a work-around instead of the \`settings.exists("scores")\` property, and which IS working:
> https://arcade.makecode.com/#pub:\_816E0X0brPog?nolocalhost=1&compile=rawELF&hw=rpi
> !\[Screenshot 2021-08-17 at 22 45 10\](https://user-images.githubusercontent.com/49032025/129802551-a40d9515-50af-4886-81b1-165aa1e27604.png)
> 
> It might also be worth mentioning(and maybe somewhat related?), that when using the \_settings namespace\_ functions, one often has to run/execute the games twice, to allow for all the settings files to be written properly in the \_game\\\_file.elf.data\_ folder, which seldom works fully/correctly on the first run no matter which permissions are set, as described here:
> https://github.com/Vegz78/McAirpos/issues/14#issuecomment-900680039
> https://github.com/microsoft/pxt-settings-blocks/issues/2#issuecomment-900662322

Keep up all the great work!

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [June 6, 2022, 5:07pm UTC](https://forum.makecode.com/t/quick-tutorial-on-the-settings-namespace/1013/7 "2022-06-06T17:07:44Z")

</div>

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

> <https://github.com/microsoft/pxt-arcade/issues/3812#issuecomment-1137197195>
>
> \*\*Describe the bug\*\*
> When using the \`settings.exists("string")\` property from t…he \_settings namespace\_ inside an \`if\` statement, the MakeCode Arcade program terminates/crashes (with \_\*\*Segmentation fault\*\*\_?) when compiled for and run natively as \_.elf\_ file on a Raspberry Pi. 
> 
> More precisely, the program terminates on the \`settings.exists("scores")\` property only if this settings file does not exist yet, but it works if the file already exists.\<br\>\<br\>
> 
> \*\*To Reproduce\*\*
> Steps to reproduce the behavior:
> 1. Go to 'https://arcade.makecode.com/#pub:\_dH1asvKAaUF8?nolocalhost=1&compile=rawELF&hw=rpi'
> 2. Click on 'Download' to download \_.elf\_ file
> 3. \`chmod 755 arcade-blocks-leaderboard\_ns.elf\`
> 4. Execute game with \`./arcade-blocks-leaderboard\_ns.elf\` or using \[McAirpos\](https://github.com/Vegz78/McAirpos)
> 5. After entering the first leader board entry, see error:
> \[1442173983\] GC block 16376b @ 0x20000000
> \[0\] runtime starting, pid=8664...
> \[6\] FB: DRM emulated at 1920x1080 1920x1080 bpp=32
> \[12\] FB: DRM emulated at 1920x1080 1920x1080 bpp=32 7680
> \[16\] init keys
> \[16\] read config: /sd/arcade.cfg 
> \[21\] SCAN\_CODES=/dev/input/event4
> \[25\] BTN\_LEFT=30
> \[28\] BTN\_RIGHT=32
> \[32\] BTN\_UP=17
> \[35\] BTN\_DOWN=31
> \[39\] BTN\_A=29
> \[42\] BTN\_B=42
> \[48\] BTN\_LEFT2=105
> \[51\] BTN\_RIGHT2=106
> \[54\] BTN\_UP2=103
> \[57\] BTN\_DOWN2=108
> \[60\] BTN\_A2=100
> \[63\] BTN\_B2=57
> \[66\] BTN\_RESET=59
> \[69\] BTN\_EXIT=1
> \[73\] BTN\_MENU=60
> \[76\] config done
> \[86\] sx=8 sy=9 ox=320 oy=0 32=1
> \[89\] fbuf=0xb5c29000 sz:8294400
> \[101\] loop
> \[336\] GC block 16376b @ 0x20004000
> \[343\] GC block 16376b @ 0x20008000
> \[1336\] PCM name: 'default'
> \[1342\] PCM state: PREPARED
> \[4358\] GC block 16376b @ 0x2000c000
> (\*\*Segmentation fault\*\*) (Not sure if this last is from the game file itself when crashing or from the launcher)\<br\>\<br\>
> 
> \*\*Expected behavior\*\*
> The MakeCode Arcade program should continue execution when encountering the \`settings.exists("string")\` property.
> 
> \*\*Screenshots\*\*
> !\[Screenshot 2021-08-17 at 22 43 34\](https://user-images.githubusercontent.com/49032025/129802585-fc67155f-f79f-493b-ba89-91e555890f00.png)
> 
> \*\*Desktop (please complete the following information):\*\*
> - OS'es run on: \[Raspberry Pi OS Jessi, Buster and Buster, on RPi 3B, RPi 3B and RPi4 B, respectively\]
> - Browsers compiled and downloaded from: \[various Chrome based browsers on MacOS and updated Win10\]
> \<br\>
> 
> \*\*Additional context\*\*
> The problem seems to be with the \_settings namespace\_ itself in pxt-arcade and NOT with the \[settings extension\](https://github.com/microsoft/pxt-settings-blocks)
> 
> It works inside the web simulator in the MakeCode Arcade editor on all browsers and OS'es.
> 
> The above none-working example is based on @riknoll's \[example from the MakeCode forum\](https://forum.makecode.com/t/save-settings-for-everyone/5932/2) which also is NOT working:
> https://arcade.makecode.com/#pub:46238-89332-69341-89890?nolocalhost=1&compile=rawELF&hw=rpi
> !\[Screenshot 2021-08-17 at 22 46 47\](https://user-images.githubusercontent.com/49032025/129802496-1efbd727-bc7e-4da6-83f0-e3b9c21d5b7a.png)
> 
> Here is a solution with a work-around instead of the \`settings.exists("scores")\` property, and which IS working:
> https://arcade.makecode.com/#pub:\_816E0X0brPog?nolocalhost=1&compile=rawELF&hw=rpi
> !\[Screenshot 2021-08-17 at 22 45 10\](https://user-images.githubusercontent.com/49032025/129802551-a40d9515-50af-4886-81b1-165aa1e27604.png)
> 
> It might also be worth mentioning(and maybe somewhat related?), that when using the \_settings namespace\_ functions, one often has to run/execute the games twice, to allow for all the settings files to be written properly in the \_game\\\_file.elf.data\_ folder, which seldom works fully/correctly on the first run no matter which permissions are set, as described here:
> https://github.com/Vegz78/McAirpos/issues/14#issuecomment-900680039
> https://github.com/microsoft/pxt-settings-blocks/issues/2#issuecomment-900662322

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [June 20, 2022, 7:54pm UTC](https://forum.makecode.com/t/quick-tutorial-on-the-settings-namespace/1013/8 "2022-06-20T19:54:10Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/3/37745b3722f6416ab6139932dbb5ea0972ba4eeb.png)

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.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/b/b803655804488dee6a8fb9577ee0afb26ca59ff2.png)

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

 ![arcade-screenshot](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/51bcdeeae60c6c6187bf039f31529477c6c1e6ce.png)

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

 ![arcade-screenshot (1)](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/642737dfaa9a0f93a89da0ba88b8a123ec118ac1.jpeg)

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

 ![arcade-screenshot (3)](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/1/1500b776d43f560e9dda07a2bfe81da7281c57eb.png)

You can play with the code here:

> **[Settings Blocks](https://arcade.makecode.com/67083-67316-16885-42466)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

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!
