Get character to use custom character movement component

Hello!
I’ve fiddled around for quite a while with blueprints and I’m trying to make a transition over to C++. My learning project involves a character with a couple unique movement options like a double-jump and an air-dash, so I figured I’d create a class that inherits from the character movement component.

Now that I’ve successfully made my custom character movement component, I am attempting to get my character to use it. I first tried:

CharacterMovement = CreateDefaultSubobject<USTCharacterMovementComponent>(ACharacter::CharacterMovementComponentName);

within my character, but turns out CharacterMovement cannot be accessed, because it is private. What is the correct way to instruct my custom character to use my custom charactermovementcomponent in c++?

I’m pretty new to C++ specifically, so sorry for any obvious oversights. Thank you!

Can you paste the exact error ?

Absolutely.

error C2248: 'ACharacter::CharacterMovement': cannot access private member declared in class 'ACharacter'

Fairly certain this is because CharacterMovement was declared privately. I am just wondering what the ‘proper’ way to change the CharacterMovementComponent to a different component that inherits from CharacterMovementComponent is.

You’re right CharacterMovement is set to private. You will not be able to override it unless you modify the Character source code, which I guess, is not something you want to do.
Instead you should find another spot to put your custom movement logic, like why not in your derived character class directly ?
Otherwise you can attach your custom component to your character without specifically overriding CharacterMovement.
I am curious about how complex is your code you placed in your custom component ? I mean, what is the exact reason you need to derive and override the CharacterMovement ?

The custom movement code I have is very simple. To be honest, I just thought that was the ‘correct’ way to go about things, to put custom movement code in a module that handles it. There’s no reason I need to derive it at all, I suppose! I’ll simply put it in character from here on out, I suppose!

Yeah I totally agree with your reasonning. But if things are setup this way Epic guys must have a good reason too (there is a comment since 4.6 that deprecates the direct access to CharacterMovement)
Anyway, i am a bit useless here as I don’t see any other solution than writing your movement logic in the character itself or handling it in a standard inherited ActorComponent attached to your character.
Keep me in touch if you find some more answers.

You can override Character Movement Component in the constructor.
Instead of use default empty constructor, replace:

AMyCharacter::AMyCharacter()

to

AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer.SetDefaultSubobjectClass<UMyMovementComponent>(ACharacter::CharacterMovementComponentName))

Although it is for an older engine version, the Rama wiki it’s a usefull resource:

https://wiki.unrealengine.com/Custom_Character_Movement_Component

1 Like