From the releae notes: Named triggers can act like reusable functions: they can declare variables (text or number), and the "Trigger Named Trigger" action lets you pass values directly.
I have seen there is a new checkbox in the Named triggger: "Named Trigger Requires Variables" (it opens 4 fields for 2 vars)
When calling the named trigger you can provide values for variables and the named trigger can then use these variables (e.g. in scripts like in that example, but any action can be converted to Java Script. Or they could be used in if conditions etc. inside of the named trigger.)
they are supposed to be used like function parameters, this is important for some other stuff I am working on and I don't want to mix that with persisting stuff.
Very cool, I've been hoping for a way to add parameters to triggers!
Some questions about using this feature:
Assume I define a trigger "My_New_Trigger2", with the following variables:
var1, type Number
var2, type Text
I see that each of these has two fields below it, "Variable 1, Default Options" and "Variable 1 Description". Is the Default Options for providing a default value for the field in case no value is provided when the trigger is called?
In my initial try at this, I put 13 in the default field, and I then have the following in my JavaScript action:
async function someJavaScriptFunction() {
let var1 = await get_number_variable("var1");
return var1;
}
I had assumed this should return 13 when run from the run button in the BTT config panel, but I'm getting undefined as a result. In your sample, the contents of the "Variable 1, Default Options" field is "the desired window width", but this looks like a description to me, so I clearly don't fully understand the intended usage...
1.) Ah, the "Default Options" name is bad, sorry! It's not a default value - it's a list of suggested values (one per line). These show up as autocomplete suggestions when you configure the "Trigger Named Trigger" action, and they are also provided to AI / Spotlight so they know which values make sense for that variable. The variable itself is only set when a caller actually passes a value. That's also why the "Run" button gives you undefined - it just runs the named trigger without passing any variables. I'll rename that field and add a better description with the next build.
For now, if you want a default value, the easiest way is to handle it in your script:
async function someJavaScriptFunction() {
let var1 = await get_number_variable("var1") ?? 13;
return var1;
}
2.) You just add the variables as additional keys, using the variable names you defined:
let result = await trigger_named({
trigger_name: "My_New_Trigger2",
wait_for_reply: true,
var1: 13,
var2: "some text"
});
One caveat: currently, when calling from Java Script, make sure to pass number variables as actual numbers (var1: 13, not var1: "13"), otherwise get_number_variable won't find them. The next build will automatically convert the value based on the type you declared in the named trigger, like the "Trigger Named Trigger" action already does.
3.) Both work If your script is an async function, just use return x; - BTT waits for the promise and uses the resolved value. returnToBTT(x) is the older style, which is mainly needed if your script is not an async function (e.g. callback-based code). For new scripts I'd recommend the async function + return variant.