관리 메뉴

기억을 위한 기록들

[UE] 블루프린트에서 비동기방식을 다루고 싶다면 UBlueprintAsyncActionBase 클래스를 사용하자. 본문

UnrealEngine

[UE] 블루프린트에서 비동기방식을 다루고 싶다면 UBlueprintAsyncActionBase 클래스를 사용하자.

에드윈H 2025. 5. 31. 15:49

블루프린트에서 비동기방식을 다루고 싶다면 UBlueprintAsyncActionBase 클래스를 사용하자.

 

옛날에 블루프린트 상에서 다른 애셋에 대한 로딩을 하는 과정에서 기다리기 위해 임의로 delay 블루프린트 함수를 쓰는 걸 본 적이 있었다. 다른 좋은 방법은 그 당시에 생각나지 않았고, delay로 임의의 시간을 입력하고 그렇게 사용하는게 좋지 않다는 것만 인지하고 지나간 기억이 있다. 그러다가 최근에 알게된 클래스가 UBlueprintAsyncActionBase 이다.

 

예를 들어 서버 요청 응답, 외부 SDK 호출이나 다운로드, 내부 로직 처리 대기 등등 을 블루프린트에서 기다리고 싶을때 UBlueprintAsyncActionBase  클래스를 사용하면된다. 예로 살펴보면

 

해당 코드는 LyraStartGame 코드 중 일부이다.

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FExperienceReadyAsyncDelegate);

/**
 * Asynchronously waits for the game state to be ready and valid and then calls the OnReady event.  Will call OnReady
 * immediately if the game state is valid already.
 */
UCLASS()
class UAsyncAction_ExperienceReady : public UBlueprintAsyncActionBase
{
	GENERATED_UCLASS_BODY()

public:
	// Waits for the experience to be determined and loaded
	UFUNCTION(BlueprintCallable, meta=(WorldContext = "WorldContextObject", BlueprintInternalUseOnly="true"))
	static UAsyncAction_ExperienceReady* WaitForExperienceReady(UObject* WorldContextObject);

	virtual void Activate() override;

public:

	// Called when the experience has been determined and is ready/loaded
	UPROPERTY(BlueprintAssignable)
	FExperienceReadyAsyncDelegate OnReady;

private:
	void Step1_HandleGameStateSet(AGameStateBase* GameState);
	void Step2_ListenToExperienceLoading(AGameStateBase* GameState);
	void Step3_HandleExperienceLoaded(const ULyraExperienceDefinition* CurrentExperience);
	void Step4_BroadcastReady();

	TWeakObjectPtr<UWorld> WorldPtr;
};

 

 

블루프린트에서 비동기 호출이 필요한 곳에서 WaitForExperienceReady 함수를 호출해줄수 있게 static으로 선언되어 있다.

 

	UFUNCTION(BlueprintCallable, meta=(WorldContext = "WorldContextObject", BlueprintInternalUseOnly="true"))
	static UAsyncAction_ExperienceReady* WaitForExperienceReady(UObject* WorldContextObject);

 

그리고 블루프린트에서 살펴보면,

 

호출하고 다 됐을때 OnReady라는 곳으로 연결되어 있는데,

 

클래스에서 해당 변수의 BlueprintAssignable로 인해 노출되고 있음을 알 수 있다.

// Called when the experience has been determined and is ready/loaded
UPROPERTY(BlueprintAssignable)
FExperienceReadyAsyncDelegate OnReady;

 

 

해당 클래스에서의 마지막 스텝으로 다 준비 됐을때,  Step4_BroadcastReady 라는 함수가 호출되는데, 그 함수를 살펴보면,

 

void UAsyncAction_ExperienceReady::Step4_BroadcastReady()
{
	OnReady.Broadcast();

	SetReadyToDestroy();
}

 

OnReady가 호출되며 내부 SetReadyDestroy 함수를 호출하며 마친다.

 

이런식으로 블루프린트에서 비동기로 호출이 필요한 곳에 DECLARE_DYNAMIC_MULTICAST_DELEGATE 타입의 델리게이트 변수와 BlueprintAssignable UPROPERTY 선언이 포함된 변수를 만들어주고, 필요한 시점에 호출해주면 될 것 같다.