Difference between revisions of "Modding:Trigger actions"
Pathoschild (talk | contribs) (→Data format: clarify wording) |
Pathoschild (talk | contribs) (→Triggers: correction (not raised on save loaded)) |
||
Line 86: | Line 86: | ||
|- | |- | ||
| <samp>LocationChanged</samp> | | <samp>LocationChanged</samp> | ||
− | | Raised when the player arrives in a location | + | | Raised when the player arrives in a location. |
|} | |} | ||
Revision as of 06:46, 17 December 2023
← Index
This page documents trigger actions, which let content packs perform an action when something happens. (C# mods should usually use SMAPI events instead.)
Overview
Introduction
Each trigger action is comprised of three main parts:
- The trigger is when to apply the trigger action, either defined by the base game (like LocationChanged) or added by C# mods.
- Actions are what to do (like send mail or start a quest).
- Conditions are optional game state queries to decide whether the trigger action should be applied.
For example, consider this Content Patcher patch:
{
"Format": "2.3.0",
"Changes": [
{
"Action": "EditData",
"Target": "Data/TriggerActions",
"Entries": {
"SomeMod.Id_OnLeoMoved": {
"Trigger": "LocationChanged",
"Location": "Farm",
"Condition": "PLAYER_HAS_FLAG Host leoMoved",
"Actions": [
"AddMail Current Abigail_LeoMoved Today",
"AddConversationTopic LeoMoved 5"
]
}
}
}
]
}
You can read that like: "When the player arrives in the Farm location, if Leo has moved to the valley, then send a letter and start a conversation topic".
Actions only run once by default, though you can use the MarkActionApplied action to re-enable one.
Action format
An action consists of an action name with space-delimited arguments. For example, AddMail Current Abigail_LeoMoved Today
has action name AddMail with three arguments (Current, Abigail_LeoMoved, and Today).
If you have spaces within an argument, you can surround it with quotes to keep it together. For example, AddFriendshipPoints "Mister Qi" 10
has two arguments (Mister Qi and 10). You can escape inner quotes with backslashes, like AddFriendshipPoints "Mister \"Qi\"" 10
.
Remember that quotes and backslashes inside JSON strings need to be escaped too. For example, "AddFriendshipPoints \"Mister Qi\" 10"
will send AddFriendshipPoints "Mister Qi" 10
to the game code. Alternatively, you can use single-quotes for the JSON string instead, like 'AddFriendshipPoints "Mister Qi" 10'
.
Data format
Trigger actions are stored in the Data/TriggerActions asset. This consists of a list of models with these fields:
field | effect |
---|---|
Id | The unique string ID for this trigger action. |
Trigger | When to apply the trigger action. This must be a valid trigger type. |
Actions | The actions to perform. This consists of a list of strings matching the action format. |
Location | (Optional) If set, the internal location name where this action should be applied. This is a shortcut for (and more efficient than) using a LOCATION_NAME game state query. Default none. |
HostOnly | (Optional) Whether this trigger action can only run for the main player. If true, the action will be ignored for farmhands in multiplayer. |
Condition | (Optional) A game state query which indicates whether this action can be applied currently. Defaults to always true. |
Valid values
Triggers
The game has one built-in trigger which can be used in the Trigger field. (Other custom triggers may be added by C# mods.)
trigger | effect |
---|---|
LocationChanged | Raised when the player arrives in a location. |
Actions
These are the built-in actions which can be used in the Actions field. (Other custom actions may be added by C# mods.)
action | effect | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
AddConversationTopic <topic ID> <day duration>
|
Start a conversation topic for the given number of days. If the topic is already active, this resets its duration to the given number. | ||||||||||
RemoveConversationTopic <topic ID>
|
End a conversation topic, if it's active. | ||||||||||
AddCookingRecipe <player> <recipe ID> AddCraftingRecipe <player> <recipe key>
|
Add a cooking or crafting recipe to the specified player(s). | ||||||||||
AddFriendshipPoints <NPC name> <count>
|
Add <count> friendship points between the current player and specified NPC. The <count> can be negative to reduce friendship points.
| ||||||||||
AddItem <item ID> [count] [quality]
|
Add an item by its qualified or unqualified item ID to the current player's inventory, with an optional count (default 1) and quality (default 0). | ||||||||||
RemoveItem <item ID> [count]
|
Deduct items by qualified or unqualified item ID from the current player's inventory, up to a max combined stack size of [count] (default 1).
| ||||||||||
AddMail <player> <mail ID> <type> RemoveMail <player> <mail ID> [type]
|
Add or remove a mail flag or letter for the specified player(s).
The
For remove only, | ||||||||||
AddMoney <amount>
|
Add the given <amount> of money for the current player. The <amount> can be negative to deduct money.
| ||||||||||
AddQuest <quest ID> RemoveQuest <quest ID>
|
Add or remove a quest for the specified player(s). | ||||||||||
AddSpecialOrder <order ID> RemoveSpecialOrder <order ID>
|
Add or remove a special order for the specified player(s). | ||||||||||
MarkEventSeen <event ID> [seen]
|
Mark an event as seen (if [seen] is true) or not seen (if false).
| ||||||||||
MarkQuestionAnswered <answer ID> [answered]
|
Mark a dialogue answer as selected (if [answered] is true) or not selected (if false).
| ||||||||||
MarkActionApplied <answer ID> [applied]
|
Mark another trigger action as applied (if [applied] is true) or not applied (if false). This can be used to skip or re-run an action, since actions are only applied once by default.
| ||||||||||
SetNpcInvisible <NPC name> <day duration>
|
Hide an NPC so they disappear and can't be interacted with for the given number of days. This is used when NPCs go away for a while (e.g. Elliott's 14-heart event). | ||||||||||
SetNpcVisible <NPC name>
|
End the NPC's invisibility, if applicable. |
Target player
Some conditions have a <player>
argument. This can be one of...
value | result |
---|---|
All | Apply the action to all players (regardless of whether they're online). |
Current | Apply to the local player. |
Host | Apply to the main player. |
For C# mod authors
Using trigger actions in C# data
Trigger actions are mainly meant for Content Patcher packs. C# mod can use SMAPI's events instead, which are much more flexible and efficient (unless you want to let content packs edit your trigger actions).
Extensibility
C# mods can use the StardewValley.Triggers.TriggerActionManager class to interact with trigger actions.
For example, you can add a new trigger type:
// register custom trigger type
TriggerActionManager.RegisterTrigger("Some.ModId_OnItemReceived");
// run actions for the custom trigger
TriggerActionManager.Raise("Some.ModId_OnItemReceived");
Or you can add a new action handler:
TriggerActionManager.RegisterAction("Some.ModId_PlaySound", this.PlaySound);
...
/// <inheritdoc cref="TriggerActionDelegate" />
public static bool PlaySound(string[] args, string trigger, TriggerActionData data)
{
// get args
if (!ArgUtility.TryGet(args, 1, out string soundId, out string error, allowBlank: false))
return TriggerActionManager.Helpers.LogActionError(args, trigger, data, error);
// apply
Game1.playSound(soundId);
return true;
}
To avoid conflicts, custom trigger names should be unique string IDs.