Modding:Modder Guide/APIs/Console

From Stardew Valley Wiki
< Modding:Modder Guide‎ | APIs
Revision as of 00:57, 28 May 2018 by Pathoschild (talk | contribs) (move content from Modding:Modder Guide/APIs (only author is Pathoschild))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Creating SMAPI mods SMAPI mascot.png


Modding:Index

Console commands

You can add commands to the SMAPI console (the terminal window that opens alongside the game), and invoke them by typing directly into the console. Note that most players aren't comfortable with a command-line interface, so you should prefer in-game interfaces for player-facing features.

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 code to run when the command is called.

The code below creates a player_setmoney command (but don't forget to validate input, this is just an example).

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);
}

/// <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]}.");
}

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.