【UnLua】在 Lua 中定义 UE 反射类型
用法
- 启动编辑器时遍历 Defines 目录下 lua 脚本来加载 UE 反射类型(开个临时的 Lua VM 即可)
- 直接像
-- define a uenum in lua
UEnum.EEnumGuestSomethingElse {
Value1 = 1;
Value2 = 2;
}
-- use it like a native uenum
UnLua.Log(UE.EEnumGuestSomethingElse.Value2)
====== UEnum ======
C++
UENUM(BlueprintType)
enum class ETest : uint8
{
Walking,
Running,
Sprinting,
ALS_MAX UMETA(DisplayName="ALS MAX")
};
Test.generated.h
#include "UObject/ObjectMacros.h"
#include "UObject/ScriptMacros.h"
#define FOREACH_ENUM_ETEST(op) \
op(ETest::Walking) \
op(ETest::Running) \
op(ETest::Sprinting) \
op(ETest::ALS_MAX)
enum class ETest : uint8;
template<> struct TIsUEnumClass<ETest> { enum { Value = true }; };
template<> LYRAGAME_API UEnum* StaticEnum<ETest>();
Test.gen.cpp
- 生成代码入口 FNativeClassHeaderGenerator::ExportGeneratedEnumInitCode
- 构造 UEnum 入口 UECodeGen_Private::ConstructUEnum
- 构造时机:static 注册,所以 C++ 启动时
void EmptyLinkFunctionForGeneratedCodeTest() {}
// Cross Module References
LYRAGAME_API UEnum* Z_Construct_UEnum_LyraGame_ETest();
UPackage* Z_Construct_UPackage__Script_LyraGame();
// End Cross Module References
static FEnumRegistrationInfo Z_Registration_Info_UEnum_ETest;
static UEnum* ETest_StaticEnum()
{
if (!Z_Registration_Info_UEnum_ETest.OuterSingleton)
{
Z_Registration_Info_UEnum_ETest.OuterSingleton = GetStaticEnum(Z_Construct_UEnum_LyraGame_ETest, Z_Construct_UPackage__Script_LyraGame(), TEXT("ETest"));
}
return Z_Registration_Info_UEnum_ETest.OuterSingleton;
}
template<> LYRAGAME_API UEnum* StaticEnum<ETest>()
{
return ETest_StaticEnum();
}
struct Z_Construct_UEnum_LyraGame_ETest_Statics
{
static const UECodeGen_Private::FEnumeratorParam Enumerators[];
#if WITH_METADATA
static const UECodeGen_Private::FMetaDataPairParam Enum_MetaDataParams[];
#endif
static const UECodeGen_Private::FEnumParams EnumParams;
};
const UECodeGen_Private::FEnumeratorParam Z_Construct_UEnum_LyraGame_ETest_Statics::Enumerators[] = {
{ "ETest::Walking", (int64)ETest::Walking },
{ "ETest::Running", (int64)ETest::Running },
{ "ETest::Sprinting", (int64)ETest::Sprinting },
{ "ETest::ALS_MAX", (int64)ETest::ALS_MAX },
};
#if WITH_METADATA
const UECodeGen_Private::FMetaDataPairParam Z_Construct_UEnum_LyraGame_ETest_Statics::Enum_MetaDataParams[] = {
{ "ALS_MAX.DisplayName", "ALS MAX" },
{ "ALS_MAX.Name", "ETest::ALS_MAX" },
{ "BlueprintType", "true" },
{ "ModuleRelativePath", "Common/Defines/Test.h" },
{ "Running.Name", "ETest::Running" },
{ "Sprinting.Name", "ETest::Sprinting" },
{ "Walking.Name", "ETest::Walking" },
};
#endif
const UECodeGen_Private::FEnumParams Z_Construct_UEnum_LyraGame_ETest_Statics::EnumParams = {
(UObject*(*)())Z_Construct_UPackage__Script_LyraGame,
nullptr,
"ETest",
"ETest",
Z_Construct_UEnum_LyraGame_ETest_Statics::Enumerators,
UE_ARRAY_COUNT(Z_Construct_UEnum_LyraGame_ETest_Statics::Enumerators),
RF_Public|RF_Transient|RF_MarkAsNative,
EEnumFlags::None,
(uint8)UEnum::ECppForm::EnumClass,
METADATA_PARAMS(Z_Construct_UEnum_LyraGame_ETest_Statics::Enum_MetaDataParams, UE_ARRAY_COUNT(Z_Construct_UEnum_LyraGame_ETest_Statics::Enum_MetaDataParams))
};
UEnum* Z_Construct_UEnum_LyraGame_ETest()
{
if (!Z_Registration_Info_UEnum_ETest.InnerSingleton)
{
UECodeGen_Private::ConstructUEnum(Z_Registration_Info_UEnum_ETest.InnerSingleton, Z_Construct_UEnum_LyraGame_ETest_Statics::EnumParams);
}
return Z_Registration_Info_UEnum_ETest.InnerSingleton;
}
struct Z_CompiledInDeferFile_FID_Lyra_Source_LyraGame_Common_Defines_Test_h_Statics
{
static const FEnumRegisterCompiledInInfo EnumInfo[];
};
const FEnumRegisterCompiledInInfo Z_CompiledInDeferFile_FID_Lyra_Source_LyraGame_Common_Defines_Test_h_Statics::EnumInfo[] = {
{ ETest_StaticEnum, TEXT("ETest"), &Z_Registration_Info_UEnum_ETest, CONSTRUCT_RELOAD_VERSION_INFO(FEnumReloadVersionInfo, 2142314138U) },
};
static FRegisterCompiledInInfo Z_CompiledInDeferFile_FID_Lyra_Source_LyraGame_Common_Defines_Test_h_3878057739(TEXT("/Script/LyraGame"),
nullptr, 0,
nullptr, 0,
Z_CompiledInDeferFile_FID_Lyra_Source_LyraGame_Common_Defines_Test_h_Statics::EnumInfo, UE_ARRAY_COUNT(Z_CompiledInDeferFile_FID_Lyra_Source_LyraGame_Common_Defines_Test_h_Statics::EnumInfo));
Python
- 创建入口 FPythonGeneratedEnumBuilder::RegisterDescriptors
- 创建时机 uenum -> GenerateEnum
@unreal.uenum()
class PyTestColor(unreal.EnumBase):
RED = unreal.uvalue(1, meta={"DisplayName": "Red (255, 0, 0)"})
GREEN = unreal.uvalue(2)
BLUE = unreal.uvalue(3)