第13行: |
第13行: |
| See [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/ ''Events'' in the C# programming guide] for more info. | | See [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/ ''Events'' in the C# programming guide] for more info. |
| | | |
− | ===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:
| + | 通常将事件处理程序添加到 <tt>Entry</tt> 方法中,可以随时添加和删除它们。例如,在每天开始时打印一条消息。首先,从下面的列表中选择适当的事件 (<tt>[[#GameLoop.DayStarted|GameLoop.DayStarted]]</tt>), 然后添加一个事件处理程序,并在方法代码中执行以下操作: |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
− | /// <summary>The main entry point for the mod.</summary> | + | /// <summary> 模组的主要入口点。</summary> |
| public class ModEntry : Mod | | public class ModEntry : Mod |
| { | | { |
| /********** | | /********** |
− | ** Public methods | + | ** 公共方法 |
| *********/ | | *********/ |
− | /// <summary>The mod entry point, called after the mod is first loaded.</summary> | + | /// <summary> 模组入口点,加载模组后自动调用</summary> |
− | /// <param name="helper">Provides simplified APIs for writing mods.</param> | + | /// <param name="helper"> 提供用于编写模组的简化API</param> |
| public override void Entry(IModHelper helper) | | public override void Entry(IModHelper helper) |
| { | | { |
− | // event += method to call | + | // 事件 += 方法 |
| helper.Events.GameLoop.DayStarted += this.OnDayStarted; | | helper.Events.GameLoop.DayStarted += this.OnDayStarted; |
| } | | } |
− | | + | |
− | /// <summary>The method called after a new day starts.</summary> | + | /********** |
− | /// <param name="sender">The event sender.</param> | + | ** 私有方法 |
− | /// <param name="e">The event arguments.</param> | + | *********/ |
| + | /// <summary> 在新的一天开始后调用的方法</summary> |
| + | /// <param name="sender"> 事件对象</param> |
| + | /// <param name="e"> 事件参数</param> |
| private void OnDayStarted(object sender, DayStartedEventArgs e) | | private void OnDayStarted(object sender, DayStartedEventArgs e) |
| { | | { |
− | this.Monitor.Log("A new day dawns!"); | + | this.Monitor.Log(" 新的一天到来了!"); |
| } | | } |
| } | | } |
| </syntaxhighlight> | | </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.)
| + | 提示:不需要记住方法参数。在 Visual Studio 中,输入 <code>helper.Events.GameLoop.SaveLoaded +=</code> 然后按 {{key|TAB}} 来自动生成方法 |
| | | |
| ===How do events fit into the game?=== | | ===How do events fit into the game?=== |