How can I modify the character movement component in UE 4.9?

Trying to add some custom movement and want to use Approach 4 from:

I can’t figure out how to change the CharacterMovementComponent in my player controller to my derived character movement class “MyCharacterMovementComponent”. I was able to reparent my player controller from Character to “MyCharacter”, but cannot figure out how to modify “MyCharacter” so that “MyCharacterMovementComponent” is the inherited movement component.

The following guides are useful, but seemed to be based around an older version of source code:

http://onlogika.com/programming/custom-character-movement-in-custom-character/
http://error454.com/2015/03/20/ue4/movement/replication/

They all reference “const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)” which I dont have in my derived classes.

One of the guides suggests adding the following within the character class in order to allow the movement component to be changed, but warns that this will affect all other projects on the same computer:

“virtual class UCharacterMovementComponent* GetCharacterMovement() const;”

Thanks!

Still have the same question after upgrading to 4.10

Hello, did you find an answer to this ? I’m stuck in it also in 4.11.2

Same question.

I do this in 4.12.5,

In my pawn class, I create my custom movement component in my constructor

PawnMovementComponent = CreateDefaultSubobject(TEXT(“PawnMovement”));
PawnMovementComponent->SetUpdatedComponent(CollisionComponent);

and override this…

virtual UPawnMovementComponent* GetMovementComponent() const override { return PawnMovementComponent; }

I strictly followed A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums and worked.
Remember to modify your character class constructor at header to
MyCharacter(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());

Actually non-character Pawns work quite differently from Characters. They don’t have a default MovementComponent when they are created so you have to add your own, e.g. when subclassing Pawn with a Blueprint. GetMovementComponent()'s base implementation returns the first component of type UPawnMovementComponent. Therefore, as long as you make sure that there is only one MovementComponent on your pawn (which is in general the case), you don’t have to override GetMovementComponent().

I guess what you meant is that Characters are Pawns and therefore could use this method. The problem is that ACharacter still accesses ACharacter::CharacterMovement in its methods, ignoring APawn::GetMovementComponent().

According to the comments in Character.h, CharacterMovement will soon be private and all references to CharacterMovement in the code (except those that set it) should use GetCharacterMovement() instead. If this method becomes virtual and called consistenly in Character.cpp, then overriding it will indeed make the custom CharacterMovement used for motion. However, there would still remain a “hidden” default CharacterMovement on the character (hence gformiga suggesting to replace the class via ObjectInitializer).

Cool, worked for me on Unreal 4.13. At first I couldn’t believe you could actually replace a class just like that, but the doc clearly states it uses the static FNames to identify and replace default classes.

Final code:

MyCharacter.h

#include ...

class AMyCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	AMyCharacter(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());

    // ...
}

MyCharacter.cpp

#include ...
#include "MyCharacterMovementComponent.h"

AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer.SetDefaultSubobjectClass(ACharacter::CharacterMovementComponentName))
{
    // ...
}