Plugin state is not restored from a ValueTree

Hi!

I am using functions getStateInformation and setStateInformation to save and restore plugin parameters. The issue is that in getStateInformation a parameter is saved correctly, but it is not restored in setStateInformation call.

I am using ValueTree class.

Here is the code of these two methods:

void NewProjectAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
    // You should use this method to store your parameters in the memory block.
    // You could do that either as raw data, or use the XML or ValueTree classes
    // as intermediaries to make it easy to save and load complex data.

    std::string currentInstanceId = current_instance_information.id;

    // return if nothing to store
    if (currentInstanceId == "")
        return;

    String testValue = "this is the test value";
    pluginParameters.setProperty("testValue",  testValue, nullptr);
    String val = pluginParameters["testValue"]; // "this is the test value" (correct value)
    std::unique_ptr<juce::XmlElement> xml (pluginParameters.createXml());
    pluginParameters.fromXml(*xml);
    val = pluginParameters["testValue"]; // "this is the test value" (correct value)
    copyXmlToBinary(*xml, destData);
}
void NewProjectAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
    // You should use this method to restore your parameters from this memory block,
    // whose contents will have been created by the getStateInformation() call.

    std::unique_ptr<juce::XmlElement> xml (getXmlFromBinary (data, sizeInBytes));
    pluginParameters.fromXml(*xml);
    String ip = pluginParameters["testValue"]; // "" (empty value)
}

What am I doing wrong?

I think this is an actually a very simple mistake using c++:
ValueTree::fromXml is a static method returning the newly parsed ValueTree. Try using pluginParameters = ValueTree::fromXml(*xml). This should actually be pointed out by your IDE – you might consider switching it. Sadly, there is no compiler warning for this as the standard explicitly allows that.

Your check in getStateInformation is busted, because fromXml does nothing but the property is still set from calling setProperty earlier.

1 Like

@Rincewind Thank you for your help. Works. Now I have a bit more knowledges about static methods.