Sign in to follow this  

Loading a level from a file XML/

This topic is 3574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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>



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

This topic is 3574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this