I really like the idea of using components but there is one thing that bugs me a little bit.
To access components on actors I have to use
UActorComponent* AActor::GetComponentByClass(TSubclassOf<UActorComponent> ComponentClass)
{
UActorComponent* FoundComponent = NULL;
for (UActorComponent* Component : OwnedComponents)
{
if (Component && Component->IsA(ComponentClass))
{
FoundComponent = Component;
break;
}
}
return FoundComponent;
}
Which seems that it could become a bottleneck in the future. At the moment I am implementing one interface per component like this
class UHealthComponent;
UINTERFACE(MinimalAPI)
class UHealthInterface : public UInterface
{
GENERATED_UINTERFACE_BODY()
};
class IHealthInterface
{
GENERATED_IINTERFACE_BODY()
virtual UHealthComponent* Get() const;
};
Then I can cast the actor to this interface and get the component in constant time. But in Unreal I have to create one interface per file and this would also be quite annoying to maintain. I could auto generate the interface files with a tool but I would like to avoid this.
Initially I thought I could just create a templated interface like
...
template<typename T>
class IComponentInterface
{
GENERATED_IINTERFACE_BODY()
virtual T* Get() const;
};
UCLASS(config=Game)
class AShooterCharacter : public ACharacter, public IComponentInterface<UHealthComponent>
but templates don’t play nice with virtual functions in C++.
Technically this would be premature optimization because I don’t even know if it might become a bottleneck in the future but if it would I would have to do a lot of refactoring.
So I am wondering how you are using components?