Changes

Jump to navigation Jump to search
expand intro
Line 1: Line 1:  
{{../../header}}
 
{{../../header}}
   −
==Events==
+
SMAPI provides several C# events which let your mod respond when something happens (like when the player places an object) or run code periodically (like once per update tick).
SMAPI publishes several C# events that tell you when something happens. For example, if you want to do something after the player loads their save, you can add this to your <tt>Entry</tt> method:
  −
 
  −
<source lang="c#">
  −
SaveEvents.AfterLoad += this.SaveEvents_AfterLoad;
  −
</source>
     −
Then declare a method like this. (The <tt>EventArgs e</tt> argument contains any extra data about the event.)
+
==Intro==
 +
A [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/ C# event] is the way that SMAPI notifies mods when something happens. You can add any number of ''event handlers'' (methods to call) when an event is raised. Event handlers are usually added in your <tt>Entry</tt> method, though you can add and remove them anytime.
    +
For example, let's say you want to print a message when the player loads a save. First you would choose the appropriate event from the list below (<tt>SaveEvents.AfterLoad</tt>), then add an event handler, and do something in your method code:
 
<source lang="c#">
 
<source lang="c#">
/// <summary>The method called after the player loads their save.</summary>
+
/// <summary>The main entry point for the mod.</summary>
/// <param name="sender">The event sender.</param>
+
public class ModEntry : Mod
/// <param name="e">The event arguments.</param>
  −
public void SaveEvents_AfterLoad(object sender, EventArgs e)
   
{
 
{
  this.Monitor.Log("The player loaded their game! This is a good time to do things.");
+
    /**********
  this.Monitor.Log("Everything in the world is ready to interact with at this point.");
+
    ** 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)
 +
    {
 +
        // SaveEvents.AfterLoad is the event
 +
        // this.SaveEvents_AfterLoad is the method below you want to call
 +
        SaveEvents.AfterLoad += this.SaveEvents_AfterLoad;
 +
    }
 +
 
 +
    /// <summary>The method called after the player loads their save.</summary>
 +
    /// <param name="sender">The event sender.</param>
 +
    /// <param name="e">The event arguments.</param>
 +
    private void SaveEvents_AfterLoad(object sender, EventArgs e)
 +
    {
 +
      this.Monitor.Log("The player loaded their game! This is a good time to do things.");
 +
      this.Monitor.Log("Everything in the world is ready to interact with at this point.");
 +
    }
 
}
 
}
 
</source>
 
</source>
    +
Tip: you don't need to memorise the method arguments. In Visual Studio, type <code>SaveEvents.AfterLoad +=</code> and press {{key|TAB}} to auto-create a method. (There doesn't seem to be an equivalent feature in MonoDevelop.)
 +
 +
==Events==
 
The available events are documented below.
 
The available events are documented below.
  
translators
8,441

edits

Navigation menu