UE4 and C++ development-Programming basics record (UE4+ code basics)

1. UE4 basic elements

①Actor

      We met the Actor again. The Actor persists in a level and usually contains several Actor components. Supports network replication and multiplayer games.
     Actor does not include position or direction. These things are stored in Root Component. Pawn in UE3 is also inherited by PlayerCharacter, because it has a MovementComponent that contains attributes such as jump and speed.Generated by SpawnActor()        Must be destroyed by Destroy< /span>    ②What is ActorComponent?        Cannot be garbage collected in the game
 
       



 

    Reusable functions can be added into Actor
    Contains some of the most interesting functions and events
    Can be accessed by Blueprint~

   ③Component example

   Scene Component adds shapes and connections
   Primitive Component adds collision and rendering
   UAudioComponent,UArrowComponent,UInputComponent,ULightComponent,UMeshComponent,UParticleSystemComponent, etc...< /span>

   If you have written UnrealScript, you will have a deep understanding of components.

   ④PrimitiveComponent component event example

   Hit- Called when strong again
   Begin/EndOverlap - In and out of a Trigger
   Begin/EndCursorOver Never used< a i=3> Clicked/Released No explanation    InputTouchBegin/End    Begin/EndTouchOver


2: Code basicsClass naming prefix
 

Unreal Engine provides you with tools to generate code during the build process. These tools have some class naming conventions. If the naming does not comply with the rules, a warning or error will be triggered. The class prefix list below illustrates the naming rules.

Classes derived from ActorThe prefix is ​​A, such as AController.
Classes derived from Object are prefixed with U, such as UComponent.
Enumerations are prefixed with E, such as EFortificationType.
Interface classes are usually prefixed with I, such as IAbilitySystemInterface.
The prefix of the template class is T, such as TArray.
Classes derived from SWidget (Slate UI) are prefixed with S, such as SButton.
The prefixes of other classes are the letters F , such as FVector.


3. Unreal C++ partial code explanation

#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class AMyActor : public AActor
{
    GENERATED_BODY()

public: 
    // 设置该 actor 属性的默认值
    AMyActor();
    // 游戏开始时或生成时调用
    virtual void BeginPlay() override;

    // 每帧调用
    virtual void Tick( float DeltaSeconds ) override;
};

AMyActor::AMyActor()

{

    // Set this actor to call Tick() every frame. You can set bCanEverTick to false to turn off this feature when not needed to improve performance.

    PrimaryActorTick.bCanEverTick = true;

}

3.1. Make the attributes appear in the editor
After the class is created. Now you can create some properties (the designer can set these properties in the Unreal Editor). You can use special macros UPROPERTY() to easily expose properties to the editor. For example: UPROPERTY(EditAnywhere) Macro is enough.

UCLASS()
class AMyActor : public AActor
{
    GENERATED_BODY()

    UPROPERTY(EditAnywhere)
    int32 TotalDamage;

    ...
};

After . You can see the value corresponding to the variable TotalDamage in the editor. And the value can be adjusted.
In addition, other attributes can be added.

In addition, you can also use macro definitions to make some properties change accordingly when their associated properties are changed:

void AMyActor::PostInitProperties()
{
    Super::PostInitProperties();

    CalculateValues();
}

void AMyActor::CalculateValues()
{
    DamagePerSecond = TotalDamage / DamageTimeInSeconds;
}
//通过WITH_EDITOR定义使这个函数的目标对象在编辑器中被更改时引擎将通知和运行这个函数。
#if WITH_EDITOR
void AMyActor::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
    CalculateValues();

    Super::PostEditChangeProperty(PropertyChangedEvent);
}
#endif

3.2, The blueprint calls a function in C++
The above mentioned how to expose properties to the blueprint. What about functions? Yes, we need another macro to define the function.

UFUNCTION(BlueprintCallable, Category="Damage")
void CalculateValues();

as above. C++ functions can be exposed to the reflection system through the UFUNCTION macro. And BlueprintCallable exposes it to the blueprint virtual machine. In this way, in the blueprint, you can use this function by right-clicking the shortcut menu.

3.3. Brief introduction to AActor
The AActor class is used as a basic object in the game scene. Therefore, objects that can be placed in the level scene can extend from this class, such as: AStaticMeshActor, ACameraActor, APointLight and other actors.
This class derives from UObject.
The life cycle of AActor is:

3.4, Unreal Reflection System
When programming with UE4 C++. You will also see many fields that are not C++ statements. For example, he will add UCLASS() to a class and so on. These are implemented by UE4 using its own reflection to enable dynamic functions such as garbage collection, serialization, network replication and Blueprint/C++ communication. You can choose to add these functions according to your needs. Just add the correct tags to the corresponding types to generate reflection data. Here are some basic tags

#include "MyObject.generated.h" //这个头文件包含虚幻引擎所有反射数据。必须在声明类型的头文件中将此文件作为最后的 include 包含。

UCLASS(Blueprintable)
class UMyObject : public UObject
{
    GENERATED_BODY()

public:
    MyUObject();

    UPROPERTY(BlueprintReadOnly, EditAnywhere)
    float ExampleProperty;

    UFUNCTION(BlueprintCallable)
    void ExampleFunction();
};

You'll also notice that you can add additional specifiers to the tags. Some common specifiers have been added here for display. Specifiers allow you to describe specific behaviors that a type possesses.

Blueprintable - This class can be extended by Blueprints.
BlueprintReadOnly - This property can only be read from Blueprints, not written to.
Category - Defines the section in which this property appears under the Editor's Details view. for organization.
BlueprintCallable - This function can be called from a Blueprint.
There are too many specifiers to list here. The following link can be used as a reference:

UCLASS specifier list

UPROPERTY specifier list

UFUNCTION specifier list

USTRUCT specifier list

3.5. Object/Actor iterator
is the for loop corresponding to U3D. First find all instances of classes derived from UObject in the level. Then iterate through them.

// 将找到当前所有的 UObjects(此处可以写入你自定义的类的类名) 实例
for (TObjectIterator<UObject> It; It; ++It)
{
    UObject* CurrentObject = *It;
    UE_LOG(LogTemp, Log, TEXT("Found UObject named:%s"), *CurrentObject.GetName());
}

Guess you like

Origin blog.csdn.net/2201_75598244/article/details/133717476