/// This function is designed to be overridden by a blueprint. Do not provide a body for this function;
/// the autogenerated code will include a thunk that calls ProcessEvent to execute the overridden body.
BlueprintImplementableEvent,
/// This function is designed to be overridden by a blueprint, but also has a native implementation.
/// Provide a body named [FunctionName]_Implementation instead of [FunctionName]; the autogenerated
/// code will include a thunk that calls the implementation method when necessary.
BlueprintNativeEvent,
BlueprintImplementableEvent:在C++可以声明函数(不能定义,蓝图重写),在C++里调用该函数,蓝图重写实现该函数
BlueprintNativeEvent:在C++可以声明和定义函数,在C++里调用该函数,蓝图重写实现该函数(蓝图可以重写或不重写C++父类函数)
MyTPCharacter.h
函数声明(BlueprintImplementableEvent和BlueprintNativeEvent):
UFUNCTION(BlueprintImplementableEvent)
void TestA();
UFUNCTION(BlueprintImplementableEvent)
int TestB();
UFUNCTION(BlueprintImplementableEvent)
void TestC(const FString& str);
UFUNCTION(BlueprintImplementableEvent)
int TestD(const FString& str);
UFUNCTION(BlueprintNativeEvent)
void TestE();
UFUNCTION(BlueprintNativeEvent)
int TestF();
UFUNCTION(BlueprintNativeEvent)
void TestG(const FString& str);
UFUNCTION(BlueprintNativeEvent)
int TestH(const FString& str);
MyTPCharacter.cpp
函数定义(BlueprintImplementableEvent不能定义和BlueprintNativeEvent定义):
BlueprintNativeEvent修饰函数定义格式:函数名_Implementation
void AMyTPCharacter::TestE_Implementation()
{
PrintScreenStr("TestE_Implementation");
}
int AMyTPCharacter::TestF_Implementation()
{
PrintScreenStr("TestF_Implementation");
return 1;
}
void AMyTPCharacter::TestG_Implementation(const FString& str)
{
PrintScreenStr("TestG_Implementation " + str);
}
int AMyTPCharacter::TestH_Implementation(const FString& str)
{
PrintScreenStr("TestH_Implementation " + str);
return 1;
}
C++调用这些函数:
void AMyTPCharacter::OnRunEvent()
{
TestA();
TestB();
TestC("TestC");
TestD("TestC");
TestE();
TestF();
TestG("TestG");
TestH("TestH");
}
蓝图可以重写函数:如下图,选中重写实现
BlueprintImplementableEvent和BlueprintNativeEvent修饰函数,有返回值和没有返回值区别,如下
BlueprintImplementableEvent和BlueprintNativeEvent修饰函数,是否重写C++父类函数,如下
运行效果:
总结:BlueprintNativeEvent和BlueprintImplementableEvent修饰函数,如果没有函数返回值,那就是类似蓝图事件通知,如果有函数返回值,那就是类似蓝图函数重写;都可以传参和返回值;BlueprintNativeEvent修饰函数在C++有声明和定义实现,在蓝图可以覆写该函数,蓝图是否调用C++定义父类函数;BlueprintNativeEvent修饰函数只有声明,没有定义实现,只能在蓝图重写实现;BlueprintNativeEvent和BlueprintImplementableEvent修饰函数都只能在C++其他函数里调用。