UE4 C++常用代码及格式

1.Debug相关

LOG Debug

UE_LOG(LogTemp,Warning,Text(“Bginplay”));

在屏幕显示文字

GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Black,Text(“Beginplay”));

2.常用数据类型变化

int变为Int8 int16 int32 int64

string 变为 FString

新增FVector等类型

3.各继承类型前缀代表含义

A --Actor

F--Other

U--UE4

E--Enum

I--Interface

T--Template

S --Slate

4.宏 暴露变量给蓝图

UPROPERTY(EditAnywhere,BlueprintReadWrite,/*VisibleAnywhere,BlueprintReadOnly*/)

    Float Health = 100;

5.宏 暴露函数给蓝图

.h	//普通节点
UFUNCTION(BlueprintCallable, Category = “MyActorFunction”)	//Category给节点分组
    void PrintAString(FString String, UPARAM(Ref) int32& a,FString& OutString);
.cpp
void AMyActor::PrintAString(FString String, UPARAM(Ref) int32& a,FString& OutString)	//通过UPARAM(Ref) 数据类型& 引用传入值
{
    GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Black,String);
    OnGetPrintString(string);	//调用下面的可执行事件
    OutString = “OK”;	//通过FString&引用,获得返回值
}

 

.h   //事件节点类型1,不需要.cpp文件

UFUNCTION(BlueprintImplementableEvent, Category = “MyActorFunction”) //不能在C++里写节点的内容

    Void OnGetPrintString(const FString& String);
.h //事件节点类型2

UFUNCTION(BlueprintNativeEvent, /*BlueprintCallable,*/ Category = “MyActorFunction”) //可以在C++里写东西,但是会被蓝图覆盖,BlueprintCallable出来的节点内容会被该事件屏蔽

    Void OnGetPrintString(const FString& String);

    virtual void OnGetPrintString_Implementation(const FString& String);

.cpp

void AMyActor::OnGetPrintString_Implementation(const FString& String)

{

    GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Green,String);

}
.h

UFUNCTION(BlueprintPure) //BlueprintPure定义纯函数,一般都有返回值

    float GetHealth();
    
.cpp

float AMyActor::GetHealth() //可借鉴const FString& String返回多个值

{

    return Health;

}

6.多播代理

.h

#include下方定义

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMyDelegate, FString,Sting);

UPROPERTY(BlueprintAssignable)

    FMyDelegate MyDelegate;

.cpp

Void AMyActor::BeginPlay()//例如在BeginPlay时引用

{

    Super::BeginPlay();

    MyDelegate.Broadcast(“BeginPlay”);

}

7.单播代理

.h

#include下方定义

DECLARE_DYNAMIC_DELEGATE_OneParam(FMyDelegateOne, float,Sting);

UFUNCTION(BlueprintCallable)

    void DelegateTest(FMyDelegateOne MyDelegateOne);

.cpp

Void AMyActor::DelegateTest(FMyDelegateOne MyDelegateOne)

{

    HealthPercentage = Health/MaxHealth;

    DelegateOne.Execute(Healthpercentage);

}

8.结构体

.h

#include下方定义

USTRUCT(BuleprintType)

    struct FHealth

    {

        GENERATED_BODY()

        UPROPERTY(EditAnywhere,BlueprintReadWrite)

            Float Health = 100;

        UPROPERTY(EditAnywhere,BlueprintReadWrite)

            Float MaxHealth = 100;

        UPROPERTY(VisibleAnywhere,BlueprintReadOnly)

            Float HealthPercentage = Health / MaxHealth;

    };

UPROPERTY(EditAnywhere,BlueprintReadWrite)

    FHealth HealthS; //C++使用时可HealthS.Healthu提取,蓝图使用时可直接调用也可先Split Struct Pin解开结构体

9.枚举

.h

#include下方定义

UENUM(BuleprintType)

    enum class EMyENumType :uint8

    {

        Data1, //蓝图创建Enum变量,get变,switch提取变量

        E_Data2 UMETA=(DisplayName=”Data2”), //蓝图中屏蔽E_前缀,便于在代码中区分变量,而蓝图中仍显示为Data2

        Data3,

        //...

    };

UPROPERTY(EditAnywhere,BlueprintReadWrite)

    EMyENumType MyENumType = EMyENumType::Data1;

10.数组

.h

UFUNCTION(BlueprintCallable)

    void PrintAllString(TArray StringArray);

.cpp

Void PrintAllString(TArray StringArray)

{

    For(FString Each : StringArray) //等同于FoeEachLoop

    {

        GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Green,Each);

    }

}

11.元数据说明符

.h //使用方法

UFUNCTION(BlueprintPure,meta=(displayname = “CustomName”,Keywords=”CustomKeywords”, DevelopmentOnly/*,...*/)) //meta=(按所需功能填写)

    Float MyFunction()

.cpp

Float AMyActor::MyFunction()

{

    Return 0.f;

}
.h //一个例子,给蓝图引脚定义默认值

UFUNCTION(BlueprintCallable,meta=(AutoCreateRefTerm="b")) //不添加AutoCreateRefTerm则引脚必须从上一节点处获取参数

    void MyInput(int a, UPARAM(Ref) int32& b);

.cpp

void AMyActor::MyInput(int a, UPARAM(Ref) int32& b) //通过UPARAM(Ref) 数据类型& 引用 在蓝图中传入值

{ 

    //随便写

}

 

以上内容参考自@暂无名讳和官方文档

你可能感兴趣的:(UE4,UE4,C++)