Skip to content
Tomasz Cichoń edited this page Aug 23, 2020 · 2 revisions

Settings system provides an easy and streamlined way for your mods to store the preferences. The system is so stupidly simple that the example below explains it all!

A practical example

using System;
using System.Collections.Generic;
using Reactor.API.Attributes;
using Reactor.API.Configuration;
using Reactor.API.Interfaces.Systems;
using Reactor.API.Logging;

namespace CentrifugeExampleMods
{
    enum TestEnum
    {
        This,
        Is,
        A,
        Test
    }
    
    [ModEntryPoint(ModID)]
    public class SettingsExampleMod
    {
        public const string ModID = "com.github.Ciastex.SettingsExampleMod";

        private Settings _settings;
        
        public void Initialize(IManager manager)
        {
            InitializeSettings();
            
            // Want to set a thing? Use an indexer.
            _settings["ExampleInt"] = 10;
            
            // Want to get a thing? Use a method.
            // NOTE: This will throw if types don't match, so be careful!
            _settings.GetItem<int>("ExampleInt");
            
            // Want to see if we have a setting X of type Y?
            _settings.ContainsKey<int>("ExampleInt");
            
            // Will save and overwrite all settings in the JSON file.
            _settings.Save();
        }

        public void InitializeSettings()
        {
            _settings = new Settings("example");

            // Not sure if settings exist? Try this.
            _settings.GetOrCreate<int>("ExampleInt", 2);
            _settings.GetOrCreate<float>("ExampleFloat", .08f);
            _settings.GetOrCreate<string>("ExampleString", "lorem ipsum dolor sit amet");
            _settings.GetOrCreate<List<string>>(
                "ExampleListOfStrings",
                new List<string> { "lorem", "ipsum", "dolor", "sit", "amet" }
            );
            _settings.GetOrCreate<TestEnum>("ExampleEnum", TestEnum.Is);
            
            // Will not overwrite anything if settings already exist!
            _settings.SaveIfDirty();
        }
    }
}
Clone this wiki locally