Search code examples
yaml-cpp

YAML root node !Null but size is 0


I'm new to YAML and trying to get an example up and running. I have the Sprites_list YAML file from http://www.gamedev.net/page/resources/_/technical/apis-and-tools/yaml-basics-and-parsing-with-yaml-cpp-r3508 and the root/base node is always NOT Null but the size is Always 0, the type is Scalar, and trying to access a node throws a YAML::BadSubscript exception. Line 118 in impl.h of Yaml 0.5.3. Why does the root node have a 0 size and why isn't accessing a node possible?

YAML::Node root_node_ = YAML::Load( file );

if( root_node_.IsNull() )
    {
    // Never entered
    }

int sz = root_node_.size(); // Always 0
YAML::Node a_node = root_node_[ "Sprites_List" ]; // Exception

Edit--> Full file contents ( pasted into a file called sprites.yml )

Sprites_List: [Player, Monster, Gem]
Player:
    SpriteSheet: /Resources/Textures/duotone.png
    Anim_Names: [run, idle, jump, die]
    run:
        Offset: {x: 0, y: 0}
        Size: {w: 32, h: 32}
        Frame_Durations: [80, 80, 80, 80, 80, 80]
    idle:
        Offset: {x: 0, y: 32}
        Size: {w: 32, h: 32}
        Frame_Durations: [80, 120, 80, 30, 30, 130] #Notice the different durations!
    jump:
        Offset: {x: 0, y: 64}
        Size: {w: 32, h: 32}
        Frame_Durations: [80, 80, 120, 80, 80, 0] #Can I say 0 mean no skipping?
    die:
        Offset: {x: 0, y: 192} #192? Yup, it is the last row in that sheet.
        Size: {w: 32, h: 32}
        Frame_Durations: [80, 80, 80, 80, 80] #this one has only 5 frames.
Monster: #lol that lam nam
    SpriteSheet: /Resources/Textures/duotone.png
    Anim_Names: [hover, die]
    hover:
        Offset: {x: 0, y: 128}
        Size: {w: 32, h: 32}
        Frame_Durations: [120, 80, 120, 80]
    die:
        Offset: {x: 0, y: 160}
        Size: {w: 32, h: 32}
        Frame_Durations: [80, 80, 80, 80, 80]
Gem:
    SpriteSheet: /Resources/Textures/duotone.png
    Anim_Names: [shine]
    shine:
        Offset: {x: 0, y: 96}
        Size: {w: 32, h: 32}
        Frame_Durations: [80, 80, 80, 80, 80, 80]

In class declaration -

YAML::Node root_node_;

In class definition -

CConfigFile( std::string const & file ) :
    root_node_      ( YAML::Load( file ) )

The file path is the full absolute path and it's escaped with \\ for each \.


Solution

  • YAML::Load loads a YAML string, not a file. To load a file, you need to use YAML::LoadFile.