记录点之前没见过的UE C++奇技淫巧 2

发布于 2023-09-12  200 次阅读


按索引获取枚举值

使用强转

int32 EnumIndex = 1;
MyEnum EnumValue = static_cast<MyEnum>(EnumIndex);

UE定时器的回调函数需要参数

如果你的回调函数需要参数,可以使用lambda表达式来传递参数。lambda表达式是一种匿名函数,可以在函数内部定义并传递参数。

例如,假设你的回调函数需要一个整数参数,你可以这样使用lambda表达式:

CPP复制int32 MyIntParam = 123;
FTimerHandle TimerHandle;
GetWorldTimerManager().SetTimer(TimerHandle, [this, MyIntParam]()
{
    MyFunctionWithParam(MyIntParam);
}, 2.0f, false);

获取蓝图中可能添加的组件 GetComponentByClass

UInworldCharacterComponent* InWorldComponent = Cast<UInworldCharacterComponent>(GetComponentByClass(UInworldCharacterComponent::StaticClass()));

打印枚举

获取枚举的静态类型,调用Class方法GetNameStringByValue

FString Description;
Description = FString::Printf(TEXT("Locomotion Type: %s"), *StaticEnum<EAILocomotionType>()->GetNameStringByValue((int64)LocomotionType));