Plugin state with ValueTree

Hi !

Im trying to store [ValueTree] content (contains only simple data) in a plugin state. Having hard time here as there is no data to be recalled after all .

Trying to search the forum ,but cant find any clue so far .


Here is the code im testing at the moment .


ValueTree temp("DataOfSomeSort");
const Identifier key = "cell0";
void Processor::getStateInformation(MemoryBlock& destData) {
    temp.setProperty(key, 199, nullptr);
    ScopedPointer<XmlElement> e = temp.createXml();
    copyXmlToBinary(*e.get(), destData);
void Processor::setStateInformation(const void* data, int sizeInBytes) {
    ScopedPointer<XmlElement> xml(getXmlFromBinary(data, sizeInBytes));
    if(xml == nullptr) return;
        DBG("has no property of a given name");
    DBG("num childs" << temp.getNumChildren()); // yields 0
    DBG("num props" << temp.getNumProperties()); // yields 0


Could someone share some hints about the proper way of serialising and storing [ValueTree] in a plugin state ?

getNumChildren is returning 0 because you have not added a child node to the tree.

Have you tried adding a child then setting the property on the child node?

Use XmlElement->writeToFile to check if you're actually creating something. 

I get your frustration with the ValueTree - it took me a while, and some trial and error, to really get to grips with it. 

I think a tutorial on best practice for use of the ValueTree would be a valuable addition. 


Hi Mark !

I have still a lot of sympathy for [ValueTree], however frustration may come really soon  :D

yeah ive tried (writeToFile) and data is definitelly there ...

actually ive spoted the mistake few minutes ago.


i should assign whats returned (fromXml)

temp = ValueTree::fromXml(*xml);



... this way it works as expected, just with one additional symptom ... all is turned into a [Strings], so i think i need different approach ,as rebuilding [ValueTree] node by node myself is the last thing i would go for if i have to ... 



The best way to store a ValueTree for this kind of purpose is as binary, with writeToStream()

BTW I'd really recommend using AudioProcessorValueTreeState instead - we did a lot of hard work in there for you already!

hah ! good to know Jules 

... are there any resources i can learn from about [AudioProcessorValueTreeState] ?

Don't think we have an example for it yet, you'll just need to read the docs and look at the class.

yes i will , thank you 

ah ok ,its a class that manages parameters state ...

well my [ValueTree] is a data structure that holds various datas about many aspects of the plugin behaviour, most of them are not related to [GUI] directly , and its not about to be exposed as a plugin parameter interface for a daw ... the data is interpreted in the process while performing and distributed conditionally. 

It work nicely with data streams 

ValueTree temp("DataOfSomeSort");
const Identifier key = "cell0";
void Processor::getStateInformation (MemoryBlock& destData){
    temp.setProperty(key, 199, nullptr);
    MemoryOutputStream ms;
    destData = ms.getMemoryBlock();
void Processor::setStateInformation (const void* data, int sizeInBytes){
    if(sizeInBytes == 0) return;
    MemoryInputStream is(data,sizeInBytes,true);
    temp = ValueTree::readFromStream(is);