Download

Novice UE4 race over C++ obstacles

Hi all
At the beginning i would like to sorry everyone for (maybe) doubling some threads by my post.
I am at the very start on my journey with Unreal Engine. I am in the middle of digging through all UE4 documentation, tutorials… etc.
But every time i am apprroaching some article or tutorial there is some kind of invisible mental wall which i cant go through.
So i decided to create this thread.
I would like to everyone who has same problems or (invisible wall) take part in it so every new person can benefit. Also mentors or experienced UE4 users can conifrm their knowledge.

First problem which i would to share is UCLASS macor problem.
in documentation

u can read some this like:

The *UCLASS macro gives the UObject a reference to a UCLASS that describes its Unreal-based type.
Each UCLASS maintains one Object called the “class default Object”, or CDO for short.
The CDO is essentially a default “template” Object, generated by the class constructor and unmodified thereafter.
Both the UCLASS and the CDO can be retrieved for a given Object instance, though they should generally be considered read-only.
The UCLASS for an Object instance can get accessed at any time using the GetClass() function.

First sentence is cool. Next on is ok. Problem start appear in the third one.
Could someone explain me clearly… How macro works in this moment.What is this CDO ?
CDO is essentially a default object “template” Object, generated by (which?) class constructor.
Thats in advance for answering.

I don t know what the CDO is but I can give you a brief overview of this system

You can derive new UCLASSes from an UObject or any other class that is derived from the UObject base class

UCLASS()
class UYourCustomClass : public UObject
{
GENERATE_BODY()

public:

}

Your new UYourCustomClass has a static function returning a UClass object

UClass* pClass = UYourCustomClass::StaticClass()

This static class can be used to check for types like this:

UObject* pActor1;

if( pActor1->IsA(UYourCustomClass::StaticClass()) == true )
{
// pActor1 points to a UObject of type UYourCustomClass
}

When creating new objects there are two helper functions:
**FObjectFinder **which can be used to find assets of some class, in this case USkeletalMesh.

static ConstructorHelpers::FObjectFinder<USkeletalMesh> CarMesh(TEXT("/Game/VehicleAdv/Vehicle/Vehicle_SkelMesh.Vehicle_SkelMesh"));
GetMesh()->SetSkeletalMesh(CarMesh.Object);

CarMesh.Object is a pointer to the actual mesh.
The text string is called “asset reference”, you can get it by right clicking on an asset in the content browser -> copy reference

**FClassFinder **which can be used to find UClasses that can later on be instantiated.
static ConstructorHelpers::FClassFinder<UObject> AnimBPClass(TEXT("/Game/VehicleAdv/Vehicle/VehicleAnimationBlueprint"));
GetMesh()->SetAnimInstanceClass(AnimBPClass.Class);

AnimBPClass.Class is a UCLASS not an object instance

I hope that gets you started

Hi EddyTheTank

Thank you so very much for this. That kind of answer i was looking for.But to be sure 100 % i got some questions regading to UCLASS macro
From what i have understood from your post:

1.UCLASS macro creates excact copy of your created class

2.THIS copied class has static function… Why is it called static

2a)Next part is really hard…

UObject* pActor1;

if( pActor1->IsA(UYourCustomClass::StaticClass()) == true )
{
// pActor1 points to a UObject of type UYourCustomClass
}

You pointng to function IsA()… The whole condition really confuses me… Is it possible to o comment more on this.

  1. This part explains a lot

When creating new objects there are two helper functions:
FObjectFinder which can be used to find assets of some class, in this case USkeletalMesh.

static ConstructorHelpers::FObjectFinder<USkeletalMesh> CarMesh(TEXT("/Game/VehicleAdv/Vehicle/Vehicle_SkelMesh.Vehicle_SkelMesh"));
GetMesh()->SetSkeletalMesh(CarMesh.Object);

my question is : Why you use <USkeletalMesh>? I mean < and > …
Also this expression
GetMesh()->SetSkeletalMesh(CarMesh.Object);

I dont exactly understand this syntax.

Thank you in advance

Those questions are more related to C++ than the Unreal Engine library, well read some courses about C++ and OOP (Oriented Object Programming), it’s will be useful for a better comprehension :slight_smile:

if( pActor1->IsA(UYourCustomClass::StaticClass()) == true )
{
// pActor1 points to a UObject of type UYourCustomClass
}

Well in this part, if you want to understand better, you need to know inheritance concept, to explain simply, imagine you have a Animal class (we call this one superclass because other class will inherit from this one in this example :)).
You make a Sheep class who inherit Animal, you have many Animals in your map and you need to check if your Animal is a Sheep, well your write something like this:

UAnimal* pAnimal;

if( pAnimal->IsA(USheep::StaticClass()) == true )
{
//This animal is a Sheep !
}

Finally, the last point, he use <> because it’s a function template, you specify the type of your object inside <>, for explain to the compiler that your object is a USkeletalMesh for example if your path go to a Skeletal Mesh.

I hope this help you ! :slight_smile:

Thank you JackBlue

It was very helpful… Your response explained me so much stuff…

Next problem which i would like to share with you is Constructors …
Especially this code snippet

AMyClass::AMyClass(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}

My questions are

  1. What is it FObjectInitializer?
    2.Why is it passsed to constructor?
  2. How does it look like? is it a class?
  3. HOW Super(ObjectInitializer) looks like ? Is this a function?
  4. Why Super() is called in Constructor?

Thanks everyone who is looks in this thread and tries to help me and new UE4 users…

Im pretty new to this too, but I thought I would help. In the unreal engine FObjectInitializer is a class that is passed into the constructor and helps initialize any class based on UClass or UObject. The Super is a function that essentially says “Hey do what the parent version of this does, then I can do mine.” For example, if you were setup input in a Player Controller, there is a function you can override called SetupInputComponent. When you use it you would put:



void SetupInputComponent()
{
    Super::SetupInputComponent();

    //Add whatever input bindings, etc you need
}


Again, this is saying do the parent classes SetupInputComponent, then do yours. So in:




AMyClass::AMyClass(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
    //Your constructor code here
}


this will call the base object’s (UClass or UObject) Constructor code and then yours. I recommend reading up on the Unreal Architechture and about the engines class structure. It helped me alot. https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/index.html

Hi,

FObjectInitializer allows you to SetDefaultSubobjecClass, which seems to allow you to specifiy what class should be instantiated internally or from a base class, as a subobject. I am looking for more information about this topic too.

See here:

Wow guys… NovanMK2, EddyTheTank

Thank you so much… Finally i got some knowledge node here :D…
Back to questions… In terms of FOobjectInitializer… i this explanation as it is for beginner…
I will just treat this as a blackbox…
Latetly i just signed up for Udemy C++ with Unreal… Course seems to be ok… But…
Pace of tutorials are ok at the beginning but there is moment when information gains speed of light and it really hard to be at the edge… I need to constantly repeat several times one lesson.
But my motivation is strong… and nothing will stop me :D…
The problem which i occured is this Code snippet…

void UPositionReport::BeginPlay()
{
Super:: BeginPlay();
UE_LOG(LogTemp,Warning, TEXT(“HelloWorld”));
}

I got problem with displaying this message in UE log window… What am I doing wrong in this situation…

SECOND PROBLEM…

…GetOwner() method…
This method returns pointer to function…
AActor* UActorComponent::GetOwner()const

But syntax for function pointers is little different from what i know… (correct me someone if i am wrong please)

type (*functionname) (type,type,…)

Why UE4 syntax here is different…

Thank you all …

Hi,

for the first problem:
I see nothing wrong here, the TEXT macro makes sure your strings are encoded (in UTF8? not sure), otherwise you would get ANSI chars.
How is UPositionReport declared? Is it an actor or a component? or a simple UObject?
Did you instantiate that Actor / Component at all?

for the second problem:

GetOwner returns a pointer to the actor this component is attached to. This has nothing to do with function pointers.

for example the CameraComponent of your player pawn would return a pointer to your player pawn class.

In UE4 you usually have a AController that controls a APawn see APawn::GetController, in your game rules class you can specify the PlayerController class you want the player to spawn with.