Thank you for you help.
Class A. We create components in the parent class:
A.h
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UsableMesh")
class UBoxComponent* Box;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UsableMesh")
class UStaticMeshComponent* PickupMesh;
A.cpp
Box = CreateDefaultSubobject<UBoxComponent>(TEXT("Box"));
RootComponent = Box;
PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));
PickupMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
PickupMesh->SetupAttachment(RootComponent);
We create a child B from the class A:
B.h
*//Nothing to do - the component is inherited from A
//Other stuff specific for B
…*
B.cpp
*//Nothing to do - the component is inherited from A
//Other stuff specific for B
…*
However, we create blueprints based on the class B and put them in the level. There are various children of the class B (B1, B2, etc.), but all are based on the class A and all inherit their components from the class A. So all children B have the same components (Box and PickupMesh).
Controller.h
//Declare a raycast function
Controller.cpp
//the raycast function detects all B classes based on A and returns A
bool bIsHitSomething = GetWorld()->LineTraceSingleByObjectType(HitData, Start, End, FCollisionObjectQueryParams::AllObjects, RV_TraceParams);
if(bIsHitSomething){
A* parent = Cast<A>(HitTarget);
}
return parent;
Because there are various blueprints B (B1, B2, etc.), the raycast search for their common parent (A) and when detected, it is stored in the variable of type A. So all B are detected by their parent A. For example, if raycast hits the B3, it is stored in the variable as A. Or if raycast detects B1, it is stored in the variable as A. So we always get A.
Player.cpp
*//Now we use the object (of type A) detected by the raycast and stored in the variable (above mentioned), so:
//Get reference to the controller (Controller.cpp)
//Get the detected object in the variable from the controller, so in reality we have A and we must cast to B (to know if it is B1, B2, etc.)*
B1* child1 = Cast<B1>(A);
B2* child2 = Cast<B2>(A);
*etc.
//Get the the inherited component from B to attach the object (a blueprint based on B) to the player*
//This doesn’t work for B
child1->Box (the component is inherited from A, not created in B)->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, SocketName);
child2->Box (the component is inherited from A, not created in B)->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, SocketName);
, but it works for A:
parent->Box (the component is created in A)->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, SocketName);