XNA Creators Club Online
Page 1 of 1 (12 items)
Sort Posts: Previous Next

XML bit flag enum reader...Am I on the right track?

Last post 5/30/2009 11:06 PM by Shawn Hargreaves. 11 replies.
  • 5/27/2009 9:09 PM

    XML bit flag enum reader...Am I on the right track?

    I have succesfully constructed a custom XML reader/writer for a "TileSet" class. (Very basic, so nothing special there)
    within the class there are two arrays:
    ZLayer[] (to position tiles in front of or behind the player - 2D) - ZLayer is of type int
    TExits[] (to describe routes through a tile, tile is passable left to right for example, but does not allow up or down movement) TExits is an bit flag enum called Exits.

    I have no problems with the Zlayer, as it is a simple type to read from an XML file. The bit flag enum on the other hand is giving me a headache.

    Originally I was going to use type int and then implement a switch statement to apply the exit logic for each tile, I'm not a fan of this solution because while I am manually creating the XML tileset files, it isn't very intuative to keep trying to remember which number represents which tile exit combination.

    So the idea came about to use a bit flag enum, my thinking was that now I only needed to supply the parts of the directions I wanted, without having to remember which number the direction is associated with. For example for a North & South tile the allocation of a variable would look like TExits = Exits.N|Exits.S, how I implement this via XML I have no idea.

    My aim is to keep the tileset XML readable to the human eye and intuative. If I get round to making a tileset editor this wouldn't be a problem, but while I am no where near that I am stuck with trying to keep it simple.

    For those that know RPG Maker XP, my tilesets will be working in a similar way.

    So brainy bunch, help my grey cells make some new connections please, How would you impliment an array of values to represent directions passable on a tile. Kudos to whoever has the best suggestion :) If you know a direct answer, please don't give it all away, just a clue will do, I enjoy the challenge.
  • 5/27/2009 9:25 PM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

    The Enum type provides methods to convert to and from a string representation.

    How are you reading and writing this XML, though? Most XML serialization technologies (eg. XmlSerializer, IntermediateSerializer) already know how to handle enum types without you having to write any specific code to enable that.
    XNA Framework Developer - blog - homepage
  • 5/27/2009 11:51 PM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

    I am able to read and write a single enum string value, but I am not able to read and write a bitwise operation. For example my enum looks like this:

    [Flags] 
        public enum Exits : uint 
        { 
            N    = 0x01, // North 
            W    = 0x02, // West 
            S    = 0x04, // South 
            E    = 0x08, // East 
            B    = 0x16  // Blocked 
     
        } 

    Now I want to input an array of data via xml for each tiles Exit, if each tile only had one exit then it would be easy, but also pointless. I need to be able to pass into my class something like the following

     <TExits> 
          N|S N|S|E W|E N|S|E N|S N N|S W|E   
     </TExits> 

    Using the bit flag enums and their bitwise operators to set each items bit flag. as yet using this hasn't worked. Can I even use bitwise operators while read XML into a class. If I can't, how can bit flag enums be used. Is there a class that supports this operation?

    I have been using the basic ContentTypeWriter<T> and ContentTypeReader<T> classes. If I need to use another type of XML reader/writer can they still do what my current classes do?

    Cheers for your response. :)
  • 5/28/2009 12:01 AM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

    The Enum <-> String conversion methods in .NET fully support bitfield enums, so you shouldn't need to write any special code to make this work. It would help if you showed us how you are currently reading and writing this XML though.

    ContentTypeWriter/Reader are for reading and writing the binary .xnb file format: these have nothing to do with XML.
    XNA Framework Developer - blog - homepage
  • 5/28/2009 6:06 AM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

    There's nothing wrong with the way the enum serialized to XML. I just whipped together a simple test app that generates an XML file for the content pipeline using the Intermediate Serializer. The serializer writes the enum as a string so, as you'd expect, it follows the string format of an enum:

    <?xml version="1.0" encoding="utf-8"?>  
    <XnaContent>  
        <Asset Type="WindowsFormsApplication1.ToSerialize">  
            <Name>My Test Class</Name>  
            <Test>N, S, E</Test>  
        </Asset>  
    </XnaContent> 

    For this test application:

    using System;  
    using System.Windows.Forms;  
    using System.Xml;  
    using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;  
     
    namespace WindowsFormsApplication1  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                XmlWriterSettings settings = new XmlWriterSettings()  
                {  
                    Indent = true,  
                    IndentChars = "    ",  
                };  
     
                using (XmlWriter writer = XmlWriter.Create(  
                    "C:\\Test.bitfield", settings))  
                {  
                    IntermediateSerializer.Serialize<ToSerialize>(writer,  
                        new ToSerialize()  
                        {  
                            Name = "My Test Class",  
                            Test = Test.N | Test.E | Test.S  
                        }, "test");  
     
                    writer.Close();  
                }  
            }  
        }  
     
        public class ToSerialize  
        {  
            public string Name { getset; }  
            public Test Test { getset; }  
        }  
     
        [Flags]  
        public enum Test  
        {  
            N = 1,  
            S = 2,  
            E = 4,  
            W = 8,  
        }  
  • 5/28/2009 8:16 PM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

    Shawn Hargreaves:
    The Enum <-> String conversion methods in .NET fully support bitfield enums, so you shouldn't need to write any special code to make this work. It would help if you showed us how you are currently reading and writing this XML though.

    ContentTypeWriter/Reader are for reading and writing the binary .xnb file format: these have nothing to do with XML.


    I am using the ContentTypeWriter/Reader to serialize and deserialize xml into .xnb to transfer in xml data into a custom class. I thought this was the way it was done. The role-playing Game Starter Kit does it this way and I have used it's technique with a bit of searching elsewhere to figure it all out.

    Is this not the normal way to use data in an XML to supply data to a class?
  • 5/28/2009 9:09 PM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

    Cheers MrLeebo, it will take me a bit of time to figure out using the intermediateSerializer.

    From your example I still can't see how I am going to use an XML file to create my tileset class data, imagine an 8 x 8 grid, which represents an image chopped up into a grid 8 tiles across and 8 tiles down, and for each tile I want to record information to describe how a player can walk across the tile, so if say one of the tiles represent part of a bridge that can only be walked across left to right and vice versa, my idea was to create an array of enums and each array element would be associated with a tile.

    The enum was going to be a bit flag enum, so that when I create the XML files for each tileset, I have an easier to read file. What I'm not sure about, is how I translate from an XML file, the enum along with the bitwise operator. Using the enum is the easy part, using the bitwise operator is the bit I'm stuck on. I can't tell yet if your example is a solution to this problem. That because I am a bit of a novice. I definatly want to explore the intermediateSerializer class more.

    Somehow I feel I am overcomplicating the problem.

    Cheers for your help on this, it is much appreciated.


  • 5/28/2009 9:18 PM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

    Magesterium:
    Using the enum is the easy part, using the bitwise operator is the bit I'm stuck on. I can't tell yet if your example is a solution to this problem.


    This is exactly what the code MrLeebo posted shows you how to do. He created a bitfield enum, stored an instance of this type in class, serialized that class into XML using IntermediateSerializer, and showed you what the resulting XML looks like.
    XNA Framework Developer - blog - homepage
  • 5/28/2009 9:22 PM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

    Magesterium:

    I am using the ContentTypeWriter/Reader to serialize and deserialize xml into .xnb to transfer in xml data into a custom class.


    Not exactly. The data flow goes like this:
    1. XmlImporter (which uses IntermediateSerializer) reads XML into a managed object
    2. ContentTypeWriter saves that managed object into XNB format
    3. ContentManager.Load reads the XNB data back into a managed object
    These three steps are totally independent of each other. By the time the ContentTypeWriter is called in step 2, it is just dealing with a managed object: there is no XML involved any more. The fact that this managed object was created by deserializing an XML file is irrelevant to the ContentTypeWriter: that same ContentTypeWriter could equally well be given a managed object created in some entirely different way, and it would not need to know or care about that.

    Understanding the flow of data is important, so that you know where to look when things go wrong. For instance if you are having a problem reading XML, there is point looking at the ContentTypeWriter when attempting to solve this.

    Magesterium:

    Is this not the normal way to use data in an XML to supply data to a class?


    This is a common way to pass data through the Content Pipeline, yes.
    XNA Framework Developer - blog - homepage
  • 5/29/2009 8:36 PM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

    Cheers for all your help on this. I feel such a newb. I guess I'm just too eager. I know that can be a common problem for us hobbyist.

    I think i'm quite okay at understanding the basic stuff, the kinda things you get in the C# books, I'm just lacking knowledge of XNA, it's classes and all the other classes out there that can provide functionality. I guess I'm trying to run before I can walk.

    For instance I wasn't aware of the intermediateSerializer, so sorry if my questions appear a little dense, I have to start somewhere :o)

    I was just uber excited to be able to get my XML into my classes (Hey, don't role your eyes :o), it feels good to do new things new)

    J
  • 5/30/2009 11:58 AM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

    The methods discussed here are great for creating an XML file to contain the data, and knowing that has come in very useful.

    However, now I've had a play, this was not what I asked for in my original plea for help. I need to take an externally created XML file and supply it's data into my XNA programme, not create and write to XML as the example above does.

    I have had a go at turning the logic around and using the reader method, but it's not gone to well when I get to the enum object.

    Here is my attempt at the reader, I'm sure it's horrably wrong at this stage, as I have only just created it
    public static myObject ReaderMethod(myObject val) 
            { 
                XmlReaderSettings RSettings = new XmlReaderSettings() 
                { 
                    IgnoreWhitespace = true
                }; 
     
                using(XmlReader reader = XmlReader.Create("XMLFile1", RSettings)) 
                { 
                    reader.Read(); 
                    reader.ReadStartElement("XnaContent"); 
                    reader.ReadStartElement("Asset"); 
                    reader.ReadStartElement("Name"); 
                    val.Words = reader.ReadString(); 
                    reader.ReadEndElement(); 
                    reader.ReadStartElement("Test"); 
                    val.Directions = reader.ReadString(); // Error here 
                    // Can't convert type string to test 
     
     
                } 
            } 

    Once the little squiggly red line appeared, where I've marked "// Error here" I tried (Test)reader.Readstring(), but as you will already know that didn't work.
    How do I use the reader to read enum elements and how do apply the bitwise operators to them, as in my orginal request.
    I need the reader to be able to turn "N, S, E" into Test.N | Test.S | Test.E

    J :o)
  • 5/30/2009 11:06 PM In reply to

    Re: XML bit flag enum reader...Am I on the right track?

Page 1 of 1 (12 items) Previous Next