Line 244: |
Line 244: |
| C# mods can use the <samp>StardewValley.Triggers.TriggerActionManager</samp> class to interact with trigger actions. | | C# mods can use the <samp>StardewValley.Triggers.TriggerActionManager</samp> class to interact with trigger actions. |
| | | |
− | For example, you can add a new trigger type: | + | For example, you can... |
| + | <ul> |
| + | <li>Add and raise a new trigger type: |
| <syntaxhighlight lang="js"> | | <syntaxhighlight lang="js"> |
| // register custom trigger type | | // register custom trigger type |
| TriggerActionManager.RegisterTrigger("Some.ModId_OnItemReceived"); | | TriggerActionManager.RegisterTrigger("Some.ModId_OnItemReceived"); |
| | | |
− | // run actions for the custom trigger | + | // run actions in Data/TriggerActions for the custom trigger |
| TriggerActionManager.Raise("Some.ModId_OnItemReceived", new[] { item, index }); // trigger can pass optional trigger arguments | | TriggerActionManager.Raise("Some.ModId_OnItemReceived", new[] { item, index }); // trigger can pass optional trigger arguments |
− | </syntaxhighlight> | + | </syntaxhighlight></li> |
| | | |
− | Or you can add a new action handler: | + | <li>Or add a new action: |
| <syntaxhighlight lang="js"> | | <syntaxhighlight lang="js"> |
| TriggerActionManager.RegisterAction("Some.ModId_PlaySound", this.PlaySound); | | TriggerActionManager.RegisterAction("Some.ModId_PlaySound", this.PlaySound); |
Line 260: |
Line 262: |
| | | |
| /// <inheritdoc cref="TriggerActionDelegate" /> | | /// <inheritdoc cref="TriggerActionDelegate" /> |
− | public static bool PlaySound(string[] args, string trigger, object[] triggerArgs, TriggerActionData data) | + | public static bool PlaySound(string[] args, TriggerActionContext context, out string error) |
| { | | { |
| // get args | | // get args |
| if (!ArgUtility.TryGet(args, 1, out string soundId, out string error, allowBlank: false)) | | if (!ArgUtility.TryGet(args, 1, out string soundId, out string error, allowBlank: false)) |
− | return TriggerActionManager.Helpers.LogActionError(args, trigger, data, error); | + | return false; |
| | | |
| // apply | | // apply |
Line 270: |
Line 272: |
| return true; | | return true; |
| } | | } |
− | </syntaxhighlight> | + | </syntaxhighlight></li> |
| + | |
| + | <li>Or run an action string: |
| + | <syntaxhighlight lang="js"> |
| + | // NOTE: this is just an example of how to run an action. This is meant to support actions specified in data or content |
| + | // packs. If you want to send mail (or perform other actions) in C#, it's better to call the C# APIs directly instead. |
| + | string action = "AddMail Current Robin Now"; |
| + | if (!TriggerActionManager.TryRunAction(action, out string error, out Exception ex)) |
| + | Game1.log.Error($"Failed running action '{action}': {error}", ex); |
| + | </syntaxhighlight></li> |
| + | </ul> |
| | | |
| To avoid conflicts, custom trigger names should be [[Modding:Modder Guide/Game Fundamentals#Unique string IDs|unique string IDs]]. | | To avoid conflicts, custom trigger names should be [[Modding:Modder Guide/Game Fundamentals#Unique string IDs|unique string IDs]]. |
| | | |
| [[Category:Modding]] | | [[Category:Modding]] |