I want to make a System where i can create Equipment and Items from.
this is what i have so far:
public class BasicGear : ScriptableObject
{
[SerializeField] protected string GearName;
[SerializeField] protected GearType gearType;
//if gearType Head Defense if gearType OneHandedWeapon Attack
}
public enum GearType
{
Head,
Chest,
Legs,
Boots,
OneHandedWeapon,
TwoHandedWeapon,
OffHand,
Ring,
Necklace,
}
Id like to create alot of diffrent items from this
but based on the type the item would have diffrent properties, like a Chestplate is unlikely to Attack the Enemy so it adds Defense
A Sword on the other Hand would be good with Attack…
i could add each and every propertie here and then only fill what i need but wouldnt that be unclean?
like this is going to grow not only do i have gearType but also rarity and lets add not only attack values but also Strenght Stats and …
That way the inspector would end up loaded, like alot
is it possible to seperate properties? Whats a good way to make it Clean and easy to use?
what if i used a list of classes instead of a enum?
1 Answer
1
I gotta admit , i hate how word “clean” is getting thrown around a lot , and it means many things depending on context and the person saying it and their experience , all i’m gonna propose is a suggestion and you can take it from there since you have more info about the actual context.
Personally , i’d go with something like this.
/// <summary>
/// Use this represent unique properties/functionalities per gear piece
/// </summary>
public abstract class GearPropertyBase : ScriptableObject { }
/// <summary>
/// Example of usable gear (this can be called on click for example)
/// </summary>
public abstract class UseableGearProperty : GearPropertyBase
{
public abstract void OnUse();
}
public class AttackContext
{
public GameObject Attacker;
public GameObject Target;
public int AttackValue;
}
/// <summary>
/// Example of attack altering buff/interaction
/// </summary>
public abstract class OnAttackGearProperty : GearPropertyBase
{
public abstract void OnAttack(AttackContext ctx);
}
// etc , etc ....
/// <summary>
// instances of this will act as the enum
// for example : Chest.asset will refer to the chest slot
// to use this in code , i'd have a "global" class (like "GlobalResources" below) that refers to all the gear types
/// </summary>
public class GearType : ScriptableObject { }
/// Maybe this can be a singleton , up to you
public class GlobalResources : MonoBehaviour
{
public GearType chest;
public GearType head;
public GearType weapon;
public GearType shield;
public IEnumerable<GearType> GetAll()
{
yield return chest;
yield return head;
yield return weapon;
yield return shield;
}
public IEnumerable<GearType> ArmorTypes()
{
yield return chest;
yield return head;
}
public IEnumerable<GearType> DamageDealingTypes()
{
yield return weapon;
yield return shield;
}
}
// you can make this a scriptableobject too , depending on use case
public enum BaseStat
{
Strength,
Stamina,
Agility
}
public struct StatValuePair
{
BaseStat Stat;
int Value;
}
public class GearData : ScriptableObject
{
/// <summary>
/// Gear name
/// </summary>
public string Name;
/// <summary>
/// Gear type
/// </summary>
public GearType GearType;
/// <summary>
/// Actual flat stats
/// </summary>
public StatValuePair[] FlatStats;
/// <summary>
/// Gear properties , this can contain the "onEquip" effects , "onAttack" effects , etc
/// </summary>
public GearPropertyBase[] GearProperties;
/// <summary>
/// Helper method to query a specific property of a gear piece
/// </summary>
public T GetProperty<T>() where T : GearPropertyBase
{
foreach(GearPropertyBase p in GearProperties)
{
if (p is T casted)
return casted;
}
return default;
}
}
I like to keep it simple and based around data , i find the overuse of generics and template contraints to be a slippery slope for trouble especially when changes are needed , this kind of setup worked many times for me , if you have questions/related comments feel free to add them.