Line 6: |
Line 6: |
| ===Creating a config model=== | | ===Creating a config model=== |
| The ''config model'' is a C# class you create, with properties representing the settings you want to store. It can contain almost anything from a few boolean fields to a complex object graph (though you should try to keep things simple for players). Here's a simple config model: | | The ''config model'' is a C# class you create, with properties representing the settings you want to store. It can contain almost anything from a few boolean fields to a complex object graph (though you should try to keep things simple for players). Here's a simple config model: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| class ModConfig | | class ModConfig |
| { | | { |
Line 12: |
Line 12: |
| public int ExampleNumber { get; set; } | | public int ExampleNumber { get; set; } |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| That model would be saved to <tt>config.json</tt> with this content: | | That model would be saved to <tt>config.json</tt> with this content: |
− | <source lang="json"> | + | <syntaxhighlight lang="json"> |
| { | | { |
| "ExampleBoolean": false, | | "ExampleBoolean": false, |
| "ExampleNumber": 0 | | "ExampleNumber": 0 |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| ===Default values=== | | ===Default values=== |
| You can set default values in your data model: | | You can set default values in your data model: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| class ModConfig | | class ModConfig |
| { | | { |
Line 30: |
Line 30: |
| public int ExampleNumber { get; set; } = 5; | | public int ExampleNumber { get; set; } = 5; |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| ...or set defaults with a constructor: | | ...or set defaults with a constructor: |
| | | |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| class ModConfig | | class ModConfig |
| { | | { |
Line 46: |
Line 46: |
| } | | } |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| ==Using the config file== | | ==Using the config file== |
Line 54: |
Line 54: |
| <li>Create your [[#Config model|config model]].</li> | | <li>Create your [[#Config model|config model]].</li> |
| <li>Access the config values in your <tt>ModEntry</tt> class: | | <li>Access the config values in your <tt>ModEntry</tt> class: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| /// <summary>The main entry point for the mod.</summary> | | /// <summary>The main entry point for the mod.</summary> |
| public class ModEntry : Mod | | public class ModEntry : Mod |
Line 76: |
Line 76: |
| } | | } |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| </li> | | </li> |
| </ol> | | </ol> |
Line 86: |
Line 86: |
| | | |
| You can use SMAPI's [[Modding:Modder Guide/APIs/Input#SButton|<tt>SButton</tt>]] type directly in your config model to configure keybindings: | | You can use SMAPI's [[Modding:Modder Guide/APIs/Input#SButton|<tt>SButton</tt>]] type directly in your config model to configure keybindings: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| class ModConfig | | class ModConfig |
| { | | { |
| public SButton ToggleKey { get; set; } = SButton.F2; | | public SButton ToggleKey { get; set; } = SButton.F2; |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| The value is automatically written/parsed in the <tt>config.json</tt> file as a string: | | The value is automatically written/parsed in the <tt>config.json</tt> file as a string: |
− | <source lang="json"> | + | <syntaxhighlight lang="json"> |
| { | | { |
| "ToggleKey": "F2" | | "ToggleKey": "F2" |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| {{SMAPI upcoming|3.9| | | {{SMAPI upcoming|3.9| |
| For compatibility with split-screen (when you may have players on both keyboard and controller), you should use [[Modding:Modder Guide/APIs/Input#KeybindList|<tt>KeybindList</tt>]] instead: | | For compatibility with split-screen (when you may have players on both keyboard and controller), you should use [[Modding:Modder Guide/APIs/Input#KeybindList|<tt>KeybindList</tt>]] instead: |
| | | |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| class ModConfig | | class ModConfig |
| { | | { |
| public KeybindList ToggleKey { get; set; } = KeybindList.ForSingle(SButton.F2); | | public KeybindList ToggleKey { get; set; } = KeybindList.ForSingle(SButton.F2); |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| This is written/parsed the same way, but lets players specify multi-key or alternative bindings like <code>"LeftShift + F2, LeftTrigger"</code>. | | This is written/parsed the same way, but lets players specify multi-key or alternative bindings like <code>"LeftShift + F2, LeftTrigger"</code>. |
| }} | | }} |