Persistent Data Recommendation
-
I have a few buttons on my UI and I want their data to persist before and after the UI is constructed/deconstructed. Just wondering what the best folder to dump JSON Data into would be? I imagine putting it in the AppData folder that is created would be the best but I am open to recommendations. Currently I am just saving it to the Expansions folder for no particular reason. Also any recommendations on best practices here is welcome!
const var STATE_FILENAME = "buttonStates.json"; const var btns= Content.getAllComponents("Button"); const var stateFilePath = FileSystem.getFolder(FileSystem.Expansions).getChildFile(STATE_FILENAME); // Presumably just change this to FileSystem.AppData /** Save button states to file */ inline function saveStates(states) { Engine.dumpAsJSON(states, stateFilePath.toString(0)); return true; } /** Load button states */ inline function loadButtonStates() { if (stateFilePath.isFile()) { return Engine.loadFromJSON(stateFilePath.toString(0)); } return {}; } /** Save state for a specific button */ inline function saveButtonState(buttonId, state) { local states = loadButtonStates(); states[buttonId] = state; return saveStates(states); } /** Get state for a specific button */ inline function getButtonState(buttonId, defaultValue) { local states = loadButtonStates(); if (isDefined(states[buttonId])) { return states[buttonId]; } return defaultValue; } /** Button callback function */ inline function onButtonControl(component, value) { // Use component's ID as the button ID local id = component.getId(); saveButtonState(id, value); } /** Initialize buttons on startup */ inline function initializeButtons() { for (i = 0; i < btns.length; i++) { local id = btns[i].getId(); local savedState = getButtonState(id, 0); btns[i].setValue(savedState); } } // Set up callbacks for all buttons for (i = 0; i < btns.length; i++) { btns[i].setControlCallback(onButtonControl); } // Initialize buttons initializeButtons(); }
-
@HISEnberg said in Persistent Data Recommendation:
before and after the UI is constructed/deconstructed.
What do you mean by this?
-
@d-healey For each instance when the plugin is loaded (initiated)*
-
@HISEnberg Writing it to a file is the correct method, I'd put it in the app data folder. I'd use the File/FileSystem APIs rather than
dumpAsJSON
.Do you need special handling if there are multiple instances of the plugin in use?
-
@d-healey said in Persistent Data Recommendation:
Do you need special handling if there are multiple instances of the plugin in use?
That is the question that made me abandon this idea... Gosh... You have a solution for this?
-
@ustk I've seen someone else came up with a solution to give each instance a temporary unique id per session, but I haven't explored it myself.
-
@d-healey Wonderful I'll see if I can rewrite with just the File (System) APIs in that case! Luckily for me these are "universal settings" so they should be applied cross each instance of the plugin.
-
H HISEnberg has marked this topic as solved