Search Unity

Is factory pattern a reasonable way to manage scriptable object skills?

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Oct 16, 2019.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I'm working on a skill/ability system and I know that inheritance tends to be pretty unmanageable in the long run, so I'm flirting with the idea of compositing each skill from a generic ScriptableObject that contains a bunch of SO components which determine how it executes. Where I'm tripping up a bit is that a lot of components, like cooldown or duration timers, need to cache and modify values as the skill is used. That in turn means that I need unique instances of each skill, and since I can't serialize SOs I need some way of doing that at runtime.

    The way I'm thinking of handling this is by creating some kind of skill manager class that contains hard references to each skill's SO template, and builds a dictionary<SkillEnum, SO reference> of every skill in the game. Characters and NPCs just have a List<SkillEnum> of every skill they know, and whenever a new character spawns in they contact the skill manager and ask for a new instance of every SO corresponding to a skill they know.

    Is that a reasonable way to approach it? I feel like it's a ton of wasted work to tie skills explicitly to objects instead of keeping them in code, but it also seems like the simplest possible path to implementing component-based skills without some kind of nefariously complicated custom editor.
     
    aperturexscience likes this.
  2. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    Well either i am complete dictionary n$#b or it was bad idea right from the start for me,
    but i had the worst horror with dictionary enum skills. Looping through and corresponding was a nightmare , it all seemed unnecessarily complicated, not talking about comparing skill level for example.

    I'd say it depends what you mean by skills you know, i guess you mean warcraft 3 like skills.

    The way i do it now, is have a Skill.cs inside which i have int Level,Mastery if you will,
    and Player just has [] of these skills.
    The skills don't do anything by themselves but if i for example cast regen spell,
    all the skill is actually for is that regen in his Tick does something like:
    Player.health += Player.Skills[2].Level * 2;

    same goes for ("bear with the silly name") "wind in the back" spell.
    it just check the level of skill inside players array with some multiplier does its work,
    and finally adds some speed to player.

    Not saying it's all correct for every case, but it works in my case. : - )

    p.s. i don't use SO, but when the time is right i'll try them out, seems like a cool approach.
    What i like about the idea is that they are not scene dependant , that's most intriguing for me.

    p.p.s and the saving is the easiest, you just loop through skills and save a bunch of ints.
     
    Last edited: Oct 16, 2019
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I'd sugges simplified approach, where you just do

    Code (CSharp):
    1. public class SkillAsset : ScriptableObject {
    2.   public string description;
    3.   public float cooldown;
    4.   public float manaCost;
    5.   public AnimatioCurve powerOfLevel;
    6.   public Sprite toolbarIcon;
    7.   public GameObject tooltipPrefab;
    8.   // etc
    9. }
    and teach some behaviour to execute that skill descriptions. After implementig that, if you ever encounter a feature do not fit into this system, you either extend the description or evolve the system one step further.
     
    Last edited: Oct 16, 2019