1
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Not in the traditional sense. What you can do is create a passage with the code you want in it, then (display:) the passage. You'll need to create a variable to pass information to the passage if it needs to evaluate something.

A couple examples:

A passage named "func:hero health" which displays a variable (with a text effect):

{(text-style: "emboss")[(print: $heroHealth's (round: $heroHP))]}

Called with

(display: "func:hero health")

For something more complex, a menu item which takes the player to the version history document (used as a menu item):

{(text-colour: "$gameColorCodes's w3-pink")[(link: $menuPassageNameForGoTo)[
(if: (passage:)'s name is 0)[
    (set: $gamePreviousPassage to "main")]
(else:)[
    (set: $gamePreviousPassage to (passage: )'s name)]
(goto: $menuPassageNameToGoTo)
    ]
]}

Which I call and pass variables to with:

{(set: $menuPassageNameToGoTo to "doc:version history")
(set: $menuPassageNameForGoTo to "Version History")
(display: "func:left menu goto passage")}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .