I think this may be an engine bug but it could be my code.
I have created a APlayerCamera in C++ and try to derive a blueprint class from it. This works fine, however, everytime I restart the editor the blueprint class is ‘invalid’ and when I try to open it the editor sometimes crashes.
Editor message on opening blueprint:
Blueprint could not be loaded because it derives from an invalid class. Check to make sure the parent class for this blueprint hasn’t been removed! Do you want to continue (it can crash the editor)?
playercamera.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "PlayerCamera.generated.h"
class UCameraComponent;
class USpringArmComponent;
UCLASS()
class APlayerCamera : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
APlayerCamera();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
UPROPERTY(EditDefaultsOnly, Category="Movement")
float CameraMoveSpeed = 1.0f;
UPROPERTY(EditDefaultsOnly, Category = "Movement")
float CameraZoomSpeed = 10.0f;
UPROPERTY(EditDefaultsOnly, Category = "Camera")
USceneComponent *Root = nullptr;
UPROPERTY(EditDefaultsOnly, Category = "Camera")
USpringArmComponent *SpringArmComponent = nullptr;
UPROPERTY(EditDefaultsOnly, Category = "Camera")
UCameraComponent *CameraComponent = nullptr;
float ReferenceMouseX = 0.0f;
float ReferenceMouseY = 0.0f;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UFUNCTION(BlueprintCallable, Category = "Movement")
void MoveAround(float mouseX, float mouseY);
UFUNCTION(BlueprintCallable, Category = "Movement")
void MoveForward(float speed);
UFUNCTION(BlueprintCallable, Category = "Movement")
void MoveSideways(float speed);
UFUNCTION(BlueprintCallable, Category = "Movement")
void SetReferenceMouseXY(float mouseX, float mouseY);
UFUNCTION(BlueprintCallable, Category = "Movement")
void Zoom(float speed);
};
playercamera.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "PlayerCamera.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
// Sets default values
APlayerCamera::APlayerCamera()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Root = CreateDefaultSubobject<USceneComponent>(FName("Root"));
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(FName("SpringArm"));
SpringArmComponent->AttachToComponent(Root, FAttachmentTransformRules::KeepRelativeTransform);
SpringArmComponent->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
CameraComponent = CreateDefaultSubobject<UCameraComponent>(FName("Camera"));
CameraComponent->AttachToComponent(SpringArmComponent, FAttachmentTransformRules::KeepRelativeTransform);
}
// Called when the game starts or when spawned
void APlayerCamera::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APlayerCamera::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void APlayerCamera::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void APlayerCamera::MoveAround(float mouseX, float mouseY)
{
// Yaw needs to be applied to this object
FRotator Rotator = this->GetActorRotation();
Rotator.Pitch = 0.0f;
Rotator.Roll = 0.0f;
Rotator.Yaw += ReferenceMouseX - mouseX;
this->SetActorRotation(Rotator);
// Pitch needs to be applied to the SpringArmComponent
FRotator SpringArmRotator = this->SpringArmComponent->GetComponentRotation();
SpringArmRotator.Pitch = FMath::Clamp(SpringArmRotator.Pitch + ReferenceMouseY - mouseY, -70.0f, -10.0f);
SpringArmRotator.Roll = 0.0f;
SpringArmRotator.Yaw = 0.0f;
this->SpringArmComponent->SetRelativeRotation(SpringArmRotator);
}
void APlayerCamera::MoveForward(float speed)
{
FVector Location = this->GetActorLocation();
Location += this->GetActorForwardVector() * speed * CameraMoveSpeed;
this->SetActorLocation(Location);
}
void APlayerCamera::MoveSideways(float speed)
{
FVector Location = this->GetActorLocation();
Location += this->GetActorRightVector() * speed * CameraMoveSpeed;
this->SetActorLocation(Location);
}
void APlayerCamera::SetReferenceMouseXY(float mouseX, float mouseY)
{
this->ReferenceMouseX = mouseX;
this->ReferenceMouseY = mouseY;
}
void APlayerCamera::Zoom(float speed)
{
// Invert scrolling
speed = speed * -1.0f;
SpringArmComponent->TargetArmLength += speed * CameraZoomSpeed;
}