Download

Question about heritance for a beginner

Hello i m starting Unreal Engine and i have a problem

I dont understand how make my UCameraComponent visible in my child class

I have a character class and a weapon class and i want access camera in weapon class so

In character.cpp


// Sets default values
ASCharacter::ASCharacter()
{
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
SpringArmComp->bUsePawnControlRotation = true;
SpringArmComp->SetupAttachment(RootComponent);

CameraComp = CreateAbstractDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
CameraComp->SetupAttachment(SpringArmComp);
}

characterr.h

Normaly i have


protected:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category= "Components")
UCameraComponent* CameraComp;


So i change it for :


public:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category= "Components")
UCameraComponent* CameraComp;


and for me in weapon.cpp i just need call



FVector CameraForward = FVector(CameraComp->GetForwardVector());


but CameraComp is not available

So what is the good way ?

Thanks for your help

Hello, what do you mean with “is not available”? Is it a nullptr? Do you get compiler errors? Can you provide more Informations? What about weapon.h? Did you included the Character in your Weapon? Please post the Character.h Character.cpp Weapon.h Weapon.cpp

It sounds like you are trying to access the camera component from the Weapon class, while the camera component is defined in your character class. You will not be able to access the camera component from a completely different instance of a different class unless you’ve somehow passed the reference to the Weapon class. You are probably trying to obtain the forward vector to shoot in the right direction, but why are you trying to get the forward vector of the camera, and not simply that of the weapon? Then you don’t need a reference to the character or the camera component.

In addition to trying to access CameraComp from an other class do you call CreateDefaultSubobject to really create CameraComp somewhere?