How to assign TWeakObjectPtr right?

Somewhy I can’t use it for my class variable. F.e.:

TWeakObjectPtr MyWeakActor = *MyActor; - works okay

TWeakObjectPtr<AActor> MyWeakActor; *(defined in class)*
MyWeakActor = *MyActor;  -- will produce error "binary '=' : no operator found which takes a left-hand operand of type.. etc, etc"

Am I doing something wrong?

1 Like


Google for shared_ptr to understand why you should use it everywhere to help with ensuring that objects are freed.

Weak-ptrs are always are only initialized/assigned to shared_ptrs. They require calling lock() to verify that they are not deleted when accessed. This can only be done when initialized by a shared_ptr.

The unreal versions are probably better optimized than the standard library versions.

This talks about the standard library version, the Unreal version is based on the same ideas and should work the same way.

It was easier in result… I’ve tried to assign variable in const function (drunk)


The TWeakObjectPtr assignment operator takes a raw C++ pointer to its template type, so for a TWeakObjectPtr<AActor> the assignable type would be AActor*. Assuming that your variable MyActor already is a pointer to an actor, you can simply do:

TWeakObjectPtr<AActor> MyWeakActor;
MyWeakActor = MyActor;

You’re right, I’ve just missed that function was declared as “const”.

Garrrr x) thanks