Changes

Jump to navigation Jump to search
+ common technique: change monitoring
Line 699: Line 699:  
}}
 
}}
 
|}
 
|}
 +
 +
==Common techniques==
 +
===Change monitoring===
 +
You may want to handle a change that doesn't have its own event (e.g. an in-game event ends, a letter is added to the mailbox, etc). You can usually do that by handling a general event like [[#GameLoop.UpdateTicked|<tt>UpdateTicked</tt>]], and detecting when the value(s) you're watching changed. For example, here's a complete mod which logs a message when an in-game event ends:
 +
<source lang="c#">
 +
/// <summary>The main entry point for the mod.</summary>
 +
public class ModEntry : Mod
 +
{
 +
    /*********
 +
    ** Fields
 +
    *********/
 +
    /// <summary>The in-game event detected on the last update tick.</summary>
 +
    private Event LastEvent;
 +
 +
 +
    /*********
 +
    ** 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.Events.GameLoop.UpdateTicked += this.OnUpdateTicked;
 +
    }
 +
 +
 +
    /*********
 +
    ** Private methods
 +
    *********/
 +
    /// <summary>The method invoked when the game updates its state.</summary>
 +
    /// <param name="sender">The event sender.</param>
 +
    /// <param name="e">The event arguments.</param>
 +
    private void OnUpdateTicked(object sender, EventArgs e)
 +
    {
 +
        if (this.LastEvent != null && Game1.CurrentEvent == null)
 +
            this.Monitor.Log($"Event {this.LastEvent.id} just ended!");
 +
 +
        this.LastEvent = Game1.CurrentEvent;
 +
    }
 +
}
 +
</source>
translators
8,403

edits

Navigation menu