I have used Twine a pretty good amount and am getting into its more advanced features, and wanting to extend it to do some more advanced things. I've searched far and wide, but am finding it difficult to find the answer to this one question: Is it possible to create custom macros in Harlowe? And if so, how would I go about implementing one?
For example, I want to make a macro that will compute the intersection of two arrays; that is, given two arrays:
a1["a", "b", "c", "d"], a2["c", "d", "e", "f"]
I want to be able to compute the common elements between the two:
result["c", "d"]
I have a good idea for how to do this in Javascript; I just need to know the exact syntax for the Story Javascript section to be able to call it in Twinescript:
(intersection: $a1, $a2)
Thank you for any help/insight!
Comments
Because Harlowe is still a work-in-progress, any solution involving hacking the engine to do what you want may not continue to work for future releases of the story format.
I suggest contacting Leon directly about the best way to achieve what you want.
Normally when I want to extend Harlowe's functionality I add a javascript function to the window object, which I can then call from either a print or set macro.
For your case I'd do
Then in twinescript (print: window.intersection($a1, $a2))
Which is really ugly and, as greyelf mentioned, could easily break in future versions of Harlowe.
OTOH for your example of calculating the intersection of 2 arrays. It's possible without using any javascript. Since $a1 - $a2 returns all the elements of $a1 that aren't in $a2, you can get the intersection by just doing $a1 - ($a1 - $a2)
I'm not sure if I agree with that approach (and I may be misquoting their views or ascribing another commentator's views to them out of a faulty recollection), since I personally don't see the negatives in optional extras and always saw the claim that Twine involves "no programming" a bit dubious anyway (even simple if-else statements or html markup is programming).
And then people tend to use hacky workarounds to get things happening, because that's easier than learning a more customisable format from scratch.
But it is the way Harlowe has been designed.