Blueprint VS C++ в Unreal Engine: пример 7 эквивалентных реализаций
база)
void AMyActor::BeginPlay()
{
Super::BeginPlay();
UE_LOG(LogTemp, Warning, TEXT("Hello World!"));
}
Детектим FPS:
void AMyActor::BeginPlay()
{
Super::BeginPlay();
if (GetWorld())
{
const float FPS = 1.0f / GetWorld()->GetDeltaSeconds();
const int32 RoundedFPS = FMath::RoundToInt(FPS);
UE_LOG(LogTemp, Log, TEXT("FPS: %d"), RoundedFPS);
}
}
Трейс (Raycast) по направлению взгляда:
void AMyActor::BeginPlay()
{
Super::BeginPlay();
UWorld* World = GetWorld();
if (!World)
{
return;
}
APlayerController* PC = World->GetFirstPlayerController();
if (!PC || !PC->PlayerCameraManager)
{
return;
}
APlayerCameraManager* Cam = PC->PlayerCameraManager;
const FVector Start = Cam->GetCameraLocation();
const FVector End = Start + Cam->GetActorForwardVector() * 16000.0f;
FHitResult Hit;
FCollisionQueryParams Params;
Params.AddIgnoredActor(this);
if (World->LineTraceSingleByChannel(Hit, Start, End, ECC_Visibility, Params)
&& Hit.GetActor())
{
UE_LOG(
LogTemp,
Log,
TEXT("HIT ACTOR NAME: %s"),
*Hit.GetActor()->GetActorNameOrLabel()
);
}
}
Анимация вращения
void AMyActor::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
this->AddActorWorldRotation(FRotator(1.0f, 1.0f, 1.0f));
}
Сканим окружение трейсами:
void AMyActor::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
UWorld* World = GetWorld();
if (!World)
{
return;
}
const FVector Start = GetActorLocation();
const FVector Forward = GetActorForwardVector();
const FVector Axis = FVector::UpVector;
FCollisionQueryParams Params;
Params.AddIgnoredActor(this);
for (int32 Index = 0; Index <= 35; ++Index)
{
const float AngleDeg = Index * 10.0f;
const FVector Direction =
Forward.RotateAngleAxis(AngleDeg, Axis);
const FVector End =
Start + Direction * 16000.0f;
FHitResult Hit;
World->LineTraceSingleByChannel(
Hit,
Start,
End,
ECC_Visibility,
Params
);
}
}
Урон
float AMyActor::TakeDamage(
float Damage,
FDamageEvent const& DamageEvent,
AController* InstigatedBy,
AActor* DamageCauser
)
{
Health = FMath::Max(Health - Damage, 0.0f);
if (FMath::IsNearlyZero(Health))
{
this->Destroy();
}
return Damage;
}
Регдол
void AMyCharacter::OnDeath()
{
GetCharacterMovement()->DisableMovement();
GetCapsuleComponent()->SetCollisionResponseToAllChannels(ECR_Ignore);
GetMesh()->SetAnimInstanceClass(nullptr);
GetMesh()->SetCollisionResponseToAllChannels(ECR_Block);
GetMesh()->SetPhysicsAsset(DeathPhysicsAsset);
GetMesh()->SetSimulatePhysics(true);
GetWorld()->GetTimerManager().SetTimer(
DestroyTimerHandle,
this,
&AMyCharacter::DestroyAfterDelay,
3.0f,
false
);
}
void AMyCharacter::DestroyAfterDelay()
{
Destroy();
}
5 комментариев