x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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)

 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. https://docs.unrealengine.com/latest...ary/index.html I have never used them yet. Where are exactly should I use pointers like TSharedRef?

Product Version: Not Selected
Tags:
more ▼

asked Sep 14 '14 at 01:49 PM in C++ Programming

avatar image

envenger
428 77 118 148

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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;

Cheers, Nick

more ▼

answered Sep 14 '14 at 02:02 PM

avatar image

Nick Darnell ♦♦ STAFF
6k 77 37 151

avatar image envenger Sep 14 '14 at 02:23 PM
 AActor* DefaultActor = AActor::StaticClass()->GetDefaultObject<AActor>();

Wow its good something like that exists.

avatar image envenger Sep 14 '14 at 03:48 PM

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?

avatar image Nick Darnell ♦♦ STAFF Sep 15 '14 at 09:21 PM

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

 GetDefault<AActor>()
 or
 GetMutableDefault<AActor>()

, both are global functions.

avatar image envenger Sep 22 '14 at 04:36 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question