Difference between revisions of "Modding:Modder Guide/APIs/Console"
< Modding:Modder Guide | APIs
Jump to navigation
Jump to search
Pathoschild (talk | contribs) (+ footer nav links) |
Margotbean (talk | contribs) |
||
(11 intermediate revisions by 5 users not shown) | |||
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 < | + | 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 < | + | * 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 < | + | This code creates a minimal <samp>player_setmoney</samp> command: |
− | < | + | <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. | + | 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); |
} | } | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
Here's how the player would use it: | Here's how the player would use it: | ||
Line 55: | Line 57: | ||
</pre> | </pre> | ||
− | == | + | ==See also== |
− | + | * [[Modding:Modder Guide/APIs/Integrations|Integration APIs]] | |
− | + | * [[Modding:Modder Guide/APIs/Logging|Logging API]] | |
− | |||
− | |||
− | |||
− | |||
− | + | [[zh:模组:制作指南/APIs/Console]] | |
− | |||
− | |||
− |
Latest revision as of 17:18, 3 April 2024
- Get started
- Game fundamentals
- Test & troubleshoot
- Release
- API reference
- Basic SMAPI APIs:
- Advanced SMAPI APIs:
- Specific guides
SMAPI can add custom commands to the SMAPI console, which players can type to do certain actions. These are also available in the in-game chat for players using the Chat Commands mod.
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 help 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.
Add a custom command
Each console command must have:
- A name which the player types to invoke the command.
- A description shown when the player uses the help 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.
This code creates a minimal player_setmoney command:
/// <summary>The main entry point for the mod.</summary>
public class ModEntry : Mod
{
/*********
** 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)
{
helper.ConsoleCommands.Add("player_setmoney", "Sets the player's money.\n\nUsage: player_setmoney <value>\n- value: the integer amount.", this.SetMoney);
}
/*********
** Private methods
*********/
/// <summary>Set the player's money when the 'player_setmoney' command is invoked.</summary>
/// <param name="command">The name of the command invoked.</param>
/// <param name="args">The arguments received by the command. Each word after the command name is a separate argument.</param>
private void SetMoney(string command, string[] args)
{
Game1.player.Money = int.Parse(args[0]);
this.Monitor.Log($"OK, set your money to {args[0]}.", LogLevel.Info);
}
}
Here's how the player would use it:
help player_setmoney > player_setmoney: Sets the player's money. > > Usage: player_setmoney <value> > - value: the integer amount. player_setmoney 5000 > OK, set your money to 5000.