TWeakObjectPtr doesn't work in 4.17 as before

Some code have worked without any issues in 4.16 version.

UPROPERTY()
TWeakObjectPtr<class ABaseCharacter> PawnInstigator;

UPROPERTY()
TWeakObjectPtr<class AActor> DamageCauser;

First part doesn’t work in 4.17 version

UPROPERTY()
TWeakObjectPtr<class ABaseCharacter> PawnInstigator;

With next notice

C:\Program Files (x86)\Epic
Games\UE_4.17\Engine\Source\Runtime\Core\Public\UObject/WeakObjectPtrTemplates.h(55):
error C2338: TWeakObjectPtr can only
be constructed with UObject types

There is no any problem with the second variable “DamageCauser”.

I compared “WeakObjectPtrTemplates.h” in old and new version. It’s the same.
Please help with correct declaration. “ABaseCharacter” inherited from “ACharacter”.

Hi,

I think the problem is that the compiler has not seen the definition of ABaseCharacter. You can tell by removing the ‘class’ (which is a habit best avoided anyway):

UPROPERTY()
TWeakObjectPtr<ABaseCharacter> PawnInstigator;

… and then you should get an error more like ‘unknown identifier: ABaseCharacter’. So you need to explicitly include the header containing ABaseCharacter at the top of this header:

#include "BaseCharacter.h"

This probably used to compile due to a different ordering of #includes.

Hope this helps,

Steve

Dear Steve,

I didn’t include some #includes to this header file.
It’s used in “BaseCharacter.h” also. I afraid to have a loop.
May be I tried already it. I’ll check.

I put away this issue and just commented all linked code.
And worked with other files.
This file had not be changed!!!

I turned back to this issue yesterday.
And miraculously this part of code started to work.
However, another issue appeared in the same file.

PawnInstigator(nullptrn);

It doesn’t work as it worked before. Do not compiled. Some problem with “nullptrn”.

Nevertheless next code works as usual

DamageCauser(nullptrn);

I changed the faulty part of code to

PawnInstigator();

and it was compiled.
But I didn’t check how does it work yet.

I don’t catch this thing. Definitely something is wrong.

I’ll test your solution anyway.

Thank you.

Do you mean nullptr, rather than nullptrn?

Regarding my suggestion above, the static assert is inside the constructor, so it’s only important that the constructor has seen the full definition of ABaseCharacter. So to avoid circular includes, it would be better like this:

// MyClass.h
#pragma once

#include "MyClass.generated.h"

class ABaseCharacter; // Forward declare this

UCLASS()
class UMyClass : public UObject
{
    GENERATED_BODY()

    UMyClass(); // Explicitly declare a constructor

    UPROPERTY()
    TWeakObjectPtr<ABaseCharacter> PawnInstigator;
};


// MyClass.cpp
#include "MyClass.h"
#include "BaseCharacter.h" // Make sure we have the full definition of ABaseCharacter

UMyClass::UMyClass()
{
} // PawnInstigator will be constructed here, where the full definition of ABaseCharacter is known

Steve

Thank you!