- Home /
inheritance from a ScriptableObject
So... I want to inheritance a class from a Scriptableobject but I have some concerns. How it should look? Let's say I make a Item class Inheritance from ScriptableObject. I want to make a weapon class witch inheritance from item, but in the same time i want to inheritance it from ScriptableObject. Can someone help me? How something like that should look? If I remember good you can inheritance from more than one class. And ScriptableObject is a class, right?
Answer by Hellium · Oct 04, 2017 at 08:30 AM
First thing, no, you can't inherit from more than one class (in C#).
Then, if you have a class B
inheriting from class A
, and class C
inheriting from class B
, then, indirectly, the class C
inherits from class A
. So your weapon will be a scriptable object thanks to the transitivity property of inheritance.
// Item.cs
public abstract class Item : ScriptableObject { /* ... */ }
// Weapon.cs
[CreateAssetMenu( fileName = "Weapon", menuName = "Items/Weapon" )]
public class Weapon: Item { /* ... */ }
Answer by Jeshira · Mar 20, 2018 at 01:01 AM
Inheritance means it will have everything from the parent plus stuff of his own and often overridden functions of the parent.
//Generic class that can't be instantiate.
public abstract class A: ScriptableObject {
//Only accessible from children.
protected int iVar;
public int getVar(){ return iVar;}
}
//Will also inherit from ScriptableObject.
public class B: A {
public B(int _iVar){
//You can write to inherited protected variable, not private ones.
iVar = _iVar;
}
}
//Can create object and call parent public functions like this.
B b = new B(10);
b.getVar(); //Will return 10.
This works to a certain extend. As said before, you can't inherit from two classes. Some way that is normally used is Component-Based Design, but those doesn't get any serialization done by the inspector by default. Component-Based Design is exactly how GameObjects are designed with various components in the inspector.
Attribute.cs
public enum AttributeType {
Elemental,
Category
}
public class abstract Attribute : scriptableObject {
protected AttributeType attribute;
public Attribute { get { return attribute; }}
//Children have to override this.
public abstract void effect(GameObject _target);
}
Elemental.cs
public enum ElementalType {
Fire,
Ice
}
[CreateAssetMenu( fileName = "Elemental", menuName = "Attributes/Elemental" )]
public class Elemental : Attribute {
public ElementType element;
public Elemental(){
attribute = AttributeType.Elemental;
}
override void effect(GameObject _target){
//Do something.
}
}
Category.cs
public enum CategoryType {
Weapon,
Armor
}
[CreateAssetMenu( fileName = "Category", menuName = "Attributes/Category" )]
public class Category : Attribute {
public CategoryType category;
public Category(){
attribute = AttributeType.Category;
}
override void effect(GameObject _target){
//Do something.
}
}
Item.cs
[CreateAssetMenu( fileName = "Item", menuName = "Items/Item" )]
public class Item : scriptableObject {
public List<Attribute> attributes;
public void Use(GameObject _target){
foreach(Attribute attribute in attributes)
attribute.effect(_target);
}
}
You'll have to create each ScriptableObjects (Weapon, Armor, Fire, Ice) and drag them in the list. Hence if you want a Fire Sword and a Fire Ax with different Fire damage value, you'll need two different Fire scriptable, but if effect is same for both Fire (set player status to burn), then you only need one.
Your answer
Follow this Question
Related Questions
Reference to scene objects from scriptable objects 1 Answer
Inheriting Parent Rotation and position 2 Answers
How to store data in script and attach it later? 0 Answers
Scrolling combat text works on enemy. When I duplicate and have two enemies I get a problem. 1 Answer
Inheritance Implementation Weapon add System for the Pro Military: FPS Character? 0 Answers