Skip to content

Instantly share code, notes, and snippets.

@qiuwch
Last active March 18, 2019 22:39
Show Gist options
  • Save qiuwch/ea24cc739aa543f6677ee605eb5e75d3 to your computer and use it in GitHub Desktop.
Save qiuwch/ea24cc739aa543f6677ee605eb5e75d3 to your computer and use it in GitHub Desktop.
[UE4 code snippets] Snippets for some simple tasks in UE4 #tags: UE4, unrealcv
FString
FName
// How to convert between them
for (TActorIterator<AActor> ActorItr(World); ActorItr; ++ActorItr)
{
AActor* Actor = *ActorItr;
FVector WorldLocation = Actor->GetActorLocation();
FVector2D ScreenLocation;
APlayerController* PlayerController = World->GetFirstPlayerController();
PlayerController->ProjectWorldLocationToScreen(WorldLocation, ScreenLocation);
// AHUD *HUD = UGameplayStatics::GetPlayerController(Player, 0)->GetHUD();
AHUD *HUD = PlayerController->GetHUD();
// Check if this position is occluded, if yes, mark it with a different color.
FVector Start = PlayerPawn->GetActorLocation();
FVector End = Actor->GetActorLocation();
FHitResult HitResult;
// World->LineTraceSingleByChannel(HitResult, Start, End, ECollisionChannel::ECC_Pawn);
FCollisionObjectQueryParams ObjectQueryParams(FCollisionObjectQueryParams::AllObjects);
FCollisionQueryParams QueryParams = FCollisionQueryParams::DefaultQueryParam;
QueryParams.AddIgnoredActor(PlayerPawn);
World->LineTraceSingleByObjectType(HitResult, Start, End, ObjectQueryParams, QueryParams);
if (HitResult.GetActor() == Actor)
{
HUD->DrawRect(FLinearColor::Green, ScreenLocation.X, ScreenLocation.Y, 5, 5);
}
else
{
AActor* HitActor = HitResult.GetActor();
HUD->DrawRect(FLinearColor::Red, ScreenLocation.X, ScreenLocation.Y, 5, 5);
}
}
#include "DrawDebugHelpers.h" // This is written by Rama and include in UE4 officially now
// The wiki page for this helper file is [here](https://wiki.unrealengine.com/Draw_3D_Debug_Points,_Lines,_and_Spheres:_Visualize_Your_Algorithm_in_Action)
DrawDebugPoint(World, ActorLocation + Keypoint, 10, FColor::Red, true, 10);
FString ModelId = TEXT("");
UWorld* World = ;
for (TActorIterator<AActor> ActorItr(World); ActorItr; ++ActorItr)
{
AActor* Actor = *ActorItr;
if (Actor->GetName() == ModelId)
{
break;
}
}
// There are two types of ID in UE4. One is the editor label, the other is the id.
// The editor label is only available in editor and not available in a game binary.
void AShaderPluginDemoCharacter::ListAllActors()
{
ULevel *Level = GetLevel();
for (auto Actor : Level->Actors)
{
UE_LOG(LogTemp, Warning, TEXT("%s"), *Actor->GetActorLabel());
}
}
// Print log information to UE4 output console and the visual studio output console.
// Be careful with Warning and Error level message, these messages will prevent the packaging of a game.
UE_LOG(LogUnrealCV, Log, TEXT("The connection is gracefully closed by the client."));
// Useful Log levels: Log, Warning, Error.
// You can also create a new log category
DEFINE_LOG_CATEGORY(LogUnrealCV);

Useful snippets for some UE4 tasks.

  • draw_center.cpp How to draw actor center
  • get_actor.cpp How to get an actor using its id
  • draw_debug_3d.cpp How to draw 3D debug points
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment