Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Scriptable Object: Add class through inspector

Discussion in 'Scripting' started by khold93, Jul 1, 2020.

  1. khold93

    khold93

    Joined:
    Feb 11, 2020
    Posts:
    17
    Hey,

    I'm trying to add a class derived from the UseEffect class to the Consumable class.
    For Example Healing Potion.
    Is this possible in the Insecptor and if so, how is it done?

    This is the structure:

    Code (CSharp):
    1. public class Item : ScriptableObject
    Code (CSharp):
    1.  
    2. public class Consumable : Item {
    3.     public UseEffect useEffect;  
    4. //Make this field show in inspector so I can add "HealingPotion" script directly in the inspector
    5. }
    6.  
    Code (CSharp):
    1. public abstract class UseEffect {
    2.     public Virtual & Abstract Methods/Functions
    3. }
    Code (CSharp):
    1. public class HealingPotion : UseEffect
    2. {
    3.     public override Methods/Functions
    4. }
    I have tried Serializable, Monoscript, Scriptable Objects (UseEffect). I might have done it wrong thought so please share if you think you know a working method.
    Scriptable Object worked fine, but I would have to create a new Scriptable object & menu for every single one, and I want to avoid that if possible. Aka, a menu for ManaPotion, HealingPotion.. you get the idea.
    I also wanted to avoid having one huge class with all the Effects.
     
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Not totally sure i do...

    Maybe try composition instead of inheritance ?

    Your HealingPotion could be a consumable itself and would have :
    - a UseEffect field & get property
    - a EffectAmount field & get property
    - a DisplayInfo field & get property,

    UseEffect sounds like it should be a scriptable.
    For example the HealEffect: the consumerable would have an amount property that you pass to the HealEffet such as healPotionInstance.UseEffect.Use(healPotionInstance.EffectAmount).
    HealEffect would here be a single asset shared by all your healing potions so you don't need to create many, just one per behaviour which should be what you're aiming for.
    As a bonus, whenever you have to change something in a behaviour to get it working you can update all items that rely on it by editing a single asset. :eek: Looks cleaner in version control as well, when you commit something like "new version of Healing effect" and what you commit is relevant scripts & the HealEffect asset instead of 20 other assets that use it..

    DisplayInfo has for example an icon and a string property so you can return relevant values to display it in your UI.

    This way you get to tweak the behaviour, settings for the behaviour and the display independantly.
     
    khold93 likes this.
  3. khold93

    khold93

    Joined:
    Feb 11, 2020
    Posts:
    17
    Thanks for great input. I just went outside, but looking forward to try your suggestions next time I'm working on the project :)

    Edit:
    I mean - like you said "single asset shared by all your healing potions so you don't need to create many". Lets say you have Healing Potion, Mana Potion, Bombs, Fireball, Frostbolt, Cleave.. you name it. A lot of them can actually share the same asset, but a lot of them will still be unique (Use Effects). So what I was worried about is that I would have to create tons of scriptable object menues like
    1. Create
      1. Use Effect
        1. Healing Potion
        2. Mana Potion
        3. Basic Spell (Fireball / Frostball)
        4. ..
        5. ..
    However this seems like a fine solution.

    This is what I did prior to asking this question, and I think I will revert it back to scriptable Object and continue with this method :) (y) Thanks for sharing it! Also I think I will use more Virtual and not abstract methods/funcitons, because abstract made my inherit classes have to implement way to many methods/funcitons that wasn't used for that specific class. If you know a better way to handle it, tell me.
    If you ain't sure what I ment: the different Use Effects will have methods that require different overloads. Some requires ints, some require Character. My only way dealing with this currently is making the parent UseEffect class have lots of Virtual/abstract voids and override them in the derived classes. If there was a more dynamic way of doing this. That would be great :)
     
    Last edited: Jul 2, 2020