Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Tweeted twitter.com/StackGameDev/status/887707126281281536
Broadened question, improved formatting
Source Link

How to design a skillSkill/Monster/Item etc. dictionary for a game?

I am trying to build a turn-based RPG where party members and enemies can cast skills.

I want to have PartyMembers and Enemies cast skills on each other. For example: Heal has a caster and a target, WarCry(attack up) has a caster and all members of the party (either all PartyMembers or all Enemies, depending on who is doing the casting). Each skill should have an SP cost, and if the player or enemy does not have enough SP they should not be able to cast ituse 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?

public static Dictionary<int, Skill> SkillDictionary = new Dictionary<int, Skill>
        {
            {1, new Skill1()}, // I don't want to new anything up though!
            {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(){};
}

HoweverAs my code currently stands, this isn't ideal. I don't wanteach skill is instantiated only once, before Main (due to new anything upthe dictionary being static). Does that meanIs there a way to do lazy loading so I would needwon't have to implement a singleton design for allinstantiate the skillsskill until called? Is lazy loading favorable to what I have so far? (I'm not too experienced with programmingin case I get to the point where I have LOTS of items, but is theremonsters, etc.) Furthermore, suppose I were to make a wayMonsterDictionary. Then would it be wise to do that using genericscall a Clone method in the abstract Skill classorder 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 enumEnum be adequate instead of a dictionary in some of these cases? (Forgive my programming skillsI'm relatively new to C# and game development, so I just learned about enums today)would like some feedback if possible. I'm curious to see how others organized their game data.

How to design a skill dictionary?

I am trying to build a turn-based RPG where party members and enemies can cast skills.

I want to have PartyMembers and Enemies cast skills on each other. For example: Heal has a caster and a target, WarCry(attack up) has a caster and all members of the party (either all PartyMembers or all Enemies, depending on who is doing the casting). Each skill should have an SP cost, and if the player or enemy does not have enough SP they should not be able to cast it.

  How could I implement a modular list of skills so that each PartyMember or Enemy could choose a skill from the list and cast it?

public static Dictionary<int, Skill> SkillDictionary = new Dictionary<int, Skill>
        {
            {1, new Skill1()}, // I don't want to new anything up though!
            {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(){};
}

However, this isn't ideal. I don't want to new anything up. Does that mean I would need to implement a singleton design for all the skills? (I'm not too experienced with programming, but is there a way to do that using generics in the abstract Skill class?)

Or - the golden question - is there a better design pattern altogether? Would an enum be adequate? (Forgive my programming skills, I just learned about enums today).

How to design a Skill/Monster/Item etc. dictionary for a game?

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?

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.

Made a more detailed description of problem
Source Link

I am trying to build a turn-based RPG where party members and enemies can cast skills.

I want to have PartyMembers and Enemies cast skills on each other. For example: Heal has a caster and a target, WarCry(attack up) has a caster and all members of the party (either all PartyMembers or all Enemies, depending on who is doing the casting). Each skill should have an SP cost, and if the player or enemy does not have enough SP they should not be able to cast it.

How could I implement a modular list of skills so that each PartyMember or Enemy could choose a skill dictionaryfrom the list and cast it? I

My first attempt was thinking of something likethis:

public static Dictionary<int, Skill> SkillDictionary = new Dictionary<int, Skill>
        {
            {1, new Skill1()}, // I don't want to new anything up though!
            {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(){};
}

I reasoned that I needed a singleton design for the abstract Skill class. Unfortunately, that doesn't make sense either, as there would only be one instance and accessing Skill1.Instance, Skill2.InstanceHowever, Skill3.Instance would only access the generic Skill.Instancethis isn't ideal. I don't want to new anything up. Does that mean I would need to implement a singleton design for all the skills? (I'm not too experienced with programming, but is there a way to do that using generics in the abstract Skill class?)

Or - the golden question - is there a better design pattern altogether? Would an enum be adequate? (Forgive my programming skills, I just learned about enums today).

I am trying to build a turn-based RPG where party members and enemies can cast skills. How could I implement a skill dictionary? I was thinking of something like

public static Dictionary<int, Skill> SkillDictionary = new Dictionary<int, Skill>
        {
            {1, new Skill1()}, // I don't want to new anything up though!
            {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(){};
}

I reasoned that I needed a singleton design for the abstract Skill class. Unfortunately, that doesn't make sense either, as there would only be one instance and accessing Skill1.Instance, Skill2.Instance, Skill3.Instance would only access the generic Skill.Instance. Does that mean I would need to implement a singleton design for all the skills? (I'm not too experienced with programming, but is there a way to do that using generics in the abstract Skill class?)

Or - the golden question - is there a better design pattern altogether?

I am trying to build a turn-based RPG where party members and enemies can cast skills.

I want to have PartyMembers and Enemies cast skills on each other. For example: Heal has a caster and a target, WarCry(attack up) has a caster and all members of the party (either all PartyMembers or all Enemies, depending on who is doing the casting). Each skill should have an SP cost, and if the player or enemy does not have enough SP they should not be able to cast it.

How could I implement a modular list of skills so that each PartyMember or Enemy could choose a skill from the list and cast it?

My first attempt was this:

public static Dictionary<int, Skill> SkillDictionary = new Dictionary<int, Skill>
        {
            {1, new Skill1()}, // I don't want to new anything up though!
            {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(){};
}

However, this isn't ideal. I don't want to new anything up. Does that mean I would need to implement a singleton design for all the skills? (I'm not too experienced with programming, but is there a way to do that using generics in the abstract Skill class?)

Or - the golden question - is there a better design pattern altogether? Would an enum be adequate? (Forgive my programming skills, I just learned about enums today).

Can't ask for "best way" here, it's very subjective. But you still can ask how you could do it :)
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 55
  • 61
Loading
Source Link
Loading