Peel off an ofxYAML node

Greetings,

I’m using ofxYAML to load some information from a config file. I can’t figure out how to peel off one node of the YAML tree to pass as an argument to a function. For example, I want to do something like this:

ofxYAML config;
config.load("config.yaml");

int n_modules = 5;

for(int i = 0; i < n_modules; i++){
    ofxYAML child = config["module-" + ofToString(i)];
    modules[i]->loadState(child);
}

The error I get says No viable conversion from 'YAML::Node' to 'ofxYAML'

Any guidance is much appreciated!

Thanks,

Ted

hello! not sure what your modules are and what the loadState() method expects, but it’s probably not an ofxYAML? (which is a complete instance of the file including a filepath, in which case you should pass the whole “config”).

the error message tells you you are getting a YAML::Node, which is perhaps what you want to pass along? (assuming it could be a tree in itself?) then you can continue to process the ::Node further within the modules.

Hi @burton,

Thanks for your help.

Helped me out–I had tried changing the type that loadState() expects to be an ofxYAML::Node and that didn’t work–but your clarification helped me realize that my (not shown) function saveState() was returning an ofxYAML instance, when I actually want it to return an ofxYAML::Node instance!

I’ve now made my saveState() return a ofxYAML::Node which has solved this for me. So now I have in the modules’ class:

ofxYAML::Node saveState(){
    ofxYAML::Node dict;
    // add stuff...
    return dict;
}

and in my ofApp.cpp:

ofxYAML config;
config.load("config.yaml");

int n_modules = 5;

for(int i = 0; i < n_modules; i++){
    ofxYAML::Node child = config["module-" + ofToString(i)];
    modules[i]->loadState(child);
}

Thank you!