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"

Pointer problem

Hello! I followed this tutorial about C++ programming: "3rd Person Power-Up Game with C++". I just don't understand why I should use pointers when I create a class type variable like in the "Pickup.h"

 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Pickup", meta = (AllowPrivateAccess = "true"))
 class UStaticMeshComponent* PickupMesh;

Can't I just use like this?

 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Pickup", meta = (AllowPrivateAccess = "true"))
 class UStaticMeshComponent PickupMesh;

And if not, why?

Product Version: UE 4.10
Tags:
more ▼

asked Apr 12 '16 at 02:25 PM in C++ Programming

avatar image

RadinQue
7 3 2 4

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

1 answer: sort voted first

You use pointers because Unreal has a garbage collector. Unreal will sense when the PickupMesh is created / destroyed and take actions accordingly. It is the same reason that you spawn an actor instead of creating one with new. Unreal's garbage collection is highly efficient so you need not worry. Also from a memory perspective, having a pointer to an external class ( this is called assosciation), has less overhead than creating a member variable. For eg : let's say you a class like this :

 class A
 {
 int i; 
 float j;
 }
 class B
 {
 class A* a;
 }

Now, B has a pointer so it's data will be 4 bytes in size. But suppose if you change the class to the following:

 class B
 {
 A a;
 }

Now B has an int and a float ( 8 bytes of data). This is just an example of a primitive class, imagine how much memory you would occupy when the class has huge data.

more ▼

answered Apr 12 '16 at 02:54 PM

avatar image

ash9991win
86 2 3 6

avatar image RadinQue Apr 12 '16 at 05:07 PM

So basically I could use the "A a;" instead of "A* a;" but it would result a lots of unnecessary data in the memory, right?

avatar image DrHobo Apr 12 '16 at 05:39 PM

If you can manage to compile successfully "A a" on UObjects and Actors then I'd be curious to know. :-) What I think ash9991win describes is the reasons why the engine is more likely to use pointers than member variables and the importance of the garbage collector in the whole process. But so far, all my tests in using non-pointers variables resulted in a compilation fail.

avatar image ash9991win Apr 12 '16 at 05:50 PM

Yep..Unreal doesn't let u create UObject as class members, only as pointers

avatar image RadinQue Apr 12 '16 at 06:00 PM

I see now. Many thanks to both of you! :)

avatar image ash9991win Apr 12 '16 at 06:01 PM

Sure :) Post any other questions if u have

(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