HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Loading items from XML document

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
xmlitemsdocumentloadingfrom

Problem

I'm making a game in Unity and I need to have a inventory/item system, I decided to store my items in simple XML document which I later read from. I'm planning to have different varieties of items and they will all have distinct classes which extend they're base interface. Here's how my hierarchy looks like for now :

IBaseItem

public interface IBaseItem
{
    int ItemID { get; }
    string ItemName { get; }
    string Description { get; }
}


A really simple interface which contains all the common properties between all the items.

EquipementItem : IBaseItem

[System.Serializable]
public abstract class EquipementItem : IBaseItem
{
    public enum ItemTypes
    {
        None,
        Armor,
        Weapon
    }

    public enum SlotTypes
    {
        Head,
        Shoulders,
        Chest,
        Bracers,
        Gloves,
        Waist,
        Legs,
        Boots,
        Weapon
    }

    private readonly int itemID;
    public int ID
    {
        get { return itemID; }
    }

    private readonly string itemName;
    public string Name
    {
        get { return itemName; }
    }

    private readonly string description;
    public string Description
    {
        get { return description; }
    }

    private readonly ItemTypes itemType;
    public ItemTypes ItemType
    {
        get { return itemType; }
    }

    private readonly SlotTypes slotType;
    public SlotTypes SlotType
    {
        get { return slotType; }
    }

    protected EquipementItem(int itemID, string itemName, ItemTypes itemType, SlotTypes slotType, string description)
    {
        this.itemID = itemID;
        this.itemName = itemName;
        this.itemType = itemType;
        this.slotType = slotType;
        this.description = description;
    }
}


This is the class for all the equippable items, and since the items are constant throughout the entire game I made the object immutable. However this might cause me some trouble since the constructor apparently needs too

Solution

Suggesting alternatives is always a risky review (and usually one gets DV for this) but I'll try it anyway...

Nowaydays I wouldn't chose XML anymore. With JSON you'd be better off. The most popular framework (JSON.NET) can solve all your problems because:

  • you don't have to worry about parsing it



  • you don't have to worry about converting any types - it'll handle everything for you - all enums, ints, doubles or any custom types.



If you decorate your type's constructor with the JsonConstructorAttribute it can even handle immutable objects .

This means that the loading and saving part would virtually be reduced to just a few lines:

var json = // read json from db
var equipementItems = JsonConvert.DeserializeObject (json);


and

var json = JsonConvert.SerializeObject(equipementItems);


JsonConvert.DeserializeObject Method

JsonConvert.SerializeObject Method

Code Snippets

var json = // read json from db
var equipementItems = JsonConvert.DeserializeObject <EquipementItem[]>(json);
var json = JsonConvert.SerializeObject(equipementItems);

Context

StackExchange Code Review Q#147931, answer score: 2

Revisions (0)

No revisions yet.