[SOLVED] Unregistering/Registering UCharacterMovementComponent

Hi guys, I’m failing pretty hard (and feeling bad about it :() at Unregistering the UCharacterMovementComponent from the default character from the engine’s template and Registering MyUCharacterMovementComponent on it, which is a movement component that inherits from UCharacterMovementComponent and extends it a bit.

What I’ve done (and not sure about) is:
1)Inherits from UCharacterMovementComponent and manually provides it with a constructor (I don’t know if UE likes this though, since a constructor wasn’t provided by it)



UMyCharacterMovementComponent::UMyCharacterMovementComponent()
{
    bMoveWithFloor = false;
}


2)include MyUCharacterMovementComponent from the ACustomCharacter c++ and initialize it in the constructor so that it replace the default inherited UCharacterMovementComponent:



ACustomCharacter::ACustomCharacter()
{
    //...OTHER STUFF
    //...
    GetCharacterMovement()->DestroyComponent();
    GetCharacterMovement()->SetActive(false);
    NewMovementComponent = CreateDefaultSubobject<UMyCharacterMovementComponent>(TEXT("NewMovementComponent"));
    NewMovementComponent->UpdatedComponent = GetCapsuleComponent();
    CrouchedEyeHeight = NewMovementComponent->CrouchedHalfHeight * 0.80f; //I copied this line from the UCharacterMovementComponent initialization in the character class, just in case
    NewMovementComponent->SetActive(true);
    NewMovementComponent->RegisterComponent();
}


This is failing, and if I check in blueprint my character has a valid UMyCharacterMovementComponent but I am getting this error in the log:

Help me pls x_x

That’s not how it’s done - see ShooterGame.



AShooterCharacter::AShooterCharacter(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer.SetDefaultSubobjectClass<UShooterCharacterMovement>(ACharacter::CharacterMovementComponentName))
{
}


When I do that, the project crash altogether

I have this in the .h :


ACustomCharacter(const FObjectInitializer& ObjectInitializer);

and this in the .cpp:



ACustomCharacter::ACustomCharacter(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer.SetDefaultSubobjectClass<UMyCharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
{
    //Default stuff
}


I also went and check the ShooterGame, another difference is that while that code you showed is indeed in the constructor definition, the header have no constructor declaration in it, both for the ShooterCharacter and for the ShooterCharacterMovement.
If I don’t provide a constructor definition in the header, I get compile errors.

Maybe it has to do with the fact the class in that project are using the macro GENERATED_UCLASS_BODY() instead of GENERATED_BODY(), and that macro somehow provides a constructor declaration? I don’t know.

Regardless, if I try to do that it crash and I can’t open the project even if I rebuild the solution first from VS x_x

@TheJamsh My bad, I was crashing because of another reason, your solution works. Tanks! :smiley: