3
\$\begingroup\$

I'm having trouble with XML and XNA. I want to be able to load weapon settings through XML to make my weapons easier to make and to have less code in the actual project file. So I started out making a basic XML document, something to just assign variables with. But no matter what I changed it gave me a new error every time. The code below gives me a "XML element 'Tag' not found", I added and it started to say the variables weren't found.

What I wanted to do in the XML file as well, was load a texture for the file too. So I created a static class to hold my texture values, then in the Texture tag of my XML document I would set it to that instance too. I think that's were the problems are occuring because that's where the "XML element 'Tag' not found" error is pointing me too.

My XML document:

 <XnaContent>
   <Asset Type="ConversationEngine.Weapon">
    <weaponStrength>0</weaponStrength>
    <damageModifiers>0</damageModifiers>
    <speed>0</speed>
    <magicDefense>0</magicDefense>
    <description>0</description>
    <identifier>0</identifier>
    <weaponTexture>LoadWeaponTextures.ironSword</weaponTexture>
   </Asset>
 </XnaContent>

My Class to load the weapon XML:

public class Weapon
{
    public int weaponStrength;
    public int damageModifiers;
    public int speed;
    public int magicDefense;
    public string description;
    public string identifier;
    public Texture2D weaponTexture;
}

public static class LoadWeaponXML
{
    static Weapon Weapons;

    public static Weapon WeaponLoad(ContentManager content, int id)
    {
        Weapons = content.Load<Weapon>(@"Weapons/" + id);

        return Weapons;
    }
}

public static class LoadWeaponTextures
{
    public static Texture2D ironSword;
    public static void TextureLoad(ContentManager content)
    {
        ironSword = content.Load<Texture2D>("Sword");
    }
}

I'm not entirely sure if you can load textures through XML, but any help would be greatly appreciated.

\$\endgroup\$
11
  • 2
    \$\begingroup\$ In your example XML, you are missing </XnaContent>, is this a typing mistake? \$\endgroup\$ Jun 8, 2012 at 14:18
  • \$\begingroup\$ @NeilKnight Yes, I didn't indent it properly and while it was there in the question it wasn't displayed right. It has been fixed. \$\endgroup\$ Jun 8, 2012 at 14:38
  • \$\begingroup\$ Show the Weapon class too \$\endgroup\$
    – Torious
    Jun 8, 2012 at 21:56
  • \$\begingroup\$ @Torious Updated \$\endgroup\$ Jun 8, 2012 at 22:06
  • 1
    \$\begingroup\$ Don't save the texture, save the path to it... and after loading the weeapon, call an initialize method in that weapon that loads the texture from content. \$\endgroup\$
    – Blau
    Jun 9, 2012 at 17:19

1 Answer 1

1
\$\begingroup\$

As Blau said, just store some kind of reference to where the texture is. I recommend using a different class for storing texture info, and referencing that by an Id. In this case, you'll have 2 XML files:

<XnaContent>
  <Asset Type="ConversationEngine.Weapon">
    <weaponStrength>0</weaponStrength>   
    <damageModifiers>0</damageModifiers>   
    <speed>0</speed>   
    <magicDefense>0</magicDefense>   
    <description>0</description>   
    <identifier>0</identifier>   
    <TextureId>1</TextureId>   
  </Asset>   
</XnaContent>

And

<XnaContent>
  <Asset Type="ConversationEngine.Texture">   
    <Id>1</Id>   
    <Path>Textures/weapon01</Path>   
    <Frames>4</Frames>   
    <FirstFrame>0 0 32 32</FirstFrame>     
  </Asset>   
</XnaContent>

This way you can easily manage your texture data in case you want to implement animated textures for example. This is really helpful if you're going to have more classes that use textures (armor, weapon, monster etc.) with different sizes and / or number of animation frames, transparency etc.

Then you'll need a generic class that handles texture loading / drawing. This will take a Weapon (or Monster, Armor...) instance, and create a new instance of the Texture class with the parameters read from the XML, identified by the Weapon.TextureId property.

This might seem to be too much work at the beginning, but as your engine gets more and more features, it will save you many days of work (and refactoring). My simple, 2D sprite engine is about 60% done, and my Sprite (~ Texture) class has more than 10 properties. But with the architecture I explained, it's really easy to implement new effects for every and any game object that is displayed as a sprite.

Unfortunately I'm not at home right now, so can't post the relevant parts of my source code. Just let me know if you need them, and I'll edit the post.

Edit: You should use the built-in serializer to create your XML templates, at least until you're familiar with XNA's content manager and the XML representation of certain types.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .