TObjectPtr을 SoftObjectPath로 만들기

 

 

 

 


 

 

TObjectPtr을 SoftObjectPath로 만들기


 

 

문제 상황

 

애셋을 로드하지 않고 SoftObjectPath로 사용하고 싶은 상황이 있다.

 

아래의 예시 상황

' 현재 재생중인 사운드 '와 ' 새롭게 재생될 사운드 '를 비교하고 싶다.

 

  • SoundTObjectPtr <USoundBase> 변수다.
  • NewBgmSoundTSoftObjectPtr <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다