+3 votes
by (550 points)
I'm starting to learn Twine and I'm putting together a little framework for making a game.

I'd like to be able to store functions or procedures somehow so that I can use them over and over again without having to write the whole thing each time.

Example[Something like this]: (set: $incrementItemsAmount to (set: _item's amount to it + 1))

I've looked at lambda macros, but they look like they're more for looped procedures.

1 Answer

+3 votes
by (144k points)
selected by
 
Best answer

Harlowe currently has no built-in method for adding custom TwineScript based macros, the best you do is to place that code in a child passage and then use the (display:) macro to 'execute' that code.

The following example is written using Twee Notation.

:: Parent Passage
(set: $length to 10)
(set: $width to 10)
(display: "Calculate Area")

area: $area


:: Calculate Area
(set: $area to $length * $width)

One downside of the above technique is that any visual output of the child passage will be added to the visual output of the parent passage, this includes any line-breaks used to format the child passage's code. One way to get around this side-effect is to wrap the (display:) macro call in a named hook and then use CSS to hide all visual contents of that named hook.

a. Change the (display:) macro call to something like the following:

|macro>[(display: "Calculate Area")]

b. Add CSS like the following, that uses a selector based on the above macro named hook, to the Story Stylesheet area of your story.

tw-hook[name="macro"] {
	display: none;
}

 

by (550 points)

Great answer! Would it be okay with you if I copied it over to stack exchange? Here

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...