Tutorial: switchable colour schemes
What you will learn
- Add a script to a scoreboard.
- Read a setting from JavaScript.
- Create multiple colour settings from one scheme setting.
In the previous tutorial we exposed every colour as a separate setting. That works, but switching from a red theme to a blue theme means changing three settings. In this tutorial we add one color-scheme setting and let a script fill in the individual colours.
Step 1: replace the individual settings
Keep the board from the previous tutorial, but replace the three colour settings with one:
settings:
# Choose between RED, GREEN, BLUE and YELLOW
color-scheme: 'RED'The lines still use {{ setting:color-light }}, {{ setting:color-dark }} and {{ setting:color-subtitle }}, so they will be empty until the script creates those settings.
Step 2: add a script
Add script-pre at the root of the board file. It runs before the lines are loaded, which means the settings it creates are available as placeholders.
settings:
color-scheme: 'RED'
script-pre: |
if (settings['color-scheme'] == 'RED') {
board.getMeta().setSetting('color-light', '&8');
board.getMeta().setSetting('color-dark', '&4');
board.getMeta().setSetting('color-subtitle', '&c');
} else if (settings['color-scheme'] == 'GREEN') {
board.getMeta().setSetting('color-light', '&a');
board.getMeta().setSetting('color-dark', '&f');
board.getMeta().setSetting('color-subtitle', '&2');
} else if (settings['color-scheme'] == 'BLUE') {
board.getMeta().setSetting('color-light', '&3');
board.getMeta().setSetting('color-dark', '&1');
board.getMeta().setSetting('color-subtitle', '&9');
} else if (settings['color-scheme'] == 'YELLOW') {
board.getMeta().setSetting('color-light', '&f');
board.getMeta().setSetting('color-dark', '&6');
board.getMeta().setSetting('color-subtitle', '&e');
}Reload the board:
/fb reload tutorialIt looks exactly like the previous board, but the palette now comes from color-scheme.
Step 3: switch schemes
Change the setting and reload, or use the in-game command:
/fb editsetting tutorial color-scheme GREENWith GREEN, the same board becomes:
A cleaner switch statement
The if/else if chain works, but a switch is easier to extend:
script-pre: |
switch (settings['color-scheme']) {
case 'RED':
board.getMeta().setSetting('color-light', '&8');
board.getMeta().setSetting('color-dark', '&4');
board.getMeta().setSetting('color-subtitle', '&c');
break;
case 'GREEN':
board.getMeta().setSetting('color-light', '&a');
board.getMeta().setSetting('color-dark', '&f');
board.getMeta().setSetting('color-subtitle', '&2');
break;
case 'BLUE':
board.getMeta().setSetting('color-light', '&3');
board.getMeta().setSetting('color-dark', '&1');
board.getMeta().setSetting('color-subtitle', '&9');
break;
case 'YELLOW':
board.getMeta().setSetting('color-light', '&f');
board.getMeta().setSetting('color-dark', '&6');
board.getMeta().setSetting('color-subtitle', '&e');
break;
}How the script works
| Variable | What it is |
|---|---|
board | The scoreboard object being built. |
settings | A map of the board's current settings. |
board.getMeta().setSetting(key, value) | Adds or replaces a setting. |
The script runs before the frames are parsed, so the new settings are available to {{ setting:... }} in every line.
Scripts are optional
If you do not want board scripts on your server, set tweaks.trust-scoreboard-scripts: false. The colour scheme tutorial then needs the individual colour settings from the previous page instead.
Extending the idea
The same pattern works for anything that needs a set of related values:
theme: HALLOWEEN→ colours, icons and messages.season: WINTER→ different spacer lines and effects.rank: VIP→ a different title animation.
Keep the script small and the settings explicit. A board that is easy to read is easier to maintain than a clever one.
