Changes

Jump to navigation Jump to search
→‎Common techniques: rename to advanced, add event priorities in SMAPI 3.6
Line 700: Line 700:  
|}
 
|}
   −
==Common techniques==
+
==Advanced==
 
===Change monitoring===
 
===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:
 
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:
Line 740: Line 740:  
}
 
}
 
</source>
 
</source>
 +
 +
===Custom priority===
 +
{{SMAPI upcoming|3.6|content=
 +
SMAPI calls event handlers in the same order they're registered by default, so the first event handler registered is the first to receive the event each time. This isn't always predictable, since it depends on mod load order and when each mod registers their handlers. This order is also an implementation detail, so it's not guaranteed.
 +
 +
If you need more control over the order, you can specify an event priority using the <code>[EventPriority]</code> attribute: <tt>Low</tt> (after most handlers), <tt>Default</tt>, <tt>High</tt> (before most handlers), or a custom value (e.g. <tt>High + 1</tt> is higher priority than <tt>High</tt>).
 +
 +
<source lang="c#">
 +
/// <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.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>
 +
    [EventPriority(EventPriority.High)]
 +
    private void OnUpdateTicked(object sender, EventArgs e)
 +
    {
 +
        this.Monitor.Log("Update!");
 +
    }
 +
}
 +
</source>
 +
}}
translators
8,447

edits

Navigation menu