Accessing Custom Character Movement Component

Hello, i’ve been following Rama’s wiki page on this subject and i’m having the following issue:

##Accessing Custom Character Movement Component

//Inside Character Class

UVictoryCharMoveComp* CustomCharMovementComp = Cast<UVictoryCharMoveComp>(CharacterMovement);

if(CustomCharMovementComp)
{
  CustomCharMovementComp->CallFunction();
}

My issue is that CharacterMovement is private on it’s parent class ACharacter, so the wiki page code won’t work.

There is a method used to access it GetCharacterMovement() but it gives me an error when i try to cast it for the child class.

This is my PlayerCharacter.h (child of ACharacter):

FORCEINLINE UPlayerCharacterMovementComponent* GetPlayerCharacterMovement() const { return Cast<UPlayerCharacterMovementComponent>(ACharacter::GetCharacterMovement()); }

I get the following error when i try to compile:

error C2664: 'UPlayerCharacterMovementComponent *TCastImpl<From,To,ECastType::UObjectToUObject>::DoCast(UObject *)': cannot convert argument 1 from 'UCharacterMovementComponent *' to 'UObject *'
2>        with
2>        [
2>            From=UCharacterMovementComponent,
2>            To=UPlayerCharacterMovementComponent
2>        ]

note: see reference to function template instantiation 'To *Cast<UPlayerCharacterMovementComponent,UCharacterMovementComponent>(From *)' being compiled
    2>        with
    2>        [
    2>            To=UPlayerCharacterMovementComponent,
    2>            From=UCharacterMovementComponent
    2>        ]

Anyone can share some insight on this matter? Thanks in advance.

Hey there, try doing the cast on a normal function that is not forceinline or is const to see if has a different result. Also make sure that you have the include for the custom character movement.

Try GetMovementComponent()

Hey thanks! I was able to make it work with the following on the cpp file:

UPlayerCharacterMovementComponent* APlayerCharacter::GetPlayerCharacterMovement() const {
	return Cast<UPlayerCharacterMovementComponent>(GetCharacterMovement());
}

Good to know :slight_smile:

Won’t work too :frowning: i think you can’t Cast from FORCEINLINE