[UE5] TIL - 23 <TArray, GetAllActorsOfClass>

ChangJin·2024년 4월 21일
0

Unreal Engine5

목록 보기
51/102
post-thumbnail

2024-04-21

깃허브!
https://github.com/ChangJin-Lee/ARproject
https://github.com/ChangJin-Lee/ToonTank

느낀점
언리언 엔진에서 굉장히 많은 유형을 저장해둘 수 있는 TArray 배열에 대해 배웠다. TArray는 시퀀스인 컨테이너 클래스이다. GetAllActorsOfClass로 원하는 조건에 맞는 모든 Actor를 찾을 수 있었다. TArray에 해당하는 Actor들을 담아둘 수 있었다.

TIL

  • TArray
  • GetAllActorsOfClass

TArray

https://dev.epicgames.com/documentation/ko-kr/unreal-engine/array-containers-in-unreal-engine?application_version=5.3


  • 언리얼 엔진의 가장 간단한 컨테이너 클래스는 TArray (배열)입니다. TArray 는 유형이 같은 다른 오브젝트(, 다른 말로 "element", 요소 내지 엘리먼트)를 순서대로 정리하여 소속시키는 것을 담당하는 클래스입니다. TArray 는 시퀀스이므로, 그 엘리먼트는 잘 정의된 순서를 갖으며, 그 함수를 사용해서 해당 오브젝트와 순서를 결정론적으로 조작하게 됩니다.
  • 배열에 저장되는 오브젝트 유형을 엘리먼트 유형이라고 하고 이 엘리먼트는 전부 엄격하게 모두 같은 유형이다.
  • 유형이 다른 엘리먼트를 하나의 TArray에 저장할 수 없다.

  • 다음처럼 사용한다. C++의 vector, queue 같은 STL과 매우 유사하다. Actor 혹은 UActorComponent 유형의 타입들도 Array로 저장할 수 있게 해주기때문에 매우 중요한 개념이다!

TArray<int32> IntArray;
TArray<FString> StrArr;
StrArr.Add    (TEXT("Hello"));
StrArr.Emplace(TEXT("World"));
// StrArr == ["Hello","World"]

StrArr.Sort();
// StrArr == ["!","Brave","Hello","of","Tomorrow","World"]

TArray<int32> ValArr;
int32 Temp[] = { 10, 20, 30, 5, 10, 15, 20, 25, 30 };
ValArr.Append(Temp, ARRAY_COUNT(Temp));
// ValArr == [10,20,30,5,10,15,20,25,30]

ValArr.Remove(20);
// ValArr == [10,30,5,10,15,25,30]

int32 Index;
if (StrArr.Find(TEXT("Hello"), Index))
{
	// Index == 3
}

bool bLen5 = StrArr.ContainsByPredicate([](const FString& Str){
	return Str.Len() == 5;
});
bool bLen6 = StrArr.ContainsByPredicate([](const FString& Str){
	return Str.Len() == 6;
});

  • 신기한 것은 TArray에는 이진 힙 데이터 구조체를 지원하는 함수가 존재한다.
  • Heapify 함수를 사용하여 기존 배열을 힙으로 만들어버릴 수 있다.
	TArray<int32> HeapArr;
	for (int32 Val = 10; Val != 0; --Val)
		HeapArr.Add(Val);
	// HeapArr == [10,9,8,7,6,5,4,3,2,1]
	HeapArr.Heapify();
	// HeapArr == [1,2,4,3,6,5,8,10,7,9]
 



GetAllActorsOfClass

https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Engine/Kismet/UGameplayStatics/GetAllActorsOfClass?application_version=5.3

  • Find all Actors in the world of the specified class.

  • 월드에 있는 특정 클래스 액터를 모두 찾아준다.

  • 이것도 역시 Kismet에서 GameplayStatics.h을 헤더파일에 추가해야한다.

  • 매 프레임마다 사용해서는 안된다. 매우 느린 operation이기 때문이다.

  • 파라미터는 다음과 같다.

NameDescription
ActorClassClass of Actor to find. Must be specified or result array will be empty.
OutActorsOutput array of Actors of the specified class.


  • 사용법은 다음과 같다.
  • TActor로 AActor 유형을 모두 담을 TArray<AActor> 클래스를 선언한다.
  • 그런다음 this로 UObject인 WorldContextObject를 추가해주어야한다.
  • 다음 찾고자하는 클래스를 넣어주어야한다. 특정되어야만한다.
  • 담고자 하는 TArray 배열을 넣어준다.
  • TActors.Num()으로 배열의 개수를 얻을 수 있다!

int32 AToonTanksGameMode::GetTargetTower()
{
	TArray<AActor*> TActors;
	UGameplayStatics::GetAllActorsOfClass(this, ATower::StaticClass(), TActors);
	UE_LOG(LogTemp, Display, TEXT("%d"), TActors.Num());
	return TActors.Num();
}
profile
Unreal Engine 클라이언트 개발자

0개의 댓글