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?
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
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);
}
}
}