For me, “Sword”, “Dagger” is a list of options.
Now my question is : is it possible to create that list dynamically, i.e. by code?
I had thought of an array, but this doesn’t work.
Please use the toolbar’s Preformatted Text option when supplying code examples. It makes the code easier to read and cut-n-paste, and stops any standard quotes (' and ") that are being used to delimit String values from being converted into invalid topographical “curly” quotes (’, “ and ”).
If you look at the 1st line of the (dropdown:) macro’s documentation you will notice that the String argument is proceeded by a spread operator (...), which consists of three consecutive full stops. Using this operator allows you to pass an Array to the macro in place of the String list…
(set: _list to (a: "Sword", "Dagger"))
(dropdown: bind $Choice, ..._list)
…and this Array can be dynamically generated.
(set: _list to (a:))
(set: _list to it + (a: "Sword"))
(set: _list to it + (a: "Dagger"))
(dropdown: bind $Choice, ..._list)
note: I am using a Temporary variable (_list) in my examples because:
the Array variable is created within the same passage as the (dropdown:) macro call,
I have no need for the contents of the Array variableto be tracked by the story’s History.
If you need to either:
create the Array variable in a (one or more) previously visited Passage(s) or within your project’s startup tagged Passage,
track the contents of the Array variable so it can be used again in a later visited Passage.
…then you may want to replace the Temporary variable (_list) with a Story variable ($list).
Note that using spread syntax means that your code won’t work in any version of Internet Explorer, and certain kinds of spread syntax won’t work in Edge either (see the “Browser compatibility” section at that link).
Normally you’d use the apply() method on a function to get around that problem. Unfortunately, due to how Harlowe works, I’m not sure if there’s an easy fix like that.
You might want to contact Leon Arnott, the maintainer of Harlowe, via Harlowe’s issues page to see if he’d add a version of “(dropdown: …)” which would accept arrays of strings as a parameter.
Harlowe has its own spread operator that is handled in-engine, so it should be safe to use anywhere Harlowe may generally be. JavaScript’s spread operator does require environment support as HiEv described.
In Greyelf’s code example above, Harlowe’s native spread operator is being used, so you should be fine.