1.定时器使用
//定义TimerHandle
FTimerHandle AttackDelayTimerHandle;
//检查是否激活定时器,若没有激活则SetTimer,0.2s后运行SetCanAttack函数
if (!GetWorld()->GetTimerManager().IsTimerActive(AttackDelayTimerHandle))
{
GetWorld()->GetTimerManager().SetTimer(AttackDelayTimerHandle, this, &ARPGCharacter::SetCanAttack, 0.2f, false);
}
//记得用完关闭TimerHandle
GetWorld()->GetTimerManager().ClearTimer(AttackDelayTimerHandle);
2.Timeline
//定义Curve(相当于你想要的Timeline曲线),记得在编辑器里设置默认值
UPROPERTY(BlueprintReadWrite, EditAnywhere)
UCurveFloat* MeleeCurve;
//定义每个Tick执行的Timeline函数,CurveOut为曲线Curve每个时刻对应的纵坐标值
UFUNCTION()
void OnMeleeCamTimelineTick(float CurveOut); // Timeline 更新时调用
//BeginPlay中
if (MeleeCurve)
{
FOnTimelineFloat OnTimelineFloatEvent;
OnTimelineFloatEvent.BindDynamic(this, &ARPGCharacter::OnMeleeCamTimelineTick);#定义每个Tick执行的函数
MeleeCamTimeline.AddInterpFloat(MeleeCurve, OnTimelineFloatEvent);#使用曲线MeleeCurve
MeleeCamTimeline.SetTimelineLength(ETimelineLengthMode::TL_LastKeyFrame);#定义Timeline长度
}
3.LineTraceSingle(射线检测)
//获得检测开始位置
FVector Start = FollowCamera->GetComponentLocation();
//获得检测结束位置
FVector End = FollowCamera->GetForwardVector() * 5000 + Start;
//定义需要忽略检测的Actor
TArray<AActor*> ActorsToIgnore;
ActorsToIgnore.Add(GetOwner());
ActorsToIgnore.Add(BowRef);
//定义检测到的结果
FHitResult OutHit;
#判断是否检测到Actor
if (UKismetSystemLibrary::LineTraceSingle(this, Start, End, TraceTypeQuery1, false, ActorsToIgnore, EDrawDebugTrace::None, OutHit, true))
{
//检测到后执行
}
4.读取DataTable
//定义待读取的DataTable
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UDataTable* ItemInfoDataTable;
//首先在空间中寻找是否有想要的枚举类型
const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EItemNames"), true);
static const FString ContextString(TEXT("Find FMyTableTest"));
//判断是否找到枚举类型
if (EnumPtr)
{
//将ItemName对应的枚举类型转为String
FString str = EnumPtr->GetNameStringByIndex(static_cast<int32>(ItemName));
if (ItemInfoDataTable)
{
//在DataTable中寻找是否有名为str的数据,并将数据赋予ItemData
FItemInfo* ItemData = ItemInfoDataTable->FindRow<FItemInfo>(FName(str), ContextString, true);
if (ItemData)
{
ItemInfo = *ItemData;
}
}
else
{
UKismetSystemLibrary::PrintString(this, "error");
}
}