Reproduction Steps
-
Create MyActorComponent Class
-
Include “UMG” in the *.Build.cs file
-
Add the following line to your MyActorComponent.h
TWeakObjectPtr WidgetComp = nullptr;
Trying to compile will result in an error, that “TWeakObjectPtr can only be constructed with UObject types”.
Solution
Setting the default value in the constructor instead of the header file does not result in a compiler error. Another possible solution is including “Components/WidgetComponent.h” in the header file, which I wouldn’t recommend if you don’t understand circular dependency.
My Thoughts:
So I guess it is a bad practice to set the default value in the header file. But the behaviour is definately inconsistent, as the following line would not result in a compiler error. That’s why I thought I would share my findings.
TWeakObjectPtr<USceneComponent> WidgetComp = nullptr;
Full Compiler Error Log for Google:
CompilerResultsLog: Info c:\program files (x86)\epic games\4.12\engine\source\runtime\core\public\uobject\WeakObjectPtrTemplates.h(49): error C2338: TWeakObjectPtr can only be constructed with UObject types
CompilerResultsLog: Info c:\program files (x86)\epic games\4.12\engine\source\runtime\core\public\uobject\WeakObjectPtrTemplates.h(44): note: while compiling class template member function 'TWeakObjectPtr<UWidgetComponent,FWeakObjectPtr>::TWeakObjectPtr(const T *)'
CompilerResultsLog: Info with
CompilerResultsLog: Info [
CompilerResultsLog: Info T=UWidgetComponent
CompilerResultsLog: Info ]
CompilerResultsLog:Error: Error c:\users\user\documents\unreal projects\weakobjectptr_bug\source\weakobjectptr_bug\MyActorComponent.h(25) : note: see reference to function template instantiation 'TWeakObjectPtr<UWidgetComponent,FWeakObjectPtr>::TWeakObjectPtr(const T *)' being compiled
CompilerResultsLog: Info with
CompilerResultsLog: Info [
CompilerResultsLog: Info T=UWidgetComponent
CompilerResultsLog: Info ]