Loading a level from a file XML/

Started by
7 comments, last by swiftcoder 16 years, 1 month ago
Hallo, Im working on a small top down game which uses a lot of tiles to represent the game world. I want to be able to load a map from a file so I looked into XML as it seams like a popular candidate. Does this format look ok?

<Game>
	<Settings>
		<screenHeigh = "800"/>
		<screenWidth = "600"/>
	</Settings>

	<Level 1>
		<Tile>
			<fileLocation = "Data//tile_1.tga" xPos = "0" yPos = "0"/>
			<fileLocation = "Data//tile_2.tga" xPos = "1" yPos = "2"/>
		</Tile>

		<CollisionBox>
			<xPos = "10" yPos = "10" width = "5" height = "5"/>
			<xPos = "15" yPos = "15" width = "5" height = "5"/>
		</CollisionBox>

		<Item>
			<Gun = "mp5" xPos = "5" yPos = "5"/>
			<Health = "50hp" xPos = "5" yPos = "6"/>
		</Item>
	</Level 1>
</Game>



Advertisement
I wouldn't try to keep all your game's data in one big xml file, that'll just get unweildy pretty quickly. Game settings for example (like your screen width/height) are user configurable and so should probably be in their own file. Similarly I'd put each level in their own file and have a master xml list which defines their order and has a relative file path to the actual level xml so you can just load in a single level instead of having to parse all the levels just to get the next one.
Ah, thats a very good idea. But is the syntax so far right?
Quote:Original post by h3ro
Ah, thats a very good idea. But is the syntax so far right?

It looks ok, but it really depends on what you want it to do. As a note, most people editing your xml files by hand would prefer that you use standard naming conventions, such as 'src' for the path to a file.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by OrangyTang
I wouldn't try to keep all your game's data in one big xml file, that'll just get unweildy pretty quickly. Game settings for example (like your screen width/height) are user configurable and so should probably be in their own file. Similarly I'd put each level in their own file and have a master xml list which defines their order and has a relative file path to the actual level xml so you can just load in a single level instead of having to parse all the levels just to get the next one.


Maybe the world in this level is 800 tiles by 600 tiles? it doesn't look like a master data file to me. Having said that, if that is the resolution setting I would definitely put that somewhere else. And yes, definitely separate levels into their own xml files.

Looks awesome. Although you will need a large section for your tile data and change the way you're currently storing it. Look into the normal forms of databases for data integrity and stuff. I was making a game that sounds very similar to yours. The way I implemented this was to have tileset xml files which referenced the actual image files like so:

<tileset>     <image file="gfx/grass.png" id="0" /> (you could also have other flags in there if you needed them)     <image file="gfx/concrete.png" id="1" />     <etc. /></tileset>


Then in the map file I referenced the appropriate tileset and then just had a data section like:

<data tileset="grasslands.xml">1,1,0,1,2,45,2,3,4,1,0,5,1... etc.</data>


In the game I loaded the tileset in, then the map data. Too easy, and your files are smaller and the integrity and clarity of your data is assured.
Nick Wilson - Junior C# Developer | See my crappy site
Might want to look up serialization. It will save you a bunch of hassles when outputting and reading your data. Let the data members write and read themselves instead of you worrying about the minor issues. It makes it easier also when you need to add more data in the future.

theTroll
Also, using XML for actual level data is not the wisest choice. Look at this:

<fileLocation = "Data//tile_1.tga" xPos = "0" yPos = "0"/>

over half of that whole line is just metadata. While it makes your level files readable, it makes them much slower to read and parse. I would suggest you use XML for organization of your files (i.e. level list, tile list, maybe settings), but for the data itself use some other format. Either go with pure data separated by some value (i.e. "tile1;0;0;tile2;1;0;tile3 etc.") or save it in binary as opposed to characters.
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
I don't believe that lines like:

<screenHeight = "800"/>


are valid XML, are they? You can't use the element name like an attribute name. You either need:

<screenHeight>800</screenHeight>


or

<screenHeight value="800"/>


Maybe I've just missed something and it's a shorthand for the former case, but I've never seen it before either way.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Quote:Original post by superpig
Maybe I've just missed something and it's a shorthand for the former case, but I've never seen it before either way.


It would make more sense as:
<screen Height="800" Width="600" />

Though portrait orientation FTW?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement