Download

Switch case or multiple TArrays and subclasses.

Well the question is purely theoretical, as I’m aware that both solution will work, and I already have it working, but I’m refactoring code, it stuck to me that I have this:


	
	switch (newEffect->EffectType)
		{
			case EEffectType::Effect_Boon:
				MyPawn->EffectsOnCharacter.Boons = MyPawn->EffectsOnCharacter.Boons + 1;
				//newEffect->Initialize();
				break;
			case EEffectType::Effect_Condition:
				MyPawn->EffectsOnCharacter.Conditions = MyPawn->EffectsOnCharacter.Conditions + 1;				
				//newEffect->Initialize();				
				break;			
			case EEffectType::Effect_Curse:				
				MyPawn->EffectsOnCharacter.Curses = MyPawn->EffectsOnCharacter.Curses + 1;				
				//newEffect->Initialize();				
				break;			
			case EEffectType::Effect_Enchantment:				
				MyPawn->EffectsOnCharacter.Enchantments = MyPawn->EffectsOnCharacter.Enchantments + 1;				
				//newEffect->Initialize();				
				break;			
			case EEffectType::Effect_Hex:				
				MyPawn->EffectsOnCharacter.Hexes = MyPawn->EffectsOnCharacter.Hexes + 1;				
				//newEffect->Initialize();				
				break;			
			default:				
				break;		
		}


It’s working, but what is wrong with it, that I have separate struct, with fields that essentialy contain simple data (as you can see).

What I was thinking about was to create subclasses for each type of effect, and then just create TArray like:



TArray<RPGEffectHex*> Hexes;


Bear In mind that I still must have some way to determine of type the effect is, and I would probably resolve to switch, anyway.

What would you guys preferred ? I think that the solution with TArrays is more, correct and elegant, and will allow me to be more specific about functionality exposed to blueprints. Although there might be draw back I’m not aware.

You could let the the EffectType decide what to do.
Just the very very rough idea:



newEffect->ApplyTo(MyPawn);
newEffect->Initialize();

TheEffectClass::ApplyTo(APawn &P)
{
// implement needed logic here...
// TheEffectClass_Hexes will Add itself to the Hexes array, Enchantments to the Enchantments array etc.
// Or, if needed, they could call P.AddEnchantment(Self) etc.
}