Search Unity

Inheritace ScriptableObjects vs Simple Classes

Discussion in 'Scripting' started by Deleted User, Apr 16, 2019.

  1. Deleted User

    Deleted User

    Guest

    Hello guys! Currently im working on making random spells that are not predefined. If im using simple classes like this, i can easily save them by json/xml, however if i need to have different sprites/projectiles/sounds etc, i need to set path to this objects in resources or asset bundles, also designer should edit code in order to balance game, which is bad in my opinion.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using Random = UnityEngine.Random;
    4.  
    5.  
    6. public class SpellManager : MonoBehaviour
    7. {
    8.     private void CreateSpell()
    9.     {
    10.         AddEffect(
    11.             new HealEffectNode(Target.Hero, HealEffectType.HealthFlat, new BalanceRange(50, 100, 1, 25, 50),
    12.                 true));
    13.  
    14.         AddEffect(
    15.             new DamageEffectNode(Target.Monster, DamageEffectType.Flat, new BalanceRange(100, 150, 1, 25, 50),
    16.                 true));
    17.  
    18.         AddEffect(
    19.             new LifeStealEffectNode(Target.Hero,
    20.                 new BalanceRange(100, 150, 1, 50, 75),
    21.                 new BalanceRange(15, 30),
    22.                 true));
    23.  
    24.         AddEffect(
    25.             new PeriodicBuffNode(Target.Monster,
    26.                 new DamageEffectNode(Target.Monster, DamageEffectType.Flat,
    27.                     new BalanceRange(175, 275, 1, 50, 75), true),
    28.                 0.5f, 10, 10, false));
    29.  
    30.         AddEffect(new StatBuffNode(Target.Monster, new BalanceRange(-75, -30),
    31.             new StatModifier(StatModifierType.Percentage, StatType.Damage), 10, 20, true));
    32.     }
    33. }
    34.    
    When using scriptable objects i wont have problem with linking assets for obvious reasons, also designer would see and change values that actually matter in gameplay cases, and wont even be able to break something. Serializing inheritance with scriptable objects is not the case, im already handling this. But i cant save and load scriptable objects which ruins all magic.
    What is the best way to create spells in inspector, handle serialzing inherited classes , be able to save/load them. Thanks for any advice!
     
  2. zioth

    zioth

    Joined:
    Jan 9, 2019
    Posts:
    111
    Your object above doesn't have to be serialized, since it doesn't contain any data. The only data you need is that the player has this spell, and you can use that to attach the spell to the player.

    The individual effects could be MonoBehaviors too, but I'm not sure what data you're trying to serialize. Just move all the parameters to the inspector, and there won't be anything to serialize (unless those parameters can change over time).

    If it did have data, Unity has a built-in serialization mechanism that you can use.
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    What do you mean you can't save/load scriptable objects? You can either hard-reference your spell assets in the inspector for your spell manager, or put them in resources and load them dynamically by name or path etc.

    Can you specify a use case that you're having trouble supporting?