Custom Configuration Serialization into Settings.json

Using Naninovel C# APIs: adding custom actor implementations or commands, overriding engine services, integrating with other systems, etc.
Post Reply
Eluem
Posts: 5
Joined: 11 Apr 2024 05:47

Custom Configuration Serialization into Settings.json

Post by Eluem »

I'm trying to create a simple new setting that can be altered by the player in a custom SettingsUI,

I'm not sure what the correct way to implement this is. The previous programmer working on this project simply made a custom global variable which kind of works, but then it requires saving the global variables/game state to save the settings, which isn't a desired behavior. So, I began investigating further and found that NaniNovel has 3 separate ways to save states, including a "Settings" state which saves into Settings.json and seems like the correct solution.

The problem I'm having is correctly getting my new custom Configuration to automatically serialize and correctly save into the Settings.json.

I read over https://naninovel.com/guide/state-management very thoroughly but the example code in there for serializing a custom Setting or Global seems incomplete and impossible to directly follow.

I'm also not positive how I'm supposed to access the data.

Here's what my code looks like:

Code: Select all

using Naninovel;
using UnityEngine;

[EditInProjectSettings]
[System.Serializable]
public class ColorTheme : Configuration
{
    [System.Serializable]
    public enum ColorThemeEnum
    {
        Dark,
        Light
    }

    [Header("Color Theme Settings")]
    [Tooltip("Selected color theme.")]
    public ColorThemeEnum selectedTheme = ColorThemeEnum.Dark;

    //Completely unsure of everything below this... just trying to figure out how to correctly copy https://naninovel.com/guide/state-management#custom-state
    private IStateManager stateManager;
    private void Awake()
    {
        stateManager = Engine.GetService<IStateManager>();
    }

    ColorTheme MyColorTheme
    {
        get => stateManager.SettingsState.GetState<ColorTheme>();
        set => stateManager.SettingsState.SetState<ColorTheme>(value);
    }
}

I'm attempting to access my custom settings like this:

Code: Select all

IStateManager stateManager = Engine.GetService<IStateManager>();
ColorTheme colorTheme = stateManager.SettingsState.GetState<ColorTheme>();
Debug.Log(colorTheme.selectedTheme);

However, I'm receiving a null reference exception because the GetState<ColorTheme> call is returning null

Can someone please help guide me. Ideally with a simplified example of how I'm supposed to do this.

Thank you!

Elringus
admin
Posts: 538
Joined: 11 May 2020 18:03

Re: Custom Configuration Serialization into Settings.json

Post by Elringus »

You're confusing settings with configuration.

Settings are player-facing game options, such as language, volume, message speed, etc. They can be changed at runtime and are serialized in settings.json. To add a new such setting, either modify existing engine service or create a new one and implement IStatefulService<SettingsStateMap>. See the existing services that implement this interface for examples.

Configurations are developer-facing Naninovel options, such as resource provider options, memory management policy, etc. They can only be changed by developers before the build and are not exposed to players. Here is the guide on adding custom configs: https://naninovel.com/guide/custom-configuration.

Eluem
Posts: 5
Joined: 11 Apr 2024 05:47

Re: Custom Configuration Serialization into Settings.json

Post by Eluem »

I had a feeling I was mixing them up.. sorry about that.

So I have to actually modify the existing settings state map service or create an entirely new one if I want to add any custom settings? Huh, I thought I'd be able to hook into the existing serialization system externally.

I guess I would edit the UIManager and add my code to that, because it's for supporting a light/dark mode option.

Elringus
admin
Posts: 538
Joined: 11 May 2020 18:03

Re: Custom Configuration Serialization into Settings.json

Post by Elringus »

You don't need to hook into or do anything with the serialization, there is a higher-level abstraction for settings.

Just look into existing services which implement IStatefulService<SettingsStateMap> for example on how to add your custom settings.

Eluem
Posts: 5
Joined: 11 Apr 2024 05:47

Re: Custom Configuration Serialization into Settings.json

Post by Eluem »

Thank you for the input. I believe I have it working now, btw.

I just copied the UIManager and made my own trimmed down CustomUISettings class to add new settings.

Thanks!

Post Reply