0 votes
by (420 points)
I'd like to be able to define Javascript objects in the Story Javascript or in <<script>> tags and then have access to those objects in <<script>> tags of subsequent passages; I'm not necessarily looking to create globals, but that could work, though ideally it would be possible to do from within <<script>> tags rather than requiring the <<set>> macro.   The ideal thing would be a communication mechanism that allows me to keep an object from going out of scope (or clones it into the new scope) between <<script>> tags and across passages -- is that possible?

2 Answers

+1 vote
by (8.6k points)
selected by
Best answer
Within <<script>> passages, you have access to the current state variables (via the object which variables() returns) and temporary variables (via temporary() return value). You can both read and write objects and values in there and if you use the state variables, those (or rather, deep clones of them) will persist in future passages.
by (158k points)

If you don't need your custom Javascript Object to be persisted in the History or Save systems then I would suggest storing it on the built in setup object which is also available in all the places that you can access the story & temporary variables from.

+1 vote
by (63.1k points)
The issue, even with globals, is that it nothing but story variables will actually persist across page reloads and save games. Why can't you just use the state?
by (420 points)
is there a functional difference between non-temporary variables attached to the state and story variables?
by (63.1k points)
Not really. Anything that the State persists is probably going to at least functionally be a story variable.
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.