Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Custom macros in Harlowe?

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

  • The developer of Harlowe (Leon) has not yet released any documentation or examples on how to add custom macros and the story format's engine is designed/built in a way to make extending it difficult.

    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.
  • Unfortunately Harlowe is designed so that the Macros object isn't accessible from within the story's javascript. So there doesn't seem to be any way to add any macros.

    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
    window.intersection(a1, a2) {
    //return the intersection
    }
    

    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)
  • Thank you! It's been a while since doing set arithmetic haha, didn't even think about that. :P
  • It's been a year. Has there been any news about adding custom macros?
  • It's been a year. Has there been any news about adding custom macros?
    No
  • edited August 2016
    I might have read somewhere that the Harlowe format was designed very much as a "walled garden" format, as they didn't want it to become dependent on external programming in the way that some other formats have evolved.

    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.
  • Claretta wrote: »
    I might have read somewhere that the Harlowe format was designed very much as a "walled garden" format, as they didn't want it to become dependent on external programming in the way that some other formats have evolved.
    Another reason could be to reduce the amount of support and hands-on instruction the developer would need to give if they increased the level of complexity of the story format's feature set/API.
Sign In or Register to comment.