How to manually save/load different value trees?

Hi, I am aiming to create the ability to load and save presets manually in the form of value trees for my vst. I am already using getStateInformation and setStateInformation which appears to load and save the same xml file (I think). I would like to be able to save the current state into a new file with some type of identifier upon a button press, and then be able to load back in a state given an identifier with another button press.

How can I save a value tree state manually?
How can I choose the file path for the value tree state?
How can I load a specific value tree state manually?

Any help or insight is greatly appreciated!

If I’m not mistaken, you can read and write a value tree with something along the lines of (not tested) :

juce::File file("/path/to/file");

// writing
juce::ValueTree myValueTree{"someID"};
juce::FileOutputStream outstream(file);
myValueTree.writeToStream(outstream);

// reading
juce::FileInputStream in(file);
if (in.openedOk())
    auto newValueTree = juce::ValueTree::readFromStream(in);
1 Like

That was exactly what I was looking for, thank you!

The method above will save the ValueTree as binary data in the selected file. If you’d rather save it as XML, you can use something like:

juce::ValueTree myValueTree { "myID" };
juce::File myFile ("/Path/To/File");

// writing
if (auto xml = myValueTree.createXml())
    xml->writeTo (myFile);

// reading
if (auto xml = juce::XmlDocument::parse (myFile))
    myValueTree = juce::ValueTree::fromXml (*xml);
3 Likes