How can I communicate/persist pure Javascript objects between passages?

0 votes
asked May 16 by CresCoJeff (170 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
answered May 16 by Akjosch (7,310 points)
selected May 16 by CresCoJeff
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.
commented May 17 by greyelf (97,410 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
answered May 16 by Chapel (51,470 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?
commented May 16 by CresCoJeff (170 points)
is there a functional difference between non-temporary variables attached to the state and story variables?
commented May 17 by Chapel (51,470 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.