Some basic Pointer related question

I have a few questions regarding use of pointers…

1: If I store an actor in a UPROPERTY() pointer say
and the original actor get deleted, so what happens to the pointer after the actor is deleted?

2: Pointers point to only one specific instanced class(reference). If I want to store a specific class which is not instanced, is it possible to do so? Like its information are accessible no matter what happens to the actor. Like how normal variables work.

I tried something like this as a test. It got compiled but when I tried to access any variables inside the non-pointer class, and got the crash(I accessed it inside the BeginPlay function)

AGeneratorClass *Generator;
Generator = GetWorld()->SpawnActor<AGeneratorClass>(PlanetGenerationClass);
AGeneratorClass Ga = *Generator;
int32 va = Ga.SomeVariable; //Crash

3: Is it possible to access the variables inside a class without creating an instance of it?
Suppose I got a class

TSubclassOf<class APlanetBase> TheClass;

I want to access a variable of the class. Is it possible for me to do so in any way other than spawning the actor in the world. Creating a pointer to the actor and accessing the value through that pointer?

4: Other than the normal UPROPERTY() pointer, there are many other types of smart pointer given by Epic.
I have never used them yet. Where are exactly should I use pointers like TSharedRef?

Pointers to UObjects associated with the UPROPERTY flag won’t be deleted, they’re reported to the garbage collector. Just so long as someone references them that is valid (reported to the GC root by someone)

The data you’re trying to access, if it’s not static data, then you need to reference it through an instance of the class. Now - if all you care about is the default value then you’re in luck, just access the Default object of the class, it’s an instance we spawn for every class.

AActor* DefaultActor = AActor::StaticClass()->GetDefaultObject<AActor>();

TSharedPtr, TSharedRef, TWeakPtr are not associated with the UObject system, UObjects are magical creatures that are counted invisibly by the garbage collector. It runs in the background looking for unreferenced stuff, similar to .Net or another managed language.

You use TSharedPtr…etc when using Slate, or another Non-UObject bit of C++ code that you still want to reference count and have cleaned up when you’re done.

If you’re ever in a situation where you want to hold onto a UObject ptr safely in C++ code but not report it as a reference, you can use the TWeakObjectPtr.

TWeakObjectPtr<AActor> WeakActor;


AActor* DefaultActor = AActor::StaticClass()->GetDefaultObject();

Wow its good something like that exists.

Also another question you didn’t answer.

If I got a few actors which I have to delete and spawn them some times later. What is the best way to store actors?

I guess something like this may work?

 Generator = GetWorld()->SpawnActor<AGeneratorClass>(PlanetGenerationClass);
AGeneratorClass Ga = *Generator;

And sometime later using the Ga while creating the pointer?

I would just add them to a TArray Uproperty. Also, instead of the other way of getting the default object you can use,


, both are global functions.

If I change the value of a default object pointer then try to spawn actors will all those actor have their value changed?