Error when overriding APawn::GetMovementComponent()

Hey,

I’m working on creating my own APawn and UPawnMovementComponent derived classes, and the comments in Pawn.h suggest overriding GetMovementComponent() in classes which have their own movement component. The ACharacter class does exactly that with the following code:

UPROPERTY(Category=Character, VisibleAnywhere, BlueprintReadOnly)
TSubobjectPtr<class UCharacterMovementComponent> CharacterMovement;

virtual class UPawnMovementComponent* GetMovementComponent() const OVERRIDE { return CharacterMovement; }

I’ve attempted to do the same in my AShmupPawn class:

UPROPERTY(Category = ShmupPawn, VisibleAnywhere, BlueprintReadOnly)
TSubobjectPtr<class UShmupPawnMovementComponent> ShmupPawnMovement;
    
virtual class UPawnMovementComponent* GetMovementComponent() const OVERRIDE{ return ShmupPawnMovement; }

However, when compiling I get the following error:

error C2440: 'return' : cannot convert from 'const TSubobjectPtr<UShmupPawnMovementComponent>' to 'UPawnMovementComponent *'

I’ve looked through the code for ACharacter and UCharacterMovementComponent but I can’t find anything that addresses the issue, and the simple documentation in APawn implies that this should be straightforward. Any ideas?

Thanks.

Did you include your header such that the compiler knows the inheritance hierarchy?

#include "ShmupPawnMovementComponent.h"

The line:

TSubobjectPtr<class UShmupPawnMovementComponent> ShmupPawnMovement;

only forward declares your class that means the compiler knows this name but nothing else about the class.

You probably just need to call Get on your TSubobjectPtr.

virtual class UPawnMovementComponent* GetMovementComponent() const OVERRIDE { return ShmupPawnMovement.Get(); }

EDIT: Actually, TSubobjectPtr has a cast operator to the wrapped type, so it’s probably not that. It’s probably missing the type declaration like DarthB suggested.

No luck, I get the same error. The GetMovementComponent() implementation in ACharacter functions fine without a call to Get() too.

Thanks, that fixed it! I thought headers were automatically included in unreal, CharacterMovement.h for example is included in EngineComponentClasses.h.

Oh well, now I know better. Cheers for the help.