“模组:制作指南/APIs/Config”的版本间的差异

来自Stardew Valley Wiki
跳到导航 跳到搜索
(翻译(我尽力了,以我水平只能翻成这样,改天我找个学计算机的学长帮我))
第3行: 第3行:
 
You can let users configure your mod through a standard <samp>config.json</samp> file. SMAPI will automatically create the file and take care of reading, normalising, and updating it.
 
You can let users configure your mod through a standard <samp>config.json</samp> file. SMAPI will automatically create the file and take care of reading, normalising, and updating it.
  
==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). <br>
 +
这是一个简单的配置模型:
 
<syntaxhighlight lang="c#">
 
<syntaxhighlight lang="c#">
 
public sealed class ModConfig
 
public sealed class ModConfig
第14行: 第15行:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
That model would be saved to <samp>config.json</samp> with this content:
+
这个模型将会保存到<samp>config.json</samp> 中,内容如下:
 
<syntaxhighlight lang="json">
 
<syntaxhighlight lang="json">
 
{
 
{
第22行: 第23行:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
These properties must be public.
+
这个属性必须是公开的。
  
===Default values===
+
=== 默认值===
You can set default values in your data model:
+
你可以在数据模型中设置默认值:
 
<syntaxhighlight lang="c#">
 
<syntaxhighlight lang="c#">
 
public sealed class ModConfig
 
public sealed class ModConfig

2024年9月8日 (日) 14:48的版本

制作SMAPI模组 SMAPI mascot.png


模组:目录

Robin building.png
“我这里还有很多事情需要处理。”
— 罗宾

不完整的翻译

本文或部分尚未完全翻译成中文。 欢迎您通过编辑帮助其建设。
最后编辑Candybear于2024-09-08 14:48:17.

You can let users configure your mod through a standard config.json file. SMAPI will automatically create the file and take care of reading, normalising, and updating it.

配置模型

创建配置模型

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).
这是一个简单的配置模型:

public sealed class ModConfig
{
   public bool ExampleBoolean { get; set; }
   public int ExampleNumber { get; set; }
}

这个模型将会保存到config.json中,内容如下:

{
   "ExampleBoolean": false,
   "ExampleNumber": 0
}

这个属性必须是公开的。

默认值

你可以在数据模型中设置默认值:

public sealed class ModConfig
{
   public bool ExampleBoolean { get; set; } = true;
   public int ExampleNumber { get; set; } = 5;
}

...or set defaults with a constructor:

public sealed class ModConfig
{
   public bool ExampleBoolean { get; set; }
   public int ExampleNumber { get; set; }

   public ModConfig()
   {
      this.ExampleBoolean = true;
      this.ExampleNumber = 5;
   }
}

Using the config file

To read the config.json (SMAPI will create it automatically):

  1. Create your config model.
  2. Access the config values in your ModEntry class:
    /// <summary>The main entry point for the mod.</summary>
    internal sealed class ModEntry : Mod
    {
        /*********
        ** Properties
        *********/
        /// <summary>The mod configuration from the player.</summary>
        private ModConfig Config;
    
    
        /*********
        ** Public methods
        *********/
        /// <summary>The mod entry point, called after the mod is first loaded.</summary>
        /// <param name="helper">Provides simplified APIs for writing mods.</param>
        public override void Entry(IModHelper helper)
        {
            this.Config = this.Helper.ReadConfig<ModConfig>();
            bool exampleBool = this.Config.ExampleBoolean;
        }
    }
    

That's it! When the player launches the game, SMAPI will create the config.json file automatically if it doesn't exist yet, using the default config options you provided in your model. If you need to save some changes, you can use this.Helper.WriteConfig(this.Config).

Note that ReadConfig will raise an exception if the user does not provide a valid JSON.

Keybind settings

主要文章:模组:制作指南/APIs/Input

You can use SMAPI's KeybindList in your model to let users configure keybinds. This automatically supports multi-key or alternative bindings (e.g., to support split-screen mode):

class ModConfig
{
   public KeybindList ToggleKey { get; set; } = KeybindList.Parse("LeftShift + F2, LeftTrigger");
}

The value is automatically written/parsed in the config.json file as a string:

{
   "ToggleKey": "LeftShift + F2, LeftTrigger"
}