Storage

App storage is where you keep the app data inside Spaces.

How to Store Data in App Storage

Recommended

App.setStorage(string);

The App.setStorage function is a data storage function complementing existing app data storage methods.

Not recommended

App.storage = string; App.save();

If the app is running on several maps in the same Space, the above method may cause issues such as data being overwritten.

How to Read the App Storage Value

App.getStorage(function(){})

The App.getStorage function helps synchronize the app data so that it can have the same data by checking if the same app is running on another map within the same Space.

As the App.getStorage function is an asynchronous function, synchronization cannot be guaranteed if you add a code that uses App.storage on the very next line of this function.

App Storage Example

Install the example code below as a sidebar app and press Q on another map in the same Space to see if the "count" value is synchronized.


App.onStart.Add(function(){
	if(App.storage == null){
		App.setStorage(JSON.stringify({count: 0}))
	}
})
// // Activates function when q is pressed
App.addOnKeyDown(81,function(player){
	// Updates App.storage and executes a callback function
	App.getStorage(function () {
		let appStorage = JSON.parse(App.storage);
		appStorage.count += 1;
		App.sayToAll(`count: ${appStorage.count}`)
		// Uses App.setStorage to save any changes
		App.setStorage(JSON.stringify(appStorage));
	});
	// Synchronization not guaranteed if you add a code that uses App.storage on the very next line of the App.getStorage function.
	App.sayToAll(App.storage);
})

Last updated