Sign in to follow this  
steveworks

building levels from an external file. C#

Recommended Posts

I have built a few games in the past but I have always had to read levels off of hard coded arrays. I'd like to be able to build my levels from an external file but I don't know how. Any recommendations on what I should learn to accomplish this is greatly appreciated.

Share this post


Link to post
Share on other sites
I like to use blender, because it's free and also pretty damn good. However first you might want to check that it creates models that are compatible with whatever graphics API you are using.

Alternatively if you want to spend some money there are loads of terrain generating software packages out there.

Share this post


Link to post
Share on other sites
I don't think I was clear. I'm talking about using an array like structure in an external file that I can load then use to parse a 2D tile level. Think about building a 2D zelda style screen from an external file.

Share this post


Link to post
Share on other sites
Quote:
Original post by steveworks
I have built a few games in the past but I have always had to read levels off of hard coded arrays. I'd like to be able to build my levels from an external file but I don't know how. Any recommendations on what I should learn to accomplish this is greatly appreciated.

Well, the first thing to do would be to build your external files. You should use a format you know and understand. That could be as simple as a text file full of numbers, or more complex such as a binary format that's then compressed using zlib. There are many standard file formats you can use, such as png, jpg, dxtf (such as dxt1 - dxt5), and then there are other formats for your models, terrain, etc. Google around to find ones appropriate for your usage.

Once you know the format of your file you can then load it up using standard file IO routines, parse out the data you need, and then do with it as you will.

Share this post


Link to post
Share on other sites
I don't know any tutorials, but maybe this will get you started. System.Runtime.Serialization.DataContractJsonSerializer is much just like XmlSerializer, but for JSON. I just happened to have it in front of me.


[Flags]
enum TileFlags
{
None,
CanWalkOn = 1,
CanBuildOn = 2,
HasOre = 4,
IsRoad = 8 | CanWalkOn,
}

[Serializable]
struct Tile
{
public TileFlags Flags;
public int SpriteIndex;
}

[Serializable]
sealed class Map
[
public Tile[][] Tiles { get; set; }

public static Map LoadMap(Stream s)
{
return Serializer.ReadObject(s) as Map;
}

static readonly DataContractJsonSerializer Serializer = new DataContractJsonSerializer(typeof(Map));
}

Share this post


Link to post
Share on other sites

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