YAML > XML
Posted: Sun Dec 06, 2009 8:59 pm
Anyone who wants to use XML should consider a short look at YAML with libyaml-cpp. It's extremely easy to use (from my game):
Use:
Just wanted to share this. I just discovered it one hour ago
Factory method:rats.yaml wrote: -
name: "Small Rat"
model: "hueteotl"
texture: "rat.png"
scale: 2.0
maxhealth: 15.0
maxmana: 0.0
maxvelocity: 20.0
-
name: "Big Rat"
model: "hueteotl"
texture: "rat.png"
scale: 3.0
maxhealth: 25.0
maxmana: 0.0
maxvelocity: 20.0
Code: Select all
KMEntity* EntityFactory::CreateEnemy(const std::string& script, const std::string& type, const ggut::Vector2f& pos)
{
//TODO: KMEnemy
KMEntity* temp = new KMEntity();
std::ifstream fin(script.c_str());
YAML::Parser parser(fin);
YAML::Node doc;
parser.GetNextDocument(doc);
try
{
for(std::size_t num = 0; num < doc.size(); ++num)
{
if(doc[num]["name"] == type)
{
temp->SetName(type);
temp->SetMaxHealth(doc[num]["maxhealth"]);
temp->SetHealth(doc[num]["maxhealth"]);
temp->SetMaxMana(doc[num]["maxmana"]);
temp->SetMana(doc[num]["maxmana"]);
temp->SetModel(doc[num]["model"]);
temp->SetPosition(pos);
temp->SetScale(doc[num]["scale"]);
temp->SetMaxVelocity(doc[num]["maxvelocity"]);
}
}
}
catch( ... )
{
std::cerr << "Failed to load Enemy from YAML file!" << std::endl;
}
return temp;
}
Code: Select all
myRat = myEntityFactory.CreateEnemy("entities/rats.yaml", "Small Rat", Vector2f(150, 150));