RhythmGame 1.4.3
Loading...
Searching...
No Matches
3. Read a profile and save a theme setting

Previous | Tutorial | Next: Translations

Goal: Greet the active player by name and save a greeting color for their profile.

Install docs/examples/theme-tutorial/03-profile/ and choose it for Main Menu.

Download 03-profile or open the source folder.

Read the active profile

Rg gives QML access to the game's shared objects. It is a singleton, which means you use the existing object by name after importing RhythmGameQml. You don't create an Rg instance.

Follow Rg.profileList.mainProfile to the active profile, then read vars.generalVars for preferences shared across themes. The example binds a local profile property so the rest of the file can use a shorter expression.

readonly property Profile profile: Rg.profileList.mainProfile
readonly property var generalVars: screen.profile.vars.generalVars
readonly property var themeVars:
screen.profile.vars.themeVars["main"][QmlUtils.themeName]

The greeting joins "Hello, " and generalVars.name with +. If the active profile changes, the binding follows the new profile and the label updates.

Label {
Layout.fillWidth: true
text: "Hello, " + screen.generalVars.name
color: screen.themeVars.accentColor
font.pixelSize: 32
wrapMode: Text.Wrap
}

Use the main profile for shared screens such as the menu. During gameplay, use the profile attached to each player for that player's display. A battle can involve two profiles, and a result's score owner need not be the current main profile. Later lessons show the supplied player and result objects.

Declare a setting for the menu

The manifest points the main screen to a settings file as well as a QML file. The settings screen uses these definitions to create controls for changing the values. A setting with type hidden has no control.

{
"$schema": "https://raw.githubusercontent.com/Bobini1/RhythmGame/master/schemas/theme.json",
"scripts": {
"main": "main.qml"
},
"settings": {
"main": "main-settings.json"
}
}
{
"$schema": "https://raw.githubusercontent.com/Bobini1/RhythmGame/master/schemas/themeSettings.json",
"items": [
{
"id": "accentColor",
"name": {
"en": "Accent color"
},
"description": {
"en": "The color of the greeting on the main menu."
},
"type": "color",
"default": "#a8dcc9"
}
]
}

id is the name used in QML. name and description are the text shown in the settings form, and default supplies the first value for a profile. The color type tells the form which control to use.

Saved values live under profile.vars.themeVars[screenName][themeName]. The example uses "main" for its screen and QmlUtils.themeName for its folder name, so it still works if a user renames the folder. QmlUtils is an attached property.

Settings belong to a profile, screen and theme. Read only the current screen's settings; another screen may use a different theme.

If a theme omits k5 or k10, the game can use its k7 or k14 screen, respectively. These fallback screens share the original screen's settings.

Try the saved value

Open Settings > Themes > Main Menu and change Accent color. Return to the menu, then restart the game to check that the color persists. Change the profile's name in its player settings and check the greeting too.

The next lesson adds translations for the greeting, buttons and color setting.

Find other game data

The Rg reference lists the available game services. Later lessons use standard components to handle tasks such as browsing and opening charts. For screen navigation, use the separate globalRoot object, for example globalRoot.returnToPreviousScreen().