Any easy way to know if an actor has a cameracomponent/scenecapturecomponent/... attached to it?

Hi,

I want to check if an actor has some kind of camera component, be it UCameraComponent, USceneCaptureComponent, etc…

As of now, we can do checks like



TArray<UCameraComponent*> CameraCompArray;
MyActor->GetComponents**<UCameraComponent>**(CameraCompArray);
if(CameraCompArray.Num() > 0)
{
 ....
}
else
{
....
}


But, then we need to do for each possible camera class. This somehow seems redundant. Am I missing something here? Afaik, there is no common base class.

Cheers!

Since they both derive from USceneComponent, I would try a GetComponents<USceneComponent> and then identifying which type in a for loop for the output. Here is an example where all things derived from a USceneComponent are returned regardless of what type of component they are.


TArray<USceneComponent*> scompswpt;
WP->GetComponents<USceneComponent>(scompswpt);
for(int i=0;i<scompswpt.Num();i++) {
    UE_LOG(LogTemp,Warning,TEXT("FlightPawn: SceneComponent = %s"),*scompswpt*->GetName());
}


Output:


LogTemp: Warning: FlightPawn: SceneComponent = Cube
LogTemp: Warning: FlightPawn: SceneComponent = Waypoint1
LogTemp: Warning: FlightPawn: SceneComponent = Sphere
LogTemp: Warning: FlightPawn: SceneComponent = Waypoint2
LogTemp: Warning: FlightPawn: SceneComponent = StaticMesh
LogTemp: Warning: FlightPawn: SceneComponent = Waypoint3
LogTemp: Warning: FlightPawn: SceneComponent = StaticMesh1
LogTemp: Warning: FlightPawn: SceneComponent = Waypoint4
LogTemp: Warning: FlightPawn: SceneComponent = WaypointSceneComponent1
LogTemp: Warning: FlightPawn: SceneComponent = StaticMesh2
LogTemp: Warning: FlightPawn: SceneComponent = WaypointSceneComponent2
LogTemp: Warning: FlightPawn: SceneComponent = StaticMesh3
LogTemp: Warning: FlightPawn: SceneComponent = WaypointSceneComponent3
LogTemp: Warning: FlightPawn: SceneComponent = StaticMesh4
LogTemp: Warning: FlightPawn: SceneComponent = WaypointSceneComponent4
LogTemp: Warning: FlightPawn: SceneComponent = StaticMesh5

@eagletree , thanks for the suggestion! But, how can I find if the scenecomponent is a camera component or not?

One way I could think of is casting, Eg;




TArray<USceneComponent*> scompswpt;
WP->GetComponents<USceneComponent>(scompswpt);
for(auto scomp : scompswpt)
{
    if( Cast<UCameraComponent>(scomp) || Cast<USceneCaptureComponent>(scomp) )
    {
        ...
    }
}


This seems ok, but was wondering if there was a better way to do it.

That works, here is a test, tossing in this crude code to what I had above:


if(Cast<UStaticMeshComponent>(scompswpt*)) {
    UE_LOG(LogTemp,Warning,TEXT("%s is a UStaticMeshComponent."),*scompswpt*->GetName());
}
if(Cast<UChildActorComponent>(scompswpt*)) {
   UE_LOG(LogTemp,Warning,TEXT("%s is a UChildActorComponent."),*scompswpt*->GetName());
}


I get this.


LogTemp: Warning: Cube is a UStaticMeshComponent.
LogTemp: Warning: Waypoint1 is a UChildActorComponent.
LogTemp: Warning: Sphere is a UStaticMeshComponent.
LogTemp: Warning: Waypoint2 is a UChildActorComponent.
...

Another approach would be this, might be more efficient than a Cast.

if(scomp->GetClass()->GetName()==“CameraComponent”) {

}

This is not correct because you are comparing the name of the component, which can be anything. In your example you can see that SMComponents are named Cube and Sphere, so this comparison doesn’t work.

Cube or Sphere is the name of component and is named or defaulted by us, StaticMeshComponent is the name returned of the class so it will be consistent. In my examples, these would be ChildActorComponent, StaticMeshComponent and SceneComponent. They are the class names but without the U at the front, the class name you would see if you hovered over the object in the blueprint hierarchy. The class name returned by “MyFPSCamera” (if it were a UCameraComponent) should be CameraComponent.

@eagletree , my bad, missed the GetClass() part. Yes, this could also be done, and it seems to be better. Thanks!