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:
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;”
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.
class AMyCharacter : public ACharacter
AMyCharacter(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());