building levels from an external file. C#

Started by
6 comments, last by CadetUmfer 14 years, 1 month ago
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.
---------------------------------------------------------------------------------------Exercise, eat right and be the best you can be!Translation: Play video games for finger streangth and eat lots of hot pockets to be at top programming efficiency.
Advertisement
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.
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.
---------------------------------------------------------------------------------------Exercise, eat right and be the best you can be!Translation: Play video games for finger streangth and eat lots of hot pockets to be at top programming efficiency.
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

As a side note, C# has built-in Xml handling.
Quote:As a side note, C# has built-in Xml handling.


I found that out recently and after looking at a few tutorials I believe I can throw something together.
---------------------------------------------------------------------------------------Exercise, eat right and be the best you can be!Translation: Play video games for finger streangth and eat lots of hot pockets to be at top programming efficiency.
well the idea I had didn't work to well. Could anyone recommend me some GOOD Xml tutorials.
---------------------------------------------------------------------------------------Exercise, eat right and be the best you can be!Translation: Play video games for finger streangth and eat lots of hot pockets to be at top programming efficiency.
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));}
Anthony Umfer

This topic is closed to new replies.

Advertisement