Hello I am trying to implement when player press E to the door, the door will open/close. But it keeps crashing whenever I try to call the function from ShooterCharacter. Any hints or suggestions?
Below is part of my code.
ShooterCharacter.h
class UOpenDoor;
public:
void Interact();
private:
UPROPERTY(EditDefaultsOnly)
TSubclassOf<UOpenDoor> DoorClasss;
UPROPERTY()
UOpenDoor *OpenDoor;
ShooterCharacter.cpp
void AShooterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction(TEXT("Interact"), IE_Pressed, this, &AShooterCharacter::Interact);
}
void AShooterCharacter::Interact()
{
OpenDoor->OpenDoor();
}
OpenDoor.h
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
void OpenDoor();
private:
float InitialYaw;
float CurrentYaw;
UPROPERTY(EditAnywhere)
float OpenAngle{90.f};
UPROPERTY(EditAnywhere)
float DoorOpenSpeed{0.8f};
UPROPERTY(EditAnywhere)
float DoorCloseSpeed{2.f};
// UPROPERTY(EditAnywhere)
// AActor *ActorThatTrigger;
};
OpenDoor.cpp
void UOpenDoor::BeginPlay()
{
Super::BeginPlay();
InitialYaw = GetOwner()->GetActorRotation().Yaw;
CurrentYaw = InitialYaw;
OpenAngle += InitialYaw;
UE_LOG(LogTemp, Warning, TEXT("Current Yaw is %f"), CurrentYaw);
//
// ...
}
}
void UOpenDoor::OpenDoor()
{
UE_LOG(LogTemp, Warning, TEXT("Current Yaw is %f"), CurrentYaw);
CurrentYaw = FMath::FInterpConstantTo(CurrentYaw, OpenAngle, GetWorld()->GetDeltaSeconds(),45);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
}
It crashes every time I press E. The crash reporter says “Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x000000bc”
Beside this crash, my idea is to call OpenDoor() from OpenDoor.cpp everytime player press E, and check if the player is within the reach to the door in that function too. Is this a good way to do so?
Thanks in advance!