Search code examples
xmlxna-4.0

Using XML files in XNA 4.0


I made a program in XNA 4.0 that should simulate an assembly line process. The model that represents the assembly line is very simple, it contains 5 "checkpoints" - start, end and 3 workstations - that represent places where some sensors would be placed on a real life model. When an object passes in front of these checkpoints, a certain method should be triggered (like "Start", "Stop" or "Count"). I managed to get this working by hard coding the coordinates of these "checkpoints" (since these "checkpoints" location will not change, there was no need for some advanced collision detection), and triggering the appropriate method when needed. Now, I want to make some additional possibilities that would make my program more flexible. I want to be able to enable/disable these "checkpoints" - when enabled, the passing objects will trigger the methods bound to the respective "checkpoint", and when disabled, the object will pass with no methods being triggered (by "I", I mean the programmer, not the user, the user should not be able to change any property of any "checkpoint"). I did some research, and I think that the best way to do this is to use an XML file that would contain the informations about every "checkpoint". I have a few questions about this - first, I never worked with loading from XML files into the XNA program, so if you could point me to some tutorials or previous posts that cover this (I don't know if it makes any difference but the files will be standardized - they will have the same number of tags, containing same number of informations), and second, is this even the right way to solve this problem or do you have some suggestions on how to do this. The application will be used only on PC's, so I don't have to take the cross platform restrictions into account. Thanks


Solution

  • Here is a blog post about loading an object from xml using the content pipeline:

    http://www.jamesewelch.com/2008/04/17/how-to-use-xnacontent-xml-files/

    You could make a Settings class or something to hold the data or just use a List<Checkpoint>. In XNAContent, Lists are defined like so:

    <Asset Type="System.Collections.Generic.List<*YourNamespaceNameHere*.Checkpoint>">
    
        <Item>
            Put your data here
        </Item>
        <Item>...
    
    </Asset>
    

    EDIT: Here are step by step instructions:

    1. Create a Content Pipeline Extension Project

    2. Create a new Class Library project

    3. Add a reference to the Class Library in the Windows Game project

    4. Add a reference to the Content Pipeline Extension Project in the Content project

    5. Follow the tutorial given (The Game Library class means your class library you created before, or the game project itself. It is better to use a Class Library.)