Line 5: |
Line 5: |
| __TOC__ | | __TOC__ |
| ==Intro== | | ==Intro== |
− | The SMAPI console is the window that opens alongside the game, which displays messages from SMAPI and mods in a text-only format. Players can enter commands directly into that window to interact with mods. For example, you can type <tt>help</tt> to see a list of available commands. Note that most players aren't comfortable with a command-line interface, so you should prefer in-game interfaces for player-facing features in most cases. | + | The SMAPI console is the window that opens alongside the game, which displays messages from SMAPI and mods in a text-only format. Players can enter commands directly into that window to interact with mods. For example, you can type <samp>help</samp> to see a list of available commands. Note that most players aren't comfortable with a command-line interface, so you should prefer in-game interfaces for player-facing features in most cases. |
| + | |
| + | [[File:smapi-console-window.png|thumb|none|The SMAPI console window with example mods (for the non-developer version of SMAPI).]] |
| | | |
| ==Add a custom command== | | ==Add a custom command== |
| Each console command must have: | | Each console command must have: |
| * A name which the player types to invoke the command. | | * A name which the player types to invoke the command. |
− | * A description shown when the player uses the <tt>help</tt> command. This should explain what the command does, how to use it, and what arguments it accepts. The example below shows the recommended convention. | + | * A description shown when the player uses the <samp>help</samp> command. This should explain what the command does, how to use it, and what arguments it accepts. The example below shows the recommended convention. |
| * The method to call when the command is entered. | | * The method to call when the command is entered. |
| | | |
− | This code creates a minimal <tt>player_setmoney</tt> command: | + | This code creates a minimal <samp>player_setmoney</samp> command: |
− | <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 37: |
Line 39: |
| private void SetMoney(string command, string[] args) | | private void SetMoney(string command, string[] args) |
| { | | { |
− | Game1.player.money = int.Parse(args[0]); | + | Game1.player.Money = int.Parse(args[0]); |
− | this.Monitor.Log($"OK, set your money to {args[0]}."); | + | this.Monitor.Log($"OK, set your money to {args[0]}.", LogLevel.Info); |
| } | | } |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| Here's how the player would use it: | | Here's how the player would use it: |
Line 55: |
Line 57: |
| </pre> | | </pre> |
| | | |
− | ==Trigger a command== | + | ==See also== |
− | You can programmatically trigger a command through the console API by specifying its name and arguments:
| + | * [[Modding:Modder Guide/APIs/Integrations|Integration APIs]] |
− | <source lang="C#">
| + | * [[Modding:Modder Guide/APIs/Logging|Logging API]] |
− | this.Helper.Commands.Trigger("player_setmoney", new[] { "5000" });
| |
− | </source>
| |
| | | |
− | Note that this is rarely useful. You won't receive any text the command outputs, and it's better to use [[../#Mod-provided APIs|mod-provided APIs]] for integrations.
| + | [[zh:模组:制作指南/APIs/Console]] |