Game Development Reference
In-Depth Information
number of engine classes in any container at runtime and use standard STL iterators
to loop through them inside the logic code, maintaining zero dependency with the
game data (Listing 20.6).
struct float3 { float x, y, z;};
struct Player
{
std::string name;
unsigned int life;
float3 position;
};
void operator>>(const Node &n, float3 &f3)
{
assert(n.GetTag() == "!float3"); // optional type-check
n[0] >> f3.x; n[1] >> f3.y; n[2] >> f3.z;
}
void operator>>(const Node &n, Player &p)
{
assert(n.GetTag() == "!Player"); // optional type-check
n["name"] >> p.name;
n["life"] >> p.life;
n["position"] >> p.position;
}
int main(int argc, char **argv)
{
std::ifstream f(argv[1]); Config cfg(f);
std::vector<Player> players = cfg["players"];
return 0;
}
Listing 20.6. Sample C++ code for the deserialization of Player and float3 objects.
Stronger type checking can be enabled with YAML's tagging feature. Each
scalar can be assigned an arbitrary number of tags, preceded by an exclamation
players:
- !Player
name: Player 1
life: 100
position: !float3 [1.0, 0.0, 0.0]
- !Player
name: Player 2
life: 80
position: !float3 [0.0, 1.0, 0.0]
Listing 20.7. Sample YAML description of Player and float3 objects.
Search WWH ::




Custom Search