Search code examples
c++unreal-engine4

Unreal Editor Crashes when checking for Overlap


unreal beginner here, I just wrote the following Code in order to let the Trigger Volume open a door (Changes Actors Yaw). if a selected Actor (ActorThatOpens) enters it But when playing the Unreal Editor instatntly crashes. Can anyone help? I hope its clear enough what i tried to achiev, if not please ask.

Added this Code to the .h:

private:
     UPROPERTY(VisibleAnywhere)
     float OpenAngle = 90.0f;

     UPROPERTY(EditAnywhere)
     ATriggerVolume* PressurePlate;

     UPROPERTY(EditAnywhere)
     AActor* ActorThatOpens; //Pawn inherits from actor

    and this to the .cpp:

 void UOpenDoor::OpenDoor()
 {
     AActor* Owner = GetOwner();

     FRotator NewRotation = FRotator(0.0f, -160.0f, 0.0f);

     Owner->SetActorRotation(NewRotation);
 }

 // Called every frame
 (line 42) void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
 {
     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

     // ...

     if (PressurePlate->IsOverlappingActor(ActorThatOpens))
     {
         OpenDoor();
     } 
 }

  I did set the pressure Plate to the TriggerVolume and added this as component to the Actor (Door). I get this Error (UE Editor Crashes):

     Fatal error!

 Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000340

 UE4Editor-Engine.dll!0x00000000AB8790A1
 UE4Editor-BuildingGame-9929.dll!UOpenDoor::TickComponent() [d:\users\lf\documents\repos\unrealcourse\buildinggame\source\buildinggame\opendoor.cpp:42]
 UE4Editor-Engine.dll!0x00000000ABC2C05F
 UE4Editor-Engine.dll!0x00000000ABC51A15
 UE4Editor-Engine.dll!0x00000000AC7DD9DD
 UE4Editor-Engine.dll!0x00000000AC7E385D
 UE4Editor-Core.dll!0x00000000C09ED84D
 UE4Editor-Core.dll!0x00000000C09EDC6D
 UE4Editor-Core.dll!0x00000000C0A0CCEB
 UE4Editor-Engine.dll!0x00000000AC808861
 UE4Editor-Engine.dll!0x00000000AC819532
 UE4Editor-Engine.dll!0x00000000AC147B64
 UE4Editor-Engine.dll!0x00000000AC1501AD
 UE4Editor-Engine.dll!0x00000000ABF05B17
 UE4Editor.exe!0x000000007A96DEE3
 UE4Editor.exe!0x000000007A95EB40
 UE4Editor.exe!0x000000007A95EBBA
 UE4Editor.exe!0x000000007A970A29
 UE4Editor.exe!0x000000007A9722B6
 KERNEL32.DLL!0x00000000E8692774
 ntdll.dll!0x00000000E8F90D61
 ntdll.dll!0x00000000E8F90D61

(Followed this tutorial: https://www.udemy.com/unrealcourse/learn/v4/t/lecture/4621210?start=15)


Solution

  • Sai Narayan is probably right, you could check your pointers validity before calling game logic code.

    if ((PressurePlate != nullptr) && (ActorThatOpens != nullptr))
    {
        if (PressurePlate->IsOverlappingActor(ActorThatOpens))
        {
            OpenDoor();
        }
    }
    

    It prevents your program to crash in case you didn't set PresurePlate or ActorThatOpen before.

    Remember that TickComponent function will start just after actor owning your component is spawned at the game world. So TickComponent could be called before you acctually initialize your component members.