So I know you can’t inherit static variables in C#, but I was wondering what the best solution to this problem would be. I have a basic Character class that has a number of child classes. I want each class (I don’t care about the parent class, I just need this for the child classes) to have a static variable that represents power, and I want this to be the same for every instance of the class. The power variable needs to be able to be adjusted in run-time for all instances, which is why it needs to be static, but it needs to be unique for each of the child classes. What is the best method of doing this?
My main problem is that I have variables declared as the Character class, and I take advantage of polymorphism to assign a more specific object in run-time, but this creates issues when I want to access the static variable. Thanks for any help.
Hey @skywalker31415,
You could consider putting your shared character stats data in its own static class, one entry for each type of character. Then, in the base character class, you add an abstract function/property each subclass implements to look up the correct set of stats.
An example is probably better than my explanation – see below
public class CharacterStat { public int Power; /* ... other shared stats ... */ }
public static class CharacterStats
{
public static CharacterStat FighterStats = new CharacterStat { Power = 100, /* etc. defaults */ };
public static CharacterStat WizardStats = new CharacterStat { Power = 200, /* etc. defaults */ };
}
public abstract class Character
{
public abstract CharacterStat Stats { get; }
}
public class Fighter : Character
{
public override CharacterStat Stats => CharacterStats.FighterStats;
}
public class Wizard : Character
{
public override CharacterStat Stats => CharacterStats.WizardStats;
}
With something like the above in place, you could do things like…
… treat characters generically, with the correct set of shared class stats used:
public void Attack(List<Character> attackers, Character defender)
{
foreach (var attacker in attackers)
{
defender.TakeDamage(attacker.Stats.Power);
}
}
… or modify the stats of all characters of a certain class at runtime.
public void PowerupAllWizards()
{
CharacterStats.WizardStats.Power += 100;
}
Note: This kind of reminded me of the Flyweight design pattern, with a data singleton shared among many instances. You can read more about Flyweight here in the amazing Game Programming Patterns book.
Hope this helps!
An option to consider would be to define a property for your default values, to separate the per-type variants into their own category. For an example of how this might look in practice:
public abstract class Fighter
{
public abstract int DefaultStrength { get; }
}
public class Warrior : Fighter
{
public override int DefaultStrength
{
get
{
return defaultStrengthWarrior;
}
}
private const int defaultStrengthWarrior = 16;
}
public class Paladin : Fighter
{
public override int DefaultStrength
{
get
{
return defaultStrengthPaladin;
}
}
private const int defaultStrengthPaladin = 18;
}