C++ Tanks vs Zombies 01: UProperty()...class? CreateDefaultSubject?

this tutorial series seems to be very nice! Looking forward to see more c++, although i’m already stuck in the first video of this series now :slight_smile:
There are two things I don’t understand, and i don’t wanna progress until i do understand these (propably) very basics:

  • In the tank.h header file we got these UPROPERTIES:

     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Tank", meta = (AllowPrivateAccess = "true"))

    UArrowComponent* TankDirection;

    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = “Tank”, meta = (AllowPrivateAccess = “true”))
    class UPaperSpriteComponent* TankSprite;
    The first looks normal to me - just a pointer to an UArrowComponent, but why is there a “class” tag on the second one? (I’m new to c++, guess it’s basic c++ coding, but some explanation would be nice about that)

  • The other thing is this CreateDefaultSubobject Methods in the tank.cpp. Haven’t found too much info about that on web. Is this just the proper way to create Objects/Instances of UE4 Objects similar to new()?

class means forward declaration and it is often used to avoid compile dependency in header file.
CreateDefaultSubobject is used to create objects in the class constructor.

Thank you very much for the clarfication. This helped a lot!

The class keyword is used in this context to forward declare the type. In c++ it’s used to avoid including the file holding that class. This helps a lot when you are in situations with circular dependencies where you have two classes that need variables to eachothers.

The CreateDefaultSubobject functions create default components for your class. If I recall correctly then it may only be used in constructors and not in any othe functions. If you want to create an object that’s not an actor during runtime then

UObject *Object = NewObject<UObject>();

Is the way to go. Actors are a bit different as you need to spawn them in the world, thus need a reference to the world. An example is:

UWorld *World = GetWorld();

if (World)

Then you can enter a multitude of params based on your needs.