Last active
August 22, 2020 02:21
-
-
Save yearofthewhopper/255e3192f57c0461f3dd82f43eade6ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================== | |
// The following example demonstrates how to store, retrieve and remove data | |
// across multiple sessions. | |
// | |
// Project setup: | |
// - Insert two rectangles | |
// - Insert text | |
// - Position the rectangles and text so that all are visible | |
// - Add 'data' to 'Whitelisted keys' under the 'Persistence' Capability | |
// - Add the 'Tap Gesture' capability under 'Touch Gestures' | |
//============================================================================== | |
// Load in the required modules | |
const Persistence = require('Persistence'); | |
const Scene = require('Scene'); | |
const TouchGestures = require('TouchGestures'); | |
// Enable async/await in JS [part 1] | |
(async function() { | |
// Locate the rectangles and text in the Scene | |
const [storeRectangle, removeRectangle, dataText] = await Promise.all([ | |
Scene.root.findFirst('rectangle0'), | |
Scene.root.findFirst('rectangle1'), | |
Scene.root.findFirst('2dText0') | |
]); | |
// Store a reference to the userScope | |
const userScope = Persistence.userScope; | |
// Create a JavaScript object to store the data | |
const data = { name: 'Spark AR' }; | |
//============================================================================== | |
// How to get stored data | |
//============================================================================== | |
try { | |
// Attempt to get the stored data and if successful... | |
const result = await userScope.get('data'); | |
// Output a success message with the data added | |
dataText.text = 'Successfully retrieved data ' + result.name; | |
} catch (error) { | |
// If not successful output a failure message with the error returned | |
dataText.text = 'Failed to retrieve data, ' + error; | |
} | |
//============================================================================== | |
// How to store data | |
//============================================================================== | |
// Subscribe to tap gestures on the storeRectangle | |
TouchGestures.onTap(storeRectangle).subscribe(async () => { | |
try { | |
// Attempt to store the data and if successful... | |
await userScope.set('data', data); | |
// Output a success message | |
dataText.text = 'Successfully stored'; | |
} catch (error) { | |
// If not successful output a failure message with the error returned | |
dataText.text = 'Failed to store, ' + error; | |
} | |
}); | |
//============================================================================== | |
// How to remove stored data | |
//============================================================================== | |
// Subscribe to tap gestures on the removeRectangle | |
TouchGestures.onTap(removeRectangle).subscribe(async () => { | |
try { | |
// Attempt to remove the data and if successful... | |
await userScope.remove('data'); | |
// Output a success message | |
dataText.text = 'Successfully removed'; | |
} catch(error) { | |
// If not successful output a failure message with the error returned | |
dataText.text = 'Failed to remove, ' + error; | |
} | |
}); | |
// Enable async/await in JS [part 2] | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment