Custom Movement Component crashing

I have a custom movement component that I’m using with a custom character. My custom movement component is like this:

UCLASS()
class UBreakdanceMovementComponent : public UCharacterMovementComponent
{ ...

And my character class has a constructor like this:

ABBoyCharacter::ABBoyCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer.SetDefaultSubobjectClass<UBreakdanceMovementComponent>(ACharacter::CharacterMovementComponentName))
{
	UBreakdanceMovementComponent* movement = GetCharacterMovement<UBreakdanceMovementComponent>();
	movement->CreateCardboard(FVector(5,5,1));
	...

This works fine, I can use GetCharacterMovement in the constructor and it’s the right type of class. But I also have an input action bound to a key, and when I call it there it crashes because the cast is saying it’s a normal UCharacterMovementComponent, not my custom class. Ie, in the input function:

void ABBoyCharacter::Spin(const FInputActionValue& Value)
{
	UBreakdanceMovementComponent* movement = GetCharacterMovement<UBreakdanceMovementComponent>();
	if(movement->HasCardboard())
	...

That same typecasted call to GetCharacterMovement fails even though it worked fine in the constructor.

What?? Why?

Try this

That has the exact same problem. The typecast version of GetCharacterMovement uses CastChecked.

Ah. It’s because this is the main pawn from the template project, which instantiates a blueprint class (BP_ThirdPersonCharacter). I had to open that blueprint, open the full view, select the Character Movement Component, and set its “Component Class” to my custom class. Compile & Save that and everything works as expected.

Also worth noting that I’ve now created a new data-only blueprint inheriting from the custom character and it assigned that movement component automatically, which is better. This feels like something the engine didn’t update behind the scenes properly…

1 Like

why would you expect it to automatically change an already existing blueprint’s components without asking it to?