1 在头文件声明变量

 

  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
    int32 TotalDamage;
    
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
    float DamageTimeInSeconds;
    
    UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Transient, Category = "Damage")
    float DamagePerSecond;

 UPROPERTY() 宏命令 定义变量的属性  

EditAnywhere   在UI编辑器中可编辑   

BlueprintReadWrite  在蓝图中可读写

Category = "Damage" 在编辑器中以组“Damage”显示

2 变量初始化两种方式

AMyActor::AMyActor() : TotalDamage(200),
DamageTimeInSeconds(1.f)
{

}

 

AMyActor::AMyActor()
{
  ChangeValue();
}
AMyActor::ChangeValue() 
{

TotalDamage = 200;
    DamageTimeInSeconds = 1.f;
}
 

3 此时如果在编辑器中修改变量

TotalDamage
DamageTimeInSeconds

值,DamagePerSecond的值并不会随之改变 

需要添加edit回调

#if WITH_EDITOR
void AMyActor::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
  
changeValue();
  super::PostEditChangeProperty(PropertyChangedEvent);
} #endif

 4 c++ wizard 中方法如果想要在Blueprint 中显示,在方法声明前添加宏

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

5 c++ 调用Blueprint方法(未测试)

UFUNCTION(BlueprintImplementableEvent, Category="Damage")
void CalledFromCpp();

6 Blueprint可override方法(未测试)

UFUNCTION(BlueprintNativeEvent, Category="Damage")
void CalledFromCpp();

This version still generates the thunking method to call into the Blueprint VM. So how do you provide the default implementation? The tools also generate a new function declaration that looks like _Implementation(). You must provide this version of the function or your project will fail to link. Here is the implementation code for the declaration above.

void AMyActor::CalledFromCpp_Implementation()
{
    // Do something cool here
}