[UE] TObjectPtr을 SoftObjectPath로 만들기

TObjectPtr을 SoftObjectPath로 만들기
목차
TObjectPtr을 SoftObjectPath로 만들기
문제 상황
애셋을 로드하지 않고 SoftObjectPath로 사용하고 싶은 상황이 있다.
아래의 예시 상황
' 현재 재생중인 사운드 '와 ' 새롭게 재생될 사운드 '를 비교하고 싶다.
- Sound는 TObjectPtr <USoundBase> 변수다.
- NewBgmSound는 TSoftObjectPtr <USoundBase> 변수다.
void BgmSoundManager::PlayBgm(const TSoftObjectPtr<USoundBase> NewBgmSound) { // Bgm사운드가 현재 재생중인 Bgm과 같은 경우, 동일한 Bgm사운드를 새로 재생하지 않는다. if (CurrentAudioComponent->Sound 비교 NewBgmSound) { return; } // Bgm 재생 }
해결방법
FSoftObjectPath() 를 사용해서 현재의 사운드의 SoftObjectPath를 알아낼 수 있다.
void BgmSoundManager::PlayBgm(const TSoftObjectPtr<USoundBase> NewBgmSound) { // Bgm사운드가 현재 재생중인 Bgm과 같은 경우, 동일한 Bgm사운드를 새로 재생하지 않는다. if (FSoftObjectPath(CurrentAudioComponent->Sound) == NewBgmSound.ToSoftObjectPath()) { return; } // Bgm 재생 }
원리
struct FSoftObjectPath { FSoftObjectPath() = default; FSoftObjectPath(const FSoftObjectPath& Other) = default; FSoftObjectPath(FSoftObjectPath&& Other) = default; ~FSoftObjectPath() = default; FSoftObjectPath& operator=(const FSoftObjectPath& Path) = default; FSoftObjectPath& operator=(FSoftObjectPath&& Path) = default; template <typename T> FSoftObjectPath(const TObjectPtr<T>& InObject) { SetPath(InObject.GetPathName()); } FSoftObjectPath(const FObjectPtr& InObject) { SetPath(InObject.GetPathName()); } /** Construct from an existing object in memory */ FSoftObjectPath(const UObject* InObject) { if (InObject) { SetPath(InObject->GetPathName()); } } //... void SetPath(const FString& Path) { SetPath(FStringView(Path)); } }
FSoftObjectPath 생성자에 const TObjectPtr<T>& InObject 형태로 인자를 넘길 수 있다.
템플릿 함수 내에서 SetPath(InObject.GetPathName())가 불린다.
SetPath(InObject.GetPathName())의 리턴 값의 자료형은 FSoftObjectPath다
'⭐ Unreal Engine > UE Debugging Log' 카테고리의 다른 글
[UE] ViewBinding 문제 해결 (The field for source 'OO' exists but is not accessible at runtime.) (0) | 2025.01.20 |
---|---|
[UE] TSoftObjectPtr, TSoftClassPtr의 Valid 체크 (IsNull 사용) (0) | 2025.01.14 |
SpawnActor (스폰을 지연시키는 방법) (0) | 2024.12.15 |
[UE] Build.cs에서 빠진게 있어 빌드가 안 되는 경우 (0) | 2024.10.08 |
[UE] Shipping 모드에서 디버깅하기 (0) | 2024.10.02 |
댓글
이 글 공유하기
다른 글
-
[UE] ViewBinding 문제 해결 (The field for source 'OO' exists but is not accessible at runtime.)
[UE] ViewBinding 문제 해결 (The field for source 'OO' exists but is not accessible at runtime.)
2025.01.20 -
[UE] TSoftObjectPtr, TSoftClassPtr의 Valid 체크 (IsNull 사용)
[UE] TSoftObjectPtr, TSoftClassPtr의 Valid 체크 (IsNull 사용)
2025.01.14 -
SpawnActor (스폰을 지연시키는 방법)
SpawnActor (스폰을 지연시키는 방법)
2024.12.15 -
[UE] Build.cs에서 빠진게 있어 빌드가 안 되는 경우
[UE] Build.cs에서 빠진게 있어 빌드가 안 되는 경우
2024.10.08
댓글을 사용할 수 없습니다.