尝试更改 TargetArmLength 时崩溃 (Unreal Engine C++)
Crash when trying to change the TargetArmLength (Unreal Engine C++)
每当我尝试更改时它都会崩溃 SpringArmComponent->TargetArmLength
,
即使我这样做:UE_LOG(LogTemp, Warning, TEXT("%f"), SpringArmComponent->TargetArmLength);
我做错了什么?
它在 ZoomIn()
和 ZoomOut()
函数中崩溃,但它在构造函数中有效。如果我只做 SpringArmComponent->TargetArmLength
它不会崩溃,但如果我尝试记录它就会崩溃。
.h文件片段:
class UCameraComponent;
class USpringArmComponent;
UCLASS()
class WELT_API ATPWBaseCharacter : public ACharacter
{
GENERATED_BODY()
public:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))
UCameraComponent* CameraComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))
USpringArmComponent* SpringArmComponent;
public:
// Sets default values for this character's properties
ATPWBaseCharacter(const FObjectInitializer& ObjInit);
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UFUNCTION(BlueprintCallable, Category = "Movement")
bool IsRunning() const;
private:
bool WantsToRun = false;
bool IsMovingForward = false;
void MoveForward(float Amount);
void MoveRight(float Amount);
void OnStartRunning();
void OnStopRunning();
void LookUp(float Amount);
void TurnAround(float Amount);
void ChangeCameraLen(bool bAmount);
public:
UFUNCTION(BlueprintCallable, Category = "Movement")
void ZoomIn();
UFUNCTION(BlueprintCallable, Category = "Movement")
void ZoomOut();
};
.cpp文件片段:
ATPWBaseCharacter::ATPWBaseCharacter(const FObjectInitializer& ObjInit) :
Super(ObjInit.SetDefaultSubobjectClass<UTPWCharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
{
PrimaryActorTick.bCanEverTick = true;
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
SpringArmComponent->SetupAttachment(GetRootComponent());
// SpringArmComponent->SocketOffset.Set(0, 0, 80);
SpringArmComponent->TargetArmLength = 400.0f;
SpringArmComponent->bUsePawnControlRotation = true;
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->SetupAttachment(SpringArmComponent, USpringArmComponent::SocketName);
CameraComponent->bUsePawnControlRotation = false;
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
}
void ATPWBaseCharacter::BeginPlay()
{
Super::BeginPlay();
}
void ATPWBaseCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// AddActorLocalRotation(QuatRotation, false, 0, ETeleportType::None);
FRotator NewRotationVector = FRotator(0.0f, GetVelocity().Rotation().Yaw, 0.0f); // pitch, yaw, roll
if (!NewRotationVector.IsZero())
{
FRotator NewRotation = FRotator(NewRotationVector);
FQuat QuatRotation = FQuat(NewRotation);
SetActorRotation(QuatRotation, ETeleportType::None);
}
}
void ATPWBaseCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &ATPWBaseCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ATPWBaseCharacter::MoveRight);
PlayerInputComponent->BindAxis("LookUp", this, &ATPWBaseCharacter::LookUp);
PlayerInputComponent->BindAxis("TurnAround", this, &ATPWBaseCharacter::TurnAround);
PlayerInputComponent->BindAction("WheelUp", IE_Pressed, this, &ATPWBaseCharacter::ZoomIn);
PlayerInputComponent->BindAction("WheelDown", IE_Pressed, this, &ATPWBaseCharacter::ZoomOut);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ATPWBaseCharacter::Jump);
PlayerInputComponent->BindAction("Run", IE_Pressed, this, &ATPWBaseCharacter::OnStartRunning);
PlayerInputComponent->BindAction("Run", IE_Released, this, &ATPWBaseCharacter::OnStopRunning);
}
void ATPWBaseCharacter::MoveForward(float Amount)
{
IsMovingForward = Amount > 0.0f;
// AddMovementInput(GetActorForwardVector(), Amount);
if ((Controller != nullptr) && (Amount != 0.0f))
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Amount);
}
}
void ATPWBaseCharacter::MoveRight(float Amount)
{
// AddMovementInput(GetActorRightVector(), Amount);
if ((Controller != nullptr) && (Amount != 0.0f))
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, Amount);
}
}
void ATPWBaseCharacter::LookUp(float Amount)
{
AddControllerPitchInput(Amount);
}
void ATPWBaseCharacter::TurnAround(float Amount)
{
AddControllerYawInput(Amount);
}
void ATPWBaseCharacter::OnStartRunning()
{
WantsToRun = true;
}
void ATPWBaseCharacter::OnStopRunning()
{
WantsToRun = false;
}
void ATPWBaseCharacter::ZoomIn()
{
// SpringArmComponent->TargetArmLength;
UE_LOG(LogTemp, Warning, TEXT("%f"), SpringArmComponent->TargetArmLength);
SpringArmComponent->TargetArmLength = 200.0f;
// UE_LOG(LogTemp, Warning, TEXT("%f"), SpringArmComponent->TargetArmLength);
// SpringArmComponent->SocketOffset = FVector(0, 0, 80);
}
void ATPWBaseCharacter::ZoomOut()
{
// SpringArmComponent->TargetArmLength;
UE_LOG(LogTemp, Warning, TEXT("%f"), SpringArmComponent->TargetArmLength);
SpringArmComponent->SocketOffset = FVector(0, 0, 80);
}
您是否有可能 运行 派生自此 class 的蓝图 actor 中的此人?如果是这样,我注意到这可能会导致组件创建出现问题,并会导致引擎崩溃,并且 return NULL 对于以其他方式正确设置的组件。
我建议也许尝试创建一个新的派生 class 并查看是否可行。我知道这是一个奇怪的建议,但它对我有用。
每当我尝试更改时它都会崩溃 SpringArmComponent->TargetArmLength
,
即使我这样做:UE_LOG(LogTemp, Warning, TEXT("%f"), SpringArmComponent->TargetArmLength);
我做错了什么?
它在 ZoomIn()
和 ZoomOut()
函数中崩溃,但它在构造函数中有效。如果我只做 SpringArmComponent->TargetArmLength
它不会崩溃,但如果我尝试记录它就会崩溃。
.h文件片段:
class UCameraComponent;
class USpringArmComponent;
UCLASS()
class WELT_API ATPWBaseCharacter : public ACharacter
{
GENERATED_BODY()
public:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))
UCameraComponent* CameraComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))
USpringArmComponent* SpringArmComponent;
public:
// Sets default values for this character's properties
ATPWBaseCharacter(const FObjectInitializer& ObjInit);
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UFUNCTION(BlueprintCallable, Category = "Movement")
bool IsRunning() const;
private:
bool WantsToRun = false;
bool IsMovingForward = false;
void MoveForward(float Amount);
void MoveRight(float Amount);
void OnStartRunning();
void OnStopRunning();
void LookUp(float Amount);
void TurnAround(float Amount);
void ChangeCameraLen(bool bAmount);
public:
UFUNCTION(BlueprintCallable, Category = "Movement")
void ZoomIn();
UFUNCTION(BlueprintCallable, Category = "Movement")
void ZoomOut();
};
.cpp文件片段:
ATPWBaseCharacter::ATPWBaseCharacter(const FObjectInitializer& ObjInit) :
Super(ObjInit.SetDefaultSubobjectClass<UTPWCharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
{
PrimaryActorTick.bCanEverTick = true;
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
SpringArmComponent->SetupAttachment(GetRootComponent());
// SpringArmComponent->SocketOffset.Set(0, 0, 80);
SpringArmComponent->TargetArmLength = 400.0f;
SpringArmComponent->bUsePawnControlRotation = true;
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->SetupAttachment(SpringArmComponent, USpringArmComponent::SocketName);
CameraComponent->bUsePawnControlRotation = false;
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
}
void ATPWBaseCharacter::BeginPlay()
{
Super::BeginPlay();
}
void ATPWBaseCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// AddActorLocalRotation(QuatRotation, false, 0, ETeleportType::None);
FRotator NewRotationVector = FRotator(0.0f, GetVelocity().Rotation().Yaw, 0.0f); // pitch, yaw, roll
if (!NewRotationVector.IsZero())
{
FRotator NewRotation = FRotator(NewRotationVector);
FQuat QuatRotation = FQuat(NewRotation);
SetActorRotation(QuatRotation, ETeleportType::None);
}
}
void ATPWBaseCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &ATPWBaseCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ATPWBaseCharacter::MoveRight);
PlayerInputComponent->BindAxis("LookUp", this, &ATPWBaseCharacter::LookUp);
PlayerInputComponent->BindAxis("TurnAround", this, &ATPWBaseCharacter::TurnAround);
PlayerInputComponent->BindAction("WheelUp", IE_Pressed, this, &ATPWBaseCharacter::ZoomIn);
PlayerInputComponent->BindAction("WheelDown", IE_Pressed, this, &ATPWBaseCharacter::ZoomOut);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ATPWBaseCharacter::Jump);
PlayerInputComponent->BindAction("Run", IE_Pressed, this, &ATPWBaseCharacter::OnStartRunning);
PlayerInputComponent->BindAction("Run", IE_Released, this, &ATPWBaseCharacter::OnStopRunning);
}
void ATPWBaseCharacter::MoveForward(float Amount)
{
IsMovingForward = Amount > 0.0f;
// AddMovementInput(GetActorForwardVector(), Amount);
if ((Controller != nullptr) && (Amount != 0.0f))
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Amount);
}
}
void ATPWBaseCharacter::MoveRight(float Amount)
{
// AddMovementInput(GetActorRightVector(), Amount);
if ((Controller != nullptr) && (Amount != 0.0f))
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, Amount);
}
}
void ATPWBaseCharacter::LookUp(float Amount)
{
AddControllerPitchInput(Amount);
}
void ATPWBaseCharacter::TurnAround(float Amount)
{
AddControllerYawInput(Amount);
}
void ATPWBaseCharacter::OnStartRunning()
{
WantsToRun = true;
}
void ATPWBaseCharacter::OnStopRunning()
{
WantsToRun = false;
}
void ATPWBaseCharacter::ZoomIn()
{
// SpringArmComponent->TargetArmLength;
UE_LOG(LogTemp, Warning, TEXT("%f"), SpringArmComponent->TargetArmLength);
SpringArmComponent->TargetArmLength = 200.0f;
// UE_LOG(LogTemp, Warning, TEXT("%f"), SpringArmComponent->TargetArmLength);
// SpringArmComponent->SocketOffset = FVector(0, 0, 80);
}
void ATPWBaseCharacter::ZoomOut()
{
// SpringArmComponent->TargetArmLength;
UE_LOG(LogTemp, Warning, TEXT("%f"), SpringArmComponent->TargetArmLength);
SpringArmComponent->SocketOffset = FVector(0, 0, 80);
}
您是否有可能 运行 派生自此 class 的蓝图 actor 中的此人?如果是这样,我注意到这可能会导致组件创建出现问题,并会导致引擎崩溃,并且 return NULL 对于以其他方式正确设置的组件。
我建议也许尝试创建一个新的派生 class 并查看是否可行。我知道这是一个奇怪的建议,但它对我有用。