Harlowe : Question about (dropdown:) macro

Hi, all!

An example of a (dropdown:) macro could be

(dropdown: bind $Choice, “Sword”, “Dagger”)

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.

Thanks in advance.

P.S : Twine 2.3.5 and Harlowe 3.1.0

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:

  1. the Array variable is created within the same passage as the (dropdown:) macro call,
  2. I have no need for the contents of the Array variableto be tracked by the story’s History.

If you need to either:

  1. create the Array variable in a (one or more) previously visited Passage(s) or within your project’s startup tagged Passage,
  2. 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).

Thanks, Greyelf. I had forgotten the spread operator;

The fact is : I’m learning Harlowe while I’m creating my Combat demo…
One good thing, though : I was not mistaken about Harlowe’s capabilities.

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.

Hope that helps! :slight_smile:

Thanks, HiEv; I’ll try to get in touch Leon Harnott, and in the meantime I’ll add a “Readme” to my combat demo.

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.

You’re correct. I looked at the source for the (dropdown: …) macro and didn’t see any code to do that, but it was elsewhere in the code.

I tested it in IE11 (which I should have done to begin with, duh) and yeah, it works fine.

Thanks to you both. You’re very helpful.