Advertisement
Sunspider

Twine Persistent Vars Javascript

Jan 4th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. // COMMANDS TO STORE OR RETRIEVE PERSISTENT VARIABLES:
  2. // ===================================================
  3. // To store persistent variables from this Twine:
  4. // -- <<run setup.saveMemory()>>
  5. //
  6. // To retrieve stored persistent variables from this Twine:
  7. // -- <<set $store to setup.loadMemory()>>
  8. //
  9. // To remove this Twine's persistent variables from local storage:
  10. // -- <<set $store to setup.eraseMemory()>>
  11. //
  12. // To clear all persistent variables from this domain:
  13. // -- <<set $store to setup.eraseAll()>>
  14. //
  15. // NOTE: you could probably tweak the functions below, to take a string parameter for the Key,
  16. // if you wanted to save/load in multiple places in Local Storage. Persistent variables from
  17. // different Twines will probably want unique Keys, and some means to load them.
  18. // For now, the Local Storage key is 'example-memory'.
  19.  
  20. // GENERAL OVERVIEW:
  21. // =================
  22. // This was created to pass variables between Twines/web games, using Local Storage and JS.
  23. // It does this using a JS object called $memory, which contains all variables intended to
  24. // be passed between/accessed in other games. The memory object is then parsed via JSON
  25. // and stored in Local Storage, for later retrieval.
  26. //
  27. // NOTE: Not all Twine variables are stored, only variables within the $memory object.
  28. //
  29. // In the first passage of the Twine, run the setup.loadMemory() JS function. This will
  30. // retrieve the $memory object from local storage. I'm not sure if it's necessary, but
  31. // I also initialized the $memory object with the variables I expected it to have, and
  32. // default values, so it'd have those to fall back on if there was nothing in Local Storage.
  33. //
  34. // Whenever persistent variables are changed in a passage, run the setup.saveMemory() function,
  35. // to update local storage with the new values.
  36. //
  37. // To access variables from the memory object in a passage, use $memory.<VARIABLE NAME>
  38.  
  39. // LIMITATIONS:
  40. // ============
  41. // I never tested/fully implemented retrieving Local Storage across domains, but I've
  42. // read that it's doable using iframes, and messages/event listeners. Load Local Storage
  43. // in the iframe, and use messages to pass data from the child iframe to the parent.
  44. // No clue if/how Local Storage can be accessed from other browsers though.
  45. //
  46. // Reading and writing to Local Storage can be fussy sometimes, especially if I wanted to
  47. // modify how many variables were in the $memory object. A few times during dev the data
  48. // got corrupted/into a state where I couldn't read or write to it. Clearing local storage
  49. // using setup.eraseAll() seemed to fix it.
  50.  
  51. setup.saveMemory = function () {
  52. var store = JSON.stringify(State.variables.memory);
  53. try {
  54. localStorage.setItem('example-memory', store);
  55. } catch (e) {
  56. UI.alert('Local storage is inaccessible to save to!');
  57. console.log(e);
  58. }
  59. };
  60.  
  61. setup.loadMemory = function () {
  62. try {
  63. var store = localStorage.getItem('example-memory');
  64. return JSON.parse(store);
  65. } catch(e) {
  66. UI.alert('Local storage is inaccessible to load from!');
  67. console.log(e);
  68. }
  69. };
  70.  
  71. setup.eraseMemory = function () {
  72. try {
  73. var store = localStorage.removeItem('example-memory');
  74. return;
  75. } catch(e) {
  76. UI.alert('Local storage is inaccessible to erase!');
  77. console.log(e);
  78. }
  79. };
  80.  
  81. // Clears ALL local storage persistent variables
  82. setup.eraseAll = function () {
  83. try {
  84. var store = localStorage.clear();
  85. return;
  86. } catch(e) {
  87. UI.alert('Local storage is inaccessible to erase all!');
  88. console.log(e);
  89. }
  90. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement