So I got all my code looking exactly like the sample but for whatever reason when I add it to a second door and hit play the engine crashes.
Here is the crash report:
Fatal error!
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000350
0x00007ffb067618d9 UE4Editor-Engine.dll!UnknownFunction
0x00007ffb53a82423 UE4Editor-BuildingEscape-7197.dll!UOpenDoor::TickComponent() [D:\repos\03_BuildingEscape\BuildingEscape\Source\BuildingEscape\OpenDoor.cpp:47]
0x00007ffb06a9f3fa UE4Editor-Engine.dll!UnknownFunction
0x00007ffb06ac5464 UE4Editor-Engine.dll!UnknownFunction
0x00007ffb077072bc UE4Editor-Engine.dll!UnknownFunction
0x00007ffb08f922d8 UE4Editor-Core.dll!UnknownFunction
0x00007ffb08f9247a UE4Editor-Core.dll!UnknownFunction
0x00007ffb0774f81f UE4Editor-Engine.dll!UnknownFunction
0x00007ffb0775bd12 UE4Editor-Engine.dll!UnknownFunction
0x00007ffb06f7dc4f UE4Editor-Engine.dll!UnknownFunction
0x00007ffb06f88417 UE4Editor-Engine.dll!UnknownFunction
0x00007ffb04dc02f7 UE4Editor-UnrealEd.dll!UnknownFunction
0x00007ffb055dd396 UE4Editor-UnrealEd.dll!UnknownFunction
0x00007ff758e06bb1 UE4Editor.exe!UnknownFunction
0x00007ff758e1554c UE4Editor.exe!UnknownFunction
0x00007ff758e155ca UE4Editor.exe!UnknownFunction
0x00007ff758e2316c UE4Editor.exe!UnknownFunction
0x00007ff758e25b8e UE4Editor.exe!UnknownFunction
0x00007ffb5f7d4034 KERNEL32.DLL!UnknownFunction
0x00007ffb61b33691 ntdll.dll!UnknownFunction
Here is my header file
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine/TriggerVolume.h"
#include "OpenDoor.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UOpenDoor : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UOpenDoor();
protected:
// Called when the game starts
virtual void BeginPlay() override;
void OpenDoor();
void CloseDoor();
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
UPROPERTY(EditAnywhere)
float OpenAngle = -10.f;
UPROPERTY(EditAnywhere)
float CloseAngle = -90.f;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate;
UPROPERTY(EditAnywhere)
float DoorCloseDelay = 0.15f;
float LastDoorOpenTime;
AActor* ActorThatOpens; // remeber pawn inherits from actor
AActor* Owner; // owns door
};
Here is my .cpp
#include "OpenDoor.h"
#include "Gameframework/Actor.h"
// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UOpenDoor::BeginPlay()
{
Super::BeginPlay();
Owner = GetOwner();
ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
}
void UOpenDoor::OpenDoor()
{
Owner->SetActorRotation(FRotator(0.f, OpenAngle, 0.f));
}
void UOpenDoor::CloseDoor()
{
Owner->SetActorRotation(FRotator(0.f, CloseAngle, 0.f));
}
// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// Poll the trigger volume
// if the actor that opens is in the volume
if (PressurePlate->IsOverlappingActor(ActorThatOpens))
{
OpenDoor();
LastDoorOpenTime = GetWorld()->GetTimeSeconds();
}
// Check if time to close the door
// if door was open a second ago
//close door
if (GetWorld()->GetTimeSeconds() - LastDoorOpenTime > DoorCloseDelay)
{
CloseDoor();
}
}