TWeakObjectPtr with Custom Character Class

Hello :slight_smile:

I’m having an issue with TWeakObjectPtr.

I’m declaring my damage structure as something like:

USTRUCT()
struct FTakeHitInfo
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
		TWeakObjectPtr<class AMyCharacter> PawnInstigator;

	UPROPERTY()
		TWeakObjectPtr<class AActor> DamageCauser;

	UPROPERTY()
		uint8 DamageEventClassID;

// etc.

};

The issue arise in the lines:

	UPROPERTY()
		TWeakObjectPtr<class AMyCharacter> PawnInstigator;

It doesn’t compile, with the error:


1>e:\unrealengine\epic games\4.7\engine\source\runtime\core\public\uobject\WeakObjectPtrTemplates.h(43): error C2338: TWeakObjectPtr can only be constructed with UObject types
1>          e:\unrealengine\epic games\4.7\engine\source\runtime\core\public\uobject\WeakObjectPtrTemplates.h(38) : while compiling class template member function 'TWeakObjectPtr<AMyCharacter,FWeakObjectPtr,FIndexToObject>::TWeakObjectPtr(const T *)'
1>          with
1>          
1>              T=AMyCharacter
1>          ]
1>          E:\Zoc_My_Workspace\MyGame\Source\My\Public\MyTypes.h(128) : see reference to function template instantiation 'TWeakObjectPtr<AMyCharacter,FWeakObjectPtr,FIndexToObject>::TWeakObjectPtr(const T *)' being compiled
1>          with
1>          
1>              T=AMyCharacter
1>          ]
1>          E:\Zoc_My_Workspace\MyGame\Source\My\Public\MyTypes.h(94) : see reference to class template instantiation 'TWeakObjectPtr<AMyCharacter,FWeakObjectPtr,FIndexToObject>' being compiled
1>e:\unrealengine\epic games\4.7\engine\source\runtime\core\public\uobject\WeakObjectPtrTemplates.h(43): error C2338: TWeakObjectPtr can only be constructed with UObject types
1>          e:\unrealengine\epic games\4.7\engine\source\runtime\core\public\uobject\WeakObjectPtrTemplates.h(38) : while compiling class template member function 'TWeakObjectPtr<AMyCharacter,FWeakObjectPtr,FIndexToObject>::TWeakObjectPtr(const T *)'
1>          with
1>          
1>              T=AMyCharacter
1>          ]
1>          e:\zoc_My_Workspace\MyGame\source\My\public\MyTypes.h(128) : see reference to function template instantiation 'TWeakObjectPtr<AMyCharacter,FWeakObjectPtr,FIndexToObject>::TWeakObjectPtr(const T *)' being compiled
1>          with
1>          
1>              T=AMyCharacter
1>          ]
1>          e:\zoc_My_Workspace\MyGame\source\My\public\MyTypes.h(94) : see reference to class template instantiation 'TWeakObjectPtr<AMyCharacter,FWeakObjectPtr,FIndexToObject>' being compiled


So, it says I can’t use TWeakObjectPtr with non-UObject classes.

My character declaration (in MyCharacter.h file) is:

UCLASS(config=Game)
class AHavenCharacter : public ACharacter
{
//etc.
};

But, if in the original FTakeHitInfo declaration I change to:

	UPROPERTY()
	TWeakObjectPtr<class ACharacter> PawnInstigator;

it works! :frowning:

I don’t really understand why (unless something is declared on MyTypes.generated.h). Also, I’m hesitant to use the ACharacter Class, to avoid casting it to AMyCharacter every time I want to check something.

Also, I can’t include MyCharacter.h on my FTakeHitInfo declaration header, since it would cause a circular includes issue.

Ideas on how to solve this? Should I just use the base ACharacter class and cast it to AMyCharacter every time I want to check something or is there a better way of doing this?

You’ve written AMyCharacter at one point but AHavenCharacter in another. I assume that’s just a typo in your post?

If so, the issue is probably caused from using your FTakeHitInfo struct in a .cpp file, rather than in the declaration itself, which seems fine. In the TakeHitInfo.cpp (if you have one) or any .cpp which accesses this weak object ptr, make sure you are including MyCharacter.h. I expect that should fix the problem.

Edit: If you have a constructor or anything else implemented inline in the TakeHitInfo header, that would likely cause this problem, in which case you’d need to move those implementations into a .cpp.

1 Like

Thanks @kamrann
I got the same problem, and including custom header solved the problem.