Velocity Check crashes the Editor

Hello everybody,

so, where to start… I have a function, and i want that function to work only when my pawn is moving, so i put a check for Velocity like this…


if (GetPawn()->GetVelocity().operator!=(FVector(0.f)))
    {
        ...
        ...
        ...
    }

The problem is that this piece of code crashes the editor when i want to run the game in Multiplayer mode (2+ players), especially when the “Use Single Process” is on.
If the Mode is set to “Play As Listen Server”, only the Client crashes, when the “Play As Client” is set, it mostly loads fine, sometimes one of the clients fails, or Editor crashes.

I am not a programmer, tried to code just out of curiosity, so what i did wrong?

P. S. : The BP version works fine.

I was able to found this info, not sure it it can help through…


Fatal error!

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000000

0x000007feaf8f74a3 UE4Editor-MyProject.dll!AMyPlayerController::PlayerTick() [c:\users\profile name\documents\unreal projects\myproject\source\myproject\myplayercontroller.cpp:35]
0x000007fec6c85908 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec5e0f123 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec70b4a2d UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec8ab1798 UE4Editor-Core.dll!UnknownFunction ]
0x000007fec8ab1c56 UE4Editor-Core.dll!UnknownFunction ]
0x000007fec8ac5085 UE4Editor-Core.dll!UnknownFunction ]
0x000007fec710bb64 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec711b9a2 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec67eb6e4 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec67f74e2 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec3672085 UE4Editor-UnrealEd.dll!UnknownFunction ]
0x000007fec3f92346 UE4Editor-UnrealEd.dll!UnknownFunction ]
0x000000013f2a6225 UE4Editor.exe!UnknownFunction ]
0x000000013f2b596c UE4Editor.exe!UnknownFunction ]
0x000000013f2b59ea UE4Editor.exe!UnknownFunction ]
0x000000013f2c48da UE4Editor.exe!UnknownFunction ]
0x000000013f2c659a UE4Editor.exe!UnknownFunction ]
0x00000000779d571d kernel32.dll!UnknownFunction ]
0x0000000077c3385d ntdll.dll!UnknownFunction ]

When i added back the bool through which i can activate my piece of code (Like this: if (bIsCameraTrackingEnabled && GetPawn()->GetVelocity().operator!=(FVector(0.f))), the problem disappears… however, when i set the bool to true in constructor the crash is back :frowning:

Here is the info:


Fatal error!

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000000

0x000007feaf8574f0 UE4Editor-MyProject.dll!AMyPlayerController::PlayerTick() [c:\users\profile name\documents\unreal projects\myproject\source\myproject\myplayercontroller.cpp:36]
0x000007fec6fd5908 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec615f123 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec7404a2d UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec94b1798 UE4Editor-Core.dll!UnknownFunction ]
0x000007fec94b1c56 UE4Editor-Core.dll!UnknownFunction ]
0x000007fec94c5085 UE4Editor-Core.dll!UnknownFunction ]
0x000007fec745bb64 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec746b9a2 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec6b3b6e4 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec6b474e2 UE4Editor-Engine.dll!UnknownFunction ]
0x000007fec3692085 UE4Editor-UnrealEd.dll!UnknownFunction ]
0x000007fec3fb2346 UE4Editor-UnrealEd.dll!UnknownFunction ]
0x000000013f136225 UE4Editor.exe!UnknownFunction ]
0x000000013f14596c UE4Editor.exe!UnknownFunction ]
0x000000013f1459ea UE4Editor.exe!UnknownFunction ]
0x000000013f1548da UE4Editor.exe!UnknownFunction ]
0x000000013f15659a UE4Editor.exe!UnknownFunction ]
0x00000000779d571d kernel32.dll!UnknownFunction ]
0x0000000077c3385d ntdll.dll!UnknownFunction ]

Here is the full code that i am using right now:


// Called every frame
void AMyPlayerController::PlayerTick(float DeltaTime)
{
    Super::PlayerTick(DeltaTime);

    // If active, slowly returns the Camera behind the Character
    if (bIsCameraTrackingEnabled && !IsInputKeyDown(EKeys::RightMouseButton) && GetPawn()->GetVelocity().operator!=(FVector(0.f)) && FMath::IsWithin(FRotationMatrix(GetControlRotation()).GetScaledAxis(EAxis::Y).operator|(FRotationMatrix(GetPawn()->GetActorRotation()).GetScaledAxis(EAxis::Y)), 0.f, 1.5f))
    {
        const FRotator InterpRotation = FMath::RInterpTo(GetControlRotation(), GetPawn()->GetActorRotation(), DeltaTime, 0.2f);
        const FRotator CameraRotation(GetControlRotation().Pitch, InterpRotation.Yaw, GetControlRotation().Roll);
        SetControlRotation(CameraRotation);
    }
}

Hi!

You could use **ensure(expression); **or ensureMsg(expression, message); so the Editor won’t crash:

I changed your code:


// Called every frame
void AMyPlayerController::PlayerTick(float DeltaTime)
{
    Super::PlayerTick(DeltaTime);

    APawn *Pawn = GetPawn();

    // Check if APawn is not null without crashing, you can see the message in Output Log.
    if (ensureMsgf(Pawn, TEXT("Pawn is NULL.")))
    {
        // .operator!=() is the same as !=
        // .operator|() is the same as |

        // If active, slowly returns the Camera behind the Character
        if (bIsCameraTrackingEnabled &&
            !IsInputKeyDown(EKeys::RightMouseButton) &&
            //Pawn->GetVelocity().operator!=(FVector(0.f)) same as Pawn->GetVelocity() != FVector(0.f)
            !Pawn->GetVelocity().IsZero() &&
            //FMath::IsWithin(FRotationMatrix(GetControlRotation()).GetScaledAxis(EAxis::Y).operator|(FRotationMatrix(Pawn->GetActorRotation()).GetScaledAxis(EAxis::Y)), 0.f, 1.5f))
            FMath::IsWithin(FRotationMatrix(GetControlRotation()).GetScaledAxis(EAxis::Y) | FRotationMatrix(Pawn->GetActorRotation()).GetScaledAxis(EAxis::Y), 0.f, 1.5f))
        {
            const FRotator InterpRotation = FMath::RInterpTo(GetControlRotation(), Pawn->GetActorRotation(), DeltaTime, 0.2f);
            const FRotator CameraRotation(GetControlRotation().Pitch, InterpRotation.Yaw, GetControlRotation().Roll);
            SetControlRotation(CameraRotation);
        }
    }
}