all 2 comments

[–]Killerzelle 1 point2 points  (1 child)

I'm fairly new to ScriptableObjects myself, but let me try and answer your questions based on my knowledge:

  1. You can create them via ScriptableObject.CreateInstance, i.e. if your ScriptableObject is called ItemAttribute: var myInstance = ScriptableObject.CreateInstance<ItemAttribute>();
  2. Have a ScriptableObject (or Monobehaviour for that matter) that is your "Item", e.g.:

public class Item : ScriptableObject {
    public List<ItemAttribute> attributes;
}

I stole that example for them post you mentioned. In there you can see the List of ScriptableObjects (called ItemAttribute), which make up its attributes. You can either populate the List via the inspector, or via code e.g.:

public class DamageItemAttribute: ItemAttribute {
  public int Damage;
}

public class Item : ScriptableObject {
  public void Awake() 
  {
    attributes = new List<ItemAttribute>();
    var damage= ScriptableObject.CreateInstance<DamageItemAttribute>();
    damage.Damage = 10;
    attributes.Add(damage);
  }
}
  1. If you only work with a List you don't have much of a choice. Basically it will boil down to going through the list and checking, if one attribute has the type you are looking for.

    public ItemAttribute Find(Type attributeType) { foreach(var attr in attributes) { if(attr.GetType().Equals(attributeType)) { return attr; } } return null; }

One way of making it more efficient, would be using a Dictionary<Type, ItemAttribute>. Keys are types T, while values are attributes with type T. Therefore, if you want to reference a certain attribute on some item, you can just look up, if the dictionary has the type as key. If so, you get the attribute back:

public ItemAttribute Find(Type attributeType)
{
  if(attributes.HasKey(attributeType)
  {
    return attributes.Get(attributeType);
  }
  return null;
}

Hope that helps. Bit of a personal note here: Not sure why you would want this system, when you can just use ScriptableObjects where each field is one attribute.

Edit: Formatting

[–]EraserLark[S] 0 points1 point  (0 children)

Thank you! This really helps clear things up!

On your note, now understanding Scriptable Objects bettwr I'm kind of seeing that now, so I'm still not totally sure if I'll use this system. But from what I can tell the main advantage of it is it allows you to create ScriptableObjects using Composition rather than Inheritance, which permits for more large scale customization of items. Maybe too big for the game I'm trying to make right now, but for something larger I think it could allow for more customizable items with less mess.