Best way to link 2 actors through pointers.

When I got an actor which spawns another actor. And I need to connect both of those actors with the help of pointers, whats the best way to do so.

I was using UPROPERTY() pointer to do such kind of links.

AActor* Actor

But, I came across many places in the engine where a SharedPtr and a WeakPtr is used such kinds of links. What are the advantage of using them over normal pointer and how do I link the pointer in that way after spawning?

SharedPtr and WeakPtr are for anything derived from TSharedFromThis or anything you might call MakeShareable on (mostly structs).

UPROPERTY() on any pointer that is a UObject (Which includes AActor) will be hard reference
TWeakObjectPtr is the weak reference version for UObjects

See the docs for a more detailed description



Thanks for the link I didn’t came across that page some how…

If I got a character class which spawns an actor called weapon class, which pointers should I use to link them both?

You should be fine using a normal pointers.

Isn’t TSharedFromThis only necessary if you want to obtain the SharedPtr from the class itself? I don’t think you need to inherit from TSharedFromThis use it with SharedPtr.


Thats right, you can also call MakeShareable on an instance that is not based on TSharedFromThis to get a shared pointer.