I implemented a custom CharacterMovementComponent yesterday and everything worked fine, even between editor sessions, meaning I closed the editor down and opened it up serveral times…
but today when I tried to fire the project up I got a crash at startup, went into debug and the Character class would crash when running the K2_OnMovementModeChanged becuase it tries to run CharacterMovement-> which is null at this point…
void ACharacter::OnMovementModeChanged(EMovementMode PrevMovementMode, uint8 PrevCustomMode)
{
if (!bPressedJump || !CharacterMovement->IsFalling())
{
ResetJumpState();
}
// Record jump force start time for proxies. Allows us to expire the jump even if not continually ticking down a timer.
if (bProxyIsJumpForceApplied && CharacterMovement->IsFalling())
{
ProxyJumpForceStartedTime = GetWorld()->GetTimeSeconds();
}
K2_OnMovementModeChanged(PrevMovementMode, CharacterMovement->MovementMode, PrevCustomMode, CharacterMovement->CustomMovementMode);
MovementModeChangedDelegate.Broadcast(this, PrevMovementMode, PrevCustomMode);
}
I have reverted all the code and deleted the “CustomCharacterMovement” class. Everything is exactly as it was before but now I dont crash at start up, but when I try to click the Character in the scene, (which is a blueprintclass based on the character in cpp) I crash. Assertion failed: IsViewportValid()
I can run several other editor projects without any issue so it cant be a hardware problem…
CustomCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "CustomCharacter.generated.h"
UCLASS()
class MY_API ACustomCharacter : public ACharacter
{
GENERATED_BODY()
public:
ACustomCharacter(const class FObjectInitializer& ObjectInitializer);
// ...
virtual void OnMovementModeChanged(EMovementMode PrevMovementMode, uint8 PreviousCustomMode = 0) override;
CustomCharacter.cpp
#include "MyGame.h"
#include "CustomCharacter.h"
#include "CustomGameMode.h"
#include "CustomMovementComponent.h"
ACustomCharacter::ACustomCharacter(const class FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer.SetDefaultSubobjectClass<UCustomMovementComponent>(ACharacter::CharacterMovementComponentName))
{
// ...
}
void ACustomCharacter::OnMovementModeChanged(EMovementMode PrevMovementMode, uint8 PreviousCustomMode)
{
Super::OnMovementModeChanged(PrevMovementMode, PreviousCustomMode);
if (PrevMovementMode == EMovementMode::MOVE_Falling &&
GetCharacterMovement()->MovementMode != EMovementMode::MOVE_Falling)
{
SetIsJumping(false);
}
}
CustomMovementComponent.h
#pragma once
#include "GameFramework/CharacterMovementComponent.h"
#include "CustomMovementComponent.generated.h"
UCLASS()
class MY_API UCustomMovementComponent : public UCharacterMovementComponent
{
GENERATED_BODY()
virtual float GetMaxSpeed() const override;
};
CustomMovementComponent.cpp
#include "MyGame.h"
#include "CustomMovementComponent.h"
#include "CustomCharacter.h"
float UCustomMovementComponent::GetMaxSpeed() const
{
float MaxSpeed = Super::GetMaxSpeed();
const ACustomCharacter* CharOwner = Cast<ACustomCharacter>(PawnOwner);
if (CharOwner)
{
if (CharOwner->IsTargeting() && !IsCrouching())
{
MaxSpeed *= CharOwner->GetTargetingSpeedModifier();
}
else if (CharOwner->IsSprinting())
{
MaxSpeed *= CharOwner->GetSprintingSpeedModifier();
}
}
return MaxSpeed;
}
Do anyone know what might have caused this? I find it strange that even reverting all the code back to the way it was before is still not making it work? Someone please…