3
\$\begingroup\$

I am trying to build a turn-based RPG where party members and enemies can cast skills and use items. How could I implement a modular list of skills/items/monsters (and maybe character classes, if I get there) so that each PartyMember or Enemy could choose a skill/item from the list and cast it?

My first attempt was this:

public static Dictionary<int, Skill> SkillDictionary = new Dictionary<int, Skill>
{
    {1, new Skill1()},
    {2, new Skill2()},
    {3, new Skill3()},
};
public abstract class Skill
{
    int skillIndex;
    public abstract void CastSkill();
}
public class Skill1 : Skill
{
    public Skill1(){skillIndex = 1;}
    public override void CastSkill(){};
}
public class Skill2 : Skill
{
    public Skill2(){skillIndex = 2;}
    public override void CastSkill(){};
}
public class Skill3 : Skill
{
    public Skill3(){skillIndex = 3;}
    public override void CastSkill(){};
}

As my code currently stands, each skill is instantiated only once, before Main (due to the dictionary being static). Is there a way to do lazy loading so I won't have to instantiate the skill until called? Is lazy loading favorable to what I have so far? (in case I get to the point where I have LOTS of items, monsters, etc.) Furthermore, suppose I were to make a MonsterDictionary. Then would it be wise to call a Clone method in order to make a new instance of a monster? Perhaps:

public Monster
{
   string name; 
   int otherStats;
   public Monster(Monster clone)
   {
      this.name = clone.name;
      this.otherStats = clone.otherStats;
   }
}

And to create a new instance of the monster in battle, for example:

Enemy e = new Monster(MonsterDictionary["Slime"]);

Or - the golden question - is there a better design pattern altogether? Would an Enum be adequate instead of a dictionary in some of these cases? I'm relatively new to C# and game development, so I would like some feedback if possible. I'm curious to see how others organized their game data.

\$\endgroup\$
4
  • \$\begingroup\$ Can you describe more in detail how it should work, what should it allow and how it should be used? What do you need static mapping of implementations to integers for in the first place? \$\endgroup\$
    – wondra
    Commented Jul 17, 2017 at 17:48
  • 2
    \$\begingroup\$ Your code so far could be easily replaced with an Enum, that's just overkill. \$\endgroup\$
    – DH.
    Commented Jul 17, 2017 at 18:15
  • 3
    \$\begingroup\$ Before we can tell you how to design it, you need to tell us how you're going to use it. Are you really just calling methods without passing in any arguments or returning any data? You don't need to, for example, apply a skill to an object and retrieve a result about that action? \$\endgroup\$
    – House
    Commented Jul 17, 2017 at 19:29
  • \$\begingroup\$ @DH, could you elaborate? How would I cast a skill using an Enum? For example, how would I store the data related to the skill such as casting cost, skill name, bool requiresTarget, etc? \$\endgroup\$ Commented Jul 18, 2017 at 3:09

1 Answer 1

1
\$\begingroup\$

For one I would suggest doing a data-driven approach for your skills instead of individual classes. It will make your game much more consistent and manageable in the long run. You also get the bonus of being able to easily pull out your skill data from the code into an easily moddable format like JSON or YAML.

One article that really demonstrates this concept is this custom ability guide for Dota2.

One way that to solve your skill problem is to create two classes, one that holds shared data (attack name, attack type, range, base damage etc.) and one that holds instance specific state (uses this turn, remaining cooldown etc.) as well as a reference to look up the shared data object in the dictionary.

So when I want to add a skill to a unit, I would just need to create a new skill instance object that would just contain a reference to the dictionary that holds the skill data.

For your monster example, the same concept can be utilized.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .