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 ABaseActor* 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)
3: Is it possible to access the variables inside a class without creating an instance of it? Suppose I got a class
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. https://docs.unrealengine.com/latest...ary/index.html 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.
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.
Follow this question
Once you sign in you will be able to subscribe for any updates here