I have been trying to create a custom component class in my C++ project and have been trying to spawn and add it to my custom controller class but have had no success. I have looked through the documentation and the answerhub but have yet to find an instance that uses my approach or solves my particular issue.
UCLASS()
class AMyPlayerController : public APlayerController
{
GENERATED_UCLASS_BODY()
UPROPERTY()
TSubobjectPtr<class UMyActorComponent> CustomComponent;
};
I had no compilation issue with the above logic.
AMyPlayerController::AMyPlayerController(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
CustomComponent = PCIP.CreateDefaultSubobject<UMyActorComponent>(this, TEXT("CustomComponent"));
}
When I attempted to create the component with the above logic I ran into the following compilation errors:
error C2440: 'initializing' : cannot convert from 'UMyActorComponent *' to 'UObject *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(567) : while compiling class template member function 'TSubobjectPtrConstructor::TSubobjectPtrConstructor(SubobjectType *)'
1> with
1> [
1> SubobjectType=UMyActorComponent
1> ]
1> c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(682) : see reference to function template instantiation 'TSubobjectPtrConstructor::TSubobjectPtrConstructor(SubobjectType *)' being compiled
1> with
1> [
1> SubobjectType=UMyActorComponent
1> ]
1> E:\Projects\CodeComponentTest\Source\CodeComponentTest\MyPlayerController.cpp(10) : see reference to class template instantiation 'TSubobjectPtrConstructor' being compiled
1>c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(569): error C2439: 'TSubobjectPtrConstructor::Object' : member could not be initialized
1> c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(565) : see declaration of 'TSubobjectPtrConstructor::Object'
I have a small amount of C++ experience and have been successful in the past in getting other functionality working in my C++ project but so far this has eluded me. I was hoping someone else in the community or within Epic might have an solution to this issue or can point out something really obvious that I might be missing? Thanks