YAML > XML

Random irrelevance that just didn't fit into other forums. Talk about anything.

Moderator: Talkative People

K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: YAML > XML

Post 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.
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: YAML > XML

Post 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.
Image
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: YAML > XML

Post 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.
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: YAML > XML

Post 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.
I'll make your software hardware.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: YAML > XML

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: YAML > XML

Post 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?
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: YAML > XML

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: YAML > XML

Post 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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: YAML > XML

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: YAML > XML

Post 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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: YAML > XML

Post 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;
	}
}
Post Reply