Add-ons / File System Macro Set

A set of macros for importing and exporting data to the user's hard drive, or for importing exported saved games into different games.

Author ChapelR
Website https://twinelab.net/custom-macros-for-sugarcube-2/#/file-system-macros
Story format SugarCube 2
Last checked Sun Jun 28 2020
License Unlicense
Download fs.zip

Index

Overview

A set of macros for importing and exporting data to the user's hard drive, or for importing exported saved games into different games.

Macros

<<export>>

<<export>>

Syntax: <<export data fileName [dataType]>>

The <<export>> macro is a way to save a little bit of data on your user's hard drive that exists outside of SugarCube's normal save systems. This macro may be appropriate for certain very specific use cases, but generally having users export saves is better. It is largely included here for completeness.

Arguments:

Usage:

<<set $player to { name : 'Bob', Level : 10 }>>

<<link 'Export Player Data'>>
    <<export $player 'player-info.twinedata' 'json'>>
<</link>>

<<import>>

<<import>>

Syntax: <<import variableName [dataType] [linkText]>>

The <<import>> macro can be used to import some data you've previously saved to the user's hard disk. This macro will create a button that, when clicked, will prompt the user to select a file. That file will be read and the data stored in the indicated variable. You can also import SugarCube 2 saved data and access it without loading it, such as to create an episodic game, or let users import characters or choices into sequels. To import SugarCube 2 saves, use the base64 data type option, and take a look at the Save API to get a handle on the data structure.

📝 Note: If the data is not able to be successfully loaded or any errors are encountered, the data sent to the variable will be null.

Arguments:

Usage:

<<import '$player' 'json' 'Import Player Info'>>

<<import '$oldGame' 'base64' 'Import Saved Data'>>

Events

The :import-macro synthetic event is fired after the data is loaded, whether the load was successful or not. This event object has the following properties:

Live demo

Demo Twee code:

:: Start
<<set $export to { name : 'Bob', level : 10 }>>\
<<button 'Export'>>
    <<export $export 'player-stats' 'json'>>
<</button>>