// Fill out your copyright notice in the Description page of Project Settings. #include "DialogueSubsystem.h" #include "SpeechDataAsset.h" #include "Engine/AssetManager.h" #include "Kismet/GameplayStatics.h" UDialogueSubsystem* UDialogueSubsystem::Get(const UObject* WorldContext) { if(const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(WorldContext)) { return GameInstance->GetSubsystem(); } return nullptr; } void UDialogueSubsystem::Initialize(FSubsystemCollectionBase& Collection) { Super::Initialize(Collection); bDialogueLoaded = false; //Fetch dialogues if(UAssetManager* AssetManager = UAssetManager::GetIfInitialized()) { FStreamableManager& StreamableManager = AssetManager->GetStreamableManager(); AssetManager->GetAssetRegistry().GetAssetsByClass(FTopLevelAssetPath(USpeechDataAsset::StaticClass()->GetPathName()), AllAssetPaths, false); TArray AssetPaths; for(int32 i = 0; i < AllAssetPaths.Num(); i++) { AssetPaths.AddUnique(AllAssetPaths[i].GetSoftObjectPath()); } StreamableManager.RequestAsyncLoad(AssetPaths, FStreamableDelegate::CreateUObject(this, &UDialogueSubsystem::DialogueAssetsLoaded)); } } TArray> UDialogueSubsystem::GetDialogueForActor(const AActor* Actor) { TArray> ActorDialogue; if(!bDialogueLoaded) return ActorDialogue; for(int32 i = 0; i < AllSpeechData.Num(); i++) { if(AllSpeechData[i] && AllSpeechData[i].Get()->Npc.IsValid()) { if(AllSpeechData[i].Get()->Npc->GetClassPathName() == Actor->GetClass()->GetClassPathName()) { ActorDialogue.Add(AllSpeechData[i]); } } } return ActorDialogue; } bool UDialogueSubsystem::StartDialogue() { if(bDialogueStarted) return false; bDialogueStarted = true; return true; } void UDialogueSubsystem::EndDialogue() { bDialogueStarted = false; } void UDialogueSubsystem::DialogueAssetsLoaded() { for(int32 i = 0; i < AllAssetPaths.Num(); i++) { AllSpeechData.Add(Cast(AllAssetPaths[i].GetAsset())); } bDialogueLoaded = true; OnDialogueAssetsLoaded.Broadcast(); UE_LOG(LogTemp, Warning, TEXT("Loaded assets")); }