NEED HELP: Camera Component cpp. subclass with adjustable Min and Max Field Of View (FOV) range (clamps)

I’m trying to make the Camera Component cpp. subclass with ability to Clamp (set Min and Max range of) FOV between adjustable Min and Max set values.

I tried my best to set up the clamp and build the project, but I’m struggling with couple of errors seen in the first shot.

What’s wrong with my scripts, and is there any fix?


3 Likes

Hello, if your UCndPlayerCameraComponent class derives correctly from UCameraComponent (as this part is not visible on your screenshots) I don’t see anything incorrect.
Maybe the fact that you are overriding public method as a protected is causing your errors? Because in CameraComponent SetFieldOfView is public.

3 Likes

(post deleted by author)

1 Like

I finally found the solution and proper answer. Pulled all-nighter figuring this out:

Subclass Header

UCLASS(ClassGroup = (ProjectName_Camera), meta = (BlueprintSpawnableComponent))
class PROJECT_NAME_API UCustomNameCameraComponent : public UCameraComponent
{
    GENERATED_UCLASS_BODY()

public:

    // DESCRIPTION: Set Min FOV range.
    UPROPERTY(EditAnywhere, Category = CameraSettings, meta = (ClampMin = "45", ClampMax = "180"))
    float MinFOV;

    // DESCRIPTION: Set Max FOV range.
    UPROPERTY(EditAnywhere, Category = CameraSettings, meta = (ClampMin = "45", ClampMax = "180"))
    float MaxFOV;

protected:

    // Set Clamp Ranges for Editor

#if WITH_EDITOR
    virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override
    {
        Super::PostEditChangeProperty(PropertyChangedEvent);
        FieldOfView = FMath::Clamp(FieldOfView, MinFOV, MaxFOV);
    }
#endif

    // Override SetFieldOfView Function
    virtual void SetFieldOfView(float InFieldOfView) override
    {
        // Clamp Field Of View between set Min and Max FOV clamp ranges.
        FieldOfView = FMath::Clamp(InFieldOfView, MinFOV, MaxFOV);
    }

};

Subclass .cpp

#include "CustomNameCameraComponent.h"

UCustomNameCameraComponent::UCustomNameCameraComponent(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
    // Default Values

    MinFOV = 45.0f;
    MaxFOV = 120.0f;
}

Thanks to this, the Field Of View from Base Class of Camera Component is now clamping between Min and Max set values in both editor and in use, and doesn’t go beyond them.

Kudos to me.

If you don’t know what’s going on, or you don’t read carefully, what I’m struggling with, and instead you immediately jump in with your “wisdom,” don’t even say anything.