Zeke
Zeke
CC#
Created by Zeke on 3/8/2024 in #help
Composition over Inheritance
My architecture is currently as follows: Unit C# object Behaviours (Attack, Health, Targeting, Movement, etc) component based so I add and remove as needed. However, the way I currently use composition is I have an AssassinAttackComponent, MageAttackComponent, DefaultAttackComponent, etc. I feel like there are some logic that I reuse between all 3 of these (and more to come), but I don't want to inherit from these as that would break the push for composition over inheritance right? At that point my class attack components would be inheriting from DefaultAttackComponent, all of them. Advice?
4 replies
CC#
Created by Zeke on 3/8/2024 in #help
Help with Generics
Here is my current code: StatAttackRange/StatAttackDamage inherit from CharacterStat
private StatAttackRange _attackRange;

public void SetCharacterStats(List<CharacterStat> characterStats)
{
foreach (CharacterStat characterStat in characterStats)
{
if (characterStat.GetType() == _attackRange.GetType())
{
_attackRange = (StatAttackRange)characterStat;
}else if (characterStat.GetType() == _attackDamage.GetType())
{
_attackDamage = (StatAttackDamage)characterStat;
}
}
}
private StatAttackRange _attackRange;

public void SetCharacterStats(List<CharacterStat> characterStats)
{
foreach (CharacterStat characterStat in characterStats)
{
if (characterStat.GetType() == _attackRange.GetType())
{
_attackRange = (StatAttackRange)characterStat;
}else if (characterStat.GetType() == _attackDamage.GetType())
{
_attackDamage = (StatAttackDamage)characterStat;
}
}
}
I feel like it's inelegant, and I believe the solution is to use generics. But when I try to make a generic function (see below) I'm doing something wrong and I'm not sure what. https://i.imgur.com/tTW44Y9.png
25 replies