Line 15: |
Line 15: |
| ===How do I use them?=== | | ===How do I use them?=== |
| 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 each day starts. First you would choose the appropriate event from the list below (<tt>[[#GameLoop.DayStarted|GameLoop.DayStarted]]</tt>), then add an event handler, and do something in your method code: | | 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 each day starts. First you would choose the appropriate event from the list below (<tt>[[#GameLoop.DayStarted|GameLoop.DayStarted]]</tt>), then add an event handler, and do something in your method code: |
− | <source lang="c#"> | + | <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 38: |
Line 38: |
| } | | } |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| Tip: you don't need to memorise the method arguments. In Visual Studio, type <code>helper.Events.GameLoop.SaveLoaded +=</code> and press {{key|TAB}} to auto-create a method. (There doesn't seem to be an equivalent feature in MonoDevelop.) | | Tip: you don't need to memorise the method arguments. In Visual Studio, type <code>helper.Events.GameLoop.SaveLoaded +=</code> and press {{key|TAB}} to auto-create a method. (There doesn't seem to be an equivalent feature in MonoDevelop.) |
Line 726: |
Line 726: |
| ===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: |
− | <source lang="c#"> | + | <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 762: |
Line 762: |
| } | | } |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| ===Custom priority=== | | ===Custom priority=== |
Line 769: |
Line 769: |
| 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>). '''You should only do this if strictly needed'''; depending on event handler order between mods is fragile (e.g. the other mod might change its priority too). | | 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>). '''You should only do this if strictly needed'''; depending on event handler order between mods is fragile (e.g. the other mod might change its priority too). |
| | | |
− | <source lang="c#"> | + | <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 796: |
Line 796: |
| } | | } |
| } | | } |
− | </source> | + | </syntaxhighlight> |