I want to ask about the best method of storing data that represents information's about specified objects.
I want to find a better way to storing somewhere items in my RPG game before problems had occurred, I had big ugly class, that was defining armors, weapons etc.
[System.Serializable]
public class Item
{
public string name;
public int cost;
public float weaponRange;
public string imagePath; //starting from Resources
public ItemType itemType;
public enum ItemType { Item, Armor, Weapon };
public enum WeaponType { None, ShortSecant, LongSecant, Blunt, Prickly, Bow };
public enum ArmorType { None, Cuirass, Helmet, Boots };
public WeaponType weaponType;
//public int damages;
public CharacterPropertyList damages = CharacterPropertyList.AttackProperties;
public bool twoHanded;
public ArmorType armorType;
//public int armorPoints;
public CharacterPropertyList armorPoints = CharacterPropertyList.DefenseProperties;
public int infoIndex = -1; // Represents item info position in Model Spawner Coordinate array, -1 when item have not model info -> item can't be spawned.
[XmlIgnore]
public bool itemExpanded = false; //expanded item in inspector?
}
But I have been able to nicely render it in inspector using PropertyDrawer.. until I had to refactor it...
I add to code some class to have better data management:
[Serializable]
public class CharacterProperty
{
[SerializeField]
private string name;
[SerializeField]
private float value;
public float Value { get { return value; } set { this.value = value; } }
public string Name { get { return name; } }
public CharacterProperty(string name, float value = 0.0F)
{
this.value = value;
this.name = name;
}
}
[Serializable]
public class CharacterPropertyList
{
[SerializeField]
private List<CharacterProperty> properties = new List<CharacterProperty>();
public List<CharacterProperty> Properties
{
get { return properties; }
}
public CharacterPropertyList(params CharacterProperty [] properties)
{
this.properties.AddRange(properties);
}
public CharacterProperty this[string name]
{
get
{
return
properties.Find(p => p.Name == name);
}}
}
public CharacterProperty this[int index]
{
get
{
return properties[index];
}
set
{
properties[index] = value;
}
}
}
And I cannot Render it correctly so finally I want to remove that big ugly Item class, and find better, more OO solution, without inspector rendering.
So I want to ask if my idea is good:
Let's say that I have a class Item, and that class will have a list that will contain child items of some interface / abstract class and that objects will be representing functionality of that Item, for example:
interface IComponent
{
//some general data
};
public class Valuable : IComponent
{
//override some data
int Value {get;set;}
}
public class DamagesItem : IComponent
{
//damages
}
public class ProtectionItem : IComponent
{
//protection
}
public class Item
{
public List<IComponent> Components {get; set;}
public Item(IComponent [] components)
{
Components = new List<IComponent>();
Components.AddRange(components);
}
}
And next I will create those items in code, or via data stored in some XML file, for example:
var sword = new Item(new IComponent[]{ new DamagesItem(), new Valuable()});
DamagesItem
,ProtectionItem
andValuable
on an item? Also, it seems a bit like you are reinventing Unity's own component system. Maybe you could use the actual system instead? That will of course only make sense if items actually have a physical presence in the game and are not just abstract entries in an inventory list. \$\endgroup\$