Page 2 of 2

Re: YAML > XML

Posted: Tue Mar 02, 2010 2:45 am
by K-Bal
You could do some thing like

Code: Select all

-
 line1: [ 0 1 4 2 5 ]
...
or

Code: Select all

-
 sizeX: 5
 tile1: 1
 tile2: 5
...
But I did not test this. Just try it out and report your experience.

Re: YAML > XML

Posted: Tue Mar 02, 2010 2:49 am
by Live-Dimension
As far as I know, most maps are in some sort of text-based format due to limitations of stuff like XML and YAML. It's simply inefficient to encode a maze/platform in something like XML - there is alot of wasted text. However if you wanted to you could do something like K-Bal suggested.

Re: YAML > XML

Posted: Tue Mar 02, 2010 11:19 am
by K-Bal
Pure XML/YAML is fine for maps if the memory consumption of your files does not matter i.e. on a PC.

Re: YAML > XML

Posted: Tue Mar 02, 2010 12:24 pm
by thejahooli
K-Bal wrote:

Code: Select all

	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"]);
		}
	}
Do you find it efficient enough to loop through the entire YAML document each time you need to find an enemy. I was thinking of doing this but I'm not sure whether it will be efficient enough.

Re: YAML > XML

Posted: Tue Mar 02, 2010 1:05 pm
by K-Bal
Not so good if you are dynamically creating enemies, which is not the case in my game. You could save an enemy preset each time an enemy is loaded and on further requests just return a clone.

Re: YAML > XML

Posted: Mon Mar 08, 2010 11:35 am
by Falco Girgis
thejahooli wrote:
K-Bal wrote:

Code: Select all

	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"]);
		}
	}
Do you find it efficient enough to loop through the entire YAML document each time you need to find an enemy. I was thinking of doing this but I'm not sure whether it will be efficient enough.
Wait, wait, wait. I'm confused as to what you're doing with this.

Are you not loading each enemy into a class/structure then just referring to them in memory later? Shouldn't loading be a one-time deal?

Re: YAML > XML

Posted: Mon Mar 08, 2010 11:44 am
by K-Bal
GyroVorbis wrote:Are you not loading each enemy into a class/structure then just referring to them in memory later? Shouldn't loading be a one-time deal?
It should be. This was just a first quick'n'dirty implementation from my game.

Re: YAML > XML

Posted: Mon Mar 08, 2010 11:55 am
by Falco Girgis
K-Bal wrote:
GyroVorbis wrote:Are you not loading each enemy into a class/structure then just referring to them in memory later? Shouldn't loading be a one-time deal?
It should be. This was just a first quick'n'dirty implementation from my game.
Oh, right, right. I wasn't looking at the original code to see that it was from a LoadEntity() function. I just saw it quoted and thejahooli asked about looping through a whole file to get an entity.

thejahooli, he's loading all of the entities at one time, so if he wanted to "find one," it's a matter of iterating through his entity list to find a particular one, not accessing the file.

This kind of thing is perfectly fine for most applications, since the processing is only going on when the level/file is being loaded. After that, it's not like you're still referring back to an XML/YAML file.

Re: YAML > XML

Posted: Mon Mar 08, 2010 12:35 pm
by K-Bal
Yeah, but if I create two enemies, it will iterate two times through the document. For better performance you would buffer the enemy template the first time and create copies on further requests. However, this is faster, but requires more memory for buffering the enemy templates.

Re: YAML > XML

Posted: Mon Mar 08, 2010 12:48 pm
by Falco Girgis
K-Bal wrote:Yeah, but if I create two enemies, it will iterate two times through the document. For better performance you would buffer the enemy template the first time and create copies on further requests. However, this is faster, but requires more memory for buffering the enemy templates.
Oh, I gotcha.

Yeah, in ES, we are loading "EnemyAttributes" so that if you were to construct one, it's common data is already loaded into a structure, and the enemy is just given a pointer to the attributes.

I see what you guys are saying.

Re: YAML > XML

Posted: Fri Mar 12, 2010 6:22 am
by K-Bal
Another example. I use a simple (2d) map format which consists of static boxes as boundaries and spawn areas for players.

Example map:
-
type: StaticBox
Rect: [0, 10, 0, 200]

-
type: StaticBox
Rect: [0, 500, 200, 210]

-
type: SpawnArea
Rect: [20, 400, 20, 150]
Now, I define myself an operator>> for Rects:

Code: Select all

const YAML::Node& operator>>(const YAML::Node& node, sf::FloatRect rect)
{
	node[0] >> rect.Left;
	node[1] >> rect.Right;
	node[2] >> rect.Top;
	node[3] >> rect.Bottom;
	return node;
}
And parse the document as follows:

Code: Select all

void SceneManager::LoadMapFromYAML(const char* filepath)
{
	std::ifstream fin(filepath);
	YAML::Parser parser(fin);

	YAML::Node doc;
	parser.GetNextDocument(doc);

	StaticBox tempBox;
	sf::FloatRect tempSpawn;

	myCurrentMap = new Map;

	if(!myCurrentMap)
		return;

	try
	{
		for(std::size_t num = 0; num < doc.size(); ++num)
		{
			if(doc[num]["type"] == "StaticBox")
			{
				doc[num]["Rect"] >> tempBox.Bounds;
				myCurrentMap->Bounds.push_back(tempBox.Bounds);
			}
			else if(doc[num]["type"] == "SpawnArea")
			{
				doc[num]["Rect"] >> tempSpawn;
				myCurrentMap->Bounds.push_back(tempSpawn);
			}
		}
	}
	catch( ... )
	{
		std::cerr << "Failed to load map." << std::endl;
		delete myCurrentMap;
		myCurrentMap = 0;
		return;
	}
}