From 935b4bca121b58688dc9176fe4f119efde7ce77b Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 13 Feb 2026 00:30:57 +0300 Subject: [PATCH 01/22] First Changes --- .../G2I/Private/Game/G2IGameSoundManager.cpp | 62 +++++++++++++++++++ Source/G2I/Public/Game/G2IGameSoundManager.h | 27 ++++++++ 2 files changed, 89 insertions(+) create mode 100644 Source/G2I/Private/Game/G2IGameSoundManager.cpp create mode 100644 Source/G2I/Public/Game/G2IGameSoundManager.h diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp new file mode 100644 index 0000000..ea9b98f --- /dev/null +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -0,0 +1,62 @@ +#include "Game/G2IGameSoundManager.h" +#define StartSize 20 +#include "G2I.h" +#include "Components/AudioComponent.h" + +void AG2IGameSoundManager::BeginPlay() +{ + for (int32 i = StartSize - 1; i >= 0; i--) { + IdStack.Push(i); + } +} + +int AG2IGameSoundManager::AddSound(USoundCue* NewSound) +{ + if (!NewSound) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager didn't get sound")); + return -1; + } + + UAudioComponent* AudioComponent = NewObject(); + if (!AudioComponent) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager can't create an audio component")); + return -1; + } + AudioComponent->SetSound(NewSound); + AudioComponent->RegisterComponent(); + + if (USceneComponent* Scene = GetRootComponent()) + { + AudioComponent->AttachToComponent( + Scene, + FAttachmentTransformRules::KeepRelativeTransform + ); + } + else { + UE_LOG(LogG2I, Warning, TEXT("Sound manager can't get the scene root component")); + } + + int32 NewSoundId = IdStack.Pop(); + ActiveSounds.Add(NewSoundId, AudioComponent); + AudioComponent->Play(); + + return NewSoundId; +} + +bool AG2IGameSoundManager::RemoveSound(int32 SoundId) +{ + if (SoundId < 0) { + UE_LOG(LogG2I, Warning, TEXT("Sound manager got wrong SoundId")); + return false; + } + UAudioComponent* Component = *ActiveSounds.Find(SoundId); + if (!Component) { + return false; + } + Component->Stop(); + Component->DestroyComponent(); + ActiveSounds.Remove(SoundId); + + IdStack.Push(SoundId); + return true; +} diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h new file mode 100644 index 0000000..9bc6489 --- /dev/null +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -0,0 +1,27 @@ +#pragma once + + + +#include "CoreMinimal.h" +#include "GameFramework/GameStateBase.h" +#include "G2IGameSoundManager.generated.h" + +class UAudioComponent; + +UCLASS() +class G2I_API AG2IGameSoundManager : public AGameStateBase +{ + GENERATED_BODY() + +private: + TArray IdStack; + UPROPERTY() + TMap ActiveSounds; + +protected: + virtual void BeginPlay() override; +public: + + int AddSound(USoundCue* NewSound); + bool RemoveSound(int SoundId); +}; From 4892d1ef60839702a080bd8e9ae207946dab27ee Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Thu, 19 Feb 2026 17:04:13 +0300 Subject: [PATCH 02/22] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9e4d22e..9664b5a 100644 --- a/.gitignore +++ b/.gitignore @@ -83,3 +83,4 @@ Plugins/**/Intermediate/* DerivedDataCache/* enc_temp_folder/* +TODO.txt From 92a73a2afed1dfd79daa8fd2c168df4e63de66fd Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Sat, 28 Feb 2026 00:35:35 +0300 Subject: [PATCH 03/22] Done base Sound Manager --- .../G2I/Private/Game/G2IGameSoundManager.cpp | 212 ++++++++++++++++-- Source/G2I/Public/Game/G2IGameSoundManager.h | 64 +++++- 2 files changed, 247 insertions(+), 29 deletions(-) diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index ea9b98f..31b3649 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -1,58 +1,210 @@ #include "Game/G2IGameSoundManager.h" -#define StartSize 20 +#define STARTSIZE 20 #include "G2I.h" #include "Components/AudioComponent.h" -void AG2IGameSoundManager::BeginPlay() +TObjectPtr UG2IGameSoundManager::GetAudioById(int32 SoundId) noexcept { - for (int32 i = StartSize - 1; i >= 0; i--) { + if (SoundId < 0) { + UE_LOG(LogG2I, Warning, TEXT("Sound manager got wrong SoundId")); + return TObjectPtr(); + } + + return *ActiveSounds.Find(SoundId); +} + +TObjectPtr UG2IGameSoundManager::Get(UObject* WorldContextObject) +{ + if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::ReturnNull)) + { + return World->GetSubsystem(); + } + + return nullptr; +} + +void UG2IGameSoundManager::Initialize(FSubsystemCollectionBase& Collection) +{ + Super::Initialize(Collection); + + for (int32 i = 0; i < STARTSIZE; ++i) + { IdStack.Push(i); } } -int AG2IGameSoundManager::AddSound(USoundCue* NewSound) +void UG2IGameSoundManager::Deinitialize() +{ + StopAllSounds(); + IdStack.Empty(); + ActiveSounds.Empty(); + + Super::Deinitialize(); +} + +int32 UG2IGameSoundManager::AddSound(TObjectPtr NewSoundConfig) { - if (!NewSound) { + if (!ensure(NewSoundConfig)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager didn't get sound")); return -1; } - - UAudioComponent* AudioComponent = NewObject(); - if (!AudioComponent) { + if (!ensure(NewSoundConfig->Sound)) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager didn't get sound")); + return -1; + } + TObjectPtr AudioComponent = NewObject(); + if (!ensure(AudioComponent)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager can't create an audio component")); return -1; } - AudioComponent->SetSound(NewSound); + AudioComponent->RegisterComponent(); + AudioComponent->SetSound(NewSoundConfig->Sound); + AudioComponent->SetWorldLocation(NewSoundConfig->WorldLocation); - if (USceneComponent* Scene = GetRootComponent()) - { + if (NewSoundConfig->AttachToComponent) { AudioComponent->AttachToComponent( - Scene, - FAttachmentTransformRules::KeepRelativeTransform - ); - } - else { - UE_LOG(LogG2I, Warning, TEXT("Sound manager can't get the scene root component")); + NewSoundConfig->AttachToComponent, + NewSoundConfig->AttachmentRules); } + AudioComponent->SetVolumeMultiplier(NewSoundConfig->VolumeMultiplier); + AudioComponent->SetPitchMultiplier(NewSoundConfig->PitchMultiplier); + int32 NewSoundId = IdStack.Pop(); ActiveSounds.Add(NewSoundId, AudioComponent); - AudioComponent->Play(); return NewSoundId; } -bool AG2IGameSoundManager::RemoveSound(int32 SoundId) +bool UG2IGameSoundManager::PlaySound(int32 SoundId) { - if (SoundId < 0) { - UE_LOG(LogG2I, Warning, TEXT("Sound manager got wrong SoundId")); + TObjectPtr Component = GetAudioById(SoundId); + if (!ensure(Component)) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); return false; } - UAudioComponent* Component = *ActiveSounds.Find(SoundId); - if (!Component) { + + Component->Play(); + + return true; +} + +bool UG2IGameSoundManager::StopSound(int32 SoundId) +{ + TObjectPtr Component = GetAudioById(SoundId); + if (!ensure(Component)) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); return false; } + + Component->Stop(); + + return true; +} + +void UG2IGameSoundManager::StopAllSounds() +{ + for (const auto& Pair : ActiveSounds) + { + if (ensure(Pair.Value)) + { + Pair.Value->Stop(); + } + } +} + +bool UG2IGameSoundManager::ChangeSoundVolume(int32 SoundId, float NewVolume) +{ + TObjectPtr Component = GetAudioById(SoundId); + if (!ensure(Component)) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); + return false; + } + + NewVolume = FMath::Clamp(NewVolume, 0.0f, 1.0f); + Component->SetVolumeMultiplier(NewVolume); + + return true; +} + +bool UG2IGameSoundManager::ChangeSoundPitch(int32 SoundId, float NewPitch) +{ + TObjectPtr Component = GetAudioById(SoundId); + if (!ensure(Component)) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); + return false; + } + + NewPitch = FMath::Clamp(NewPitch, 0.0f, 2.0f); + Component->SetPitchMultiplier(NewPitch); + + return true; +} + +bool UG2IGameSoundManager::ChangeSoundLocation(int32 SoundId, FVector NewLocation) +{ + TObjectPtr Component = GetAudioById(SoundId); + if (!ensure(Component)) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); + return false; + } + + Component->SetWorldLocation(NewLocation); + + return true; +} + +bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, + TObjectPtr NewAttachmentComponent, + FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform) +{ + if (!ensure(NewAttachmentComponent)) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager got wrong component to attach")); + return false; + } + TObjectPtr Component = GetAudioById(SoundId); + if (!ensure(Component)) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); + return false; + } + + Component->AttachToComponent(NewAttachmentComponent, AttachmentRules); + + return true; +} + +bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, + TObjectPtr NewAttachmentActor, + FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform) +{ + if (!ensure(NewAttachmentActor)) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager got wrong actor to attach")); + return false; + } + TObjectPtr RootComponent = NewAttachmentActor->GetRootComponent(); + if (!ensure(RootComponent)) { + UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get root component from actor to attach")); + return false; + } + TObjectPtr Component = GetAudioById(SoundId); + if (!ensure(Component)) { + return false; + } + + Component->AttachToComponent(RootComponent, AttachmentRules); + + return true; +} + + +bool UG2IGameSoundManager::RemoveSound(int32 SoundId) +{ + TObjectPtr Component = GetAudioById(SoundId); + if (!ensure(Component)) { + return false; + } + Component->Stop(); Component->DestroyComponent(); ActiveSounds.Remove(SoundId); @@ -60,3 +212,17 @@ bool AG2IGameSoundManager::RemoveSound(int32 SoundId) IdStack.Push(SoundId); return true; } + +void UG2IGameSoundManager::RemoveAllSounds() +{ + for (const auto& Pair : ActiveSounds) + { + if (ensure(Pair.Value)) + { + Pair.Value->Stop(); + Pair.Value->DestroyComponent(); + IdStack.Push(Pair.Key); + ActiveSounds.Remove(Pair.Key); + } + } +} diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h index 9bc6489..93326b5 100644 --- a/Source/G2I/Public/Game/G2IGameSoundManager.h +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -3,25 +3,77 @@ #include "CoreMinimal.h" -#include "GameFramework/GameStateBase.h" +#include "Subsystems/WorldSubsystem.h" #include "G2IGameSoundManager.generated.h" + class UAudioComponent; +USTRUCT(BlueprintType) +struct FSoundConfig +{ + GENERATED_BODY() + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Sound") + TObjectPtr Sound; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Location") + FVector WorldLocation = FVector::ZeroVector; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment") + TObjectPtr AttachToComponent; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment") + FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::SnapToTargetIncludingScale; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play", meta = (ClampMin = "0.0", ClampMax = "1.0")) + float VolumeMultiplier = 1.0f; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play", meta = (ClampMin = "0.1", ClampMax = "2.0")) + float PitchMultiplier = 1.0f; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Lifetime") + bool bAutoDestroy = true; +}; + UCLASS() -class G2I_API AG2IGameSoundManager : public AGameStateBase +class G2I_API UG2IGameSoundManager : public UWorldSubsystem { GENERATED_BODY() private: + + UPROPERTY() TArray IdStack; + UPROPERTY() - TMap ActiveSounds; + TMap> ActiveSounds; protected: - virtual void BeginPlay() override; + + TObjectPtr GetAudioById(int32 SoundId); public: - int AddSound(USoundCue* NewSound); - bool RemoveSound(int SoundId); + UFUNCTION(BlueprintPure, Category = "Sound Manager") + static TObjectPtr Get(UObject* WorldContextObject); + + virtual void Initialize(FSubsystemCollectionBase& Collection) override; + virtual void Deinitialize() override; + + int32 AddSound(TObjectPtr NewSoundConfig); + bool RemoveSound(int32 SoundId); + void RemoveAllSounds(); + + bool PlaySound(int32 SoundId); + bool StopSound(int32 SoundId); + void StopAllSounds(); + + bool ChangeSoundVolume(int32 SoundId, float NewVolume); + bool ChangeSoundPitch(int32 SoundId, float NewPitch); + bool ChangeSoundLocation(int32 SoundId, FVector NewLocation); + bool ChangeSoundAttachment(int32 SoundId, + TObjectPtr NewAttachementComponent, + FAttachmentTransformRules AttachmentRules); + bool ChangeSoundAttachment(int32 SoundId, + TObjectPtr NewAttachementActor, + FAttachmentTransformRules AttachmentRules); }; From f200b46fe629fd6f7f0d3c29c13273b9b22752d0 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 20 Mar 2026 00:19:25 +0300 Subject: [PATCH 04/22] =?UTF-8?q?=D0=A5=D0=B0=D0=B9=D0=BF=D0=BE=D0=B2?= =?UTF-8?q?=D1=8B=D0=B5=20=D1=84=D0=B8=D0=BA=D1=81=D1=8B=20=D0=B2=D1=81?= =?UTF-8?q?=D1=8F=D0=BA=D0=BE=D0=B3=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../G2I/Private/Game/G2IGameSoundManager.cpp | 71 +++++++++++++------ Source/G2I/Public/Game/G2IGameSoundManager.h | 28 ++++---- 2 files changed, 64 insertions(+), 35 deletions(-) diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index 31b3649..343ea70 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -2,18 +2,26 @@ #define STARTSIZE 20 #include "G2I.h" #include "Components/AudioComponent.h" +#include "Components/SceneComponent.h" +#include "Sound/SoundCue.h" -TObjectPtr UG2IGameSoundManager::GetAudioById(int32 SoundId) noexcept +UAudioComponent* UG2IGameSoundManager::GetAudioById(int32 SoundId) { if (SoundId < 0) { UE_LOG(LogG2I, Warning, TEXT("Sound manager got wrong SoundId")); return TObjectPtr(); } - return *ActiveSounds.Find(SoundId); + TObjectPtr* FoundAudio = ActiveSounds.Find(SoundId); + if (FoundAudio) { + return *FoundAudio; + } + + UE_LOG(LogG2I, Warning, TEXT("Sound manager: SoundId %d not found"), SoundId); + return nullptr; } -TObjectPtr UG2IGameSoundManager::Get(UObject* WorldContextObject) +UG2IGameSoundManager* UG2IGameSoundManager::Get(UObject* WorldContextObject) { if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::ReturnNull)) { @@ -31,6 +39,7 @@ void UG2IGameSoundManager::Initialize(FSubsystemCollectionBase& Collection) { IdStack.Push(i); } + CurrentNumberAvailable = STARTSIZE; } void UG2IGameSoundManager::Deinitialize() @@ -42,7 +51,7 @@ void UG2IGameSoundManager::Deinitialize() Super::Deinitialize(); } -int32 UG2IGameSoundManager::AddSound(TObjectPtr NewSoundConfig) +int32 UG2IGameSoundManager::AddSound(const FSoundConfig* NewSoundConfig) { if (!ensure(NewSoundConfig)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager didn't get sound")); @@ -52,25 +61,32 @@ int32 UG2IGameSoundManager::AddSound(TObjectPtr NewSoundConfig) UE_LOG(LogG2I, Warning, TEXT("The sound manager didn't get sound")); return -1; } - TObjectPtr AudioComponent = NewObject(); + UAudioComponent* AudioComponent = NewObject(); if (!ensure(AudioComponent)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager can't create an audio component")); return -1; } AudioComponent->RegisterComponent(); - AudioComponent->SetSound(NewSoundConfig->Sound); + AudioComponent->SetSound(NewSoundConfig->Sound.Get()); AudioComponent->SetWorldLocation(NewSoundConfig->WorldLocation); if (NewSoundConfig->AttachToComponent) { + FAttachmentTransformRules Rules( + NewSoundConfig->AttachmentRules, + NewSoundConfig->AttachmentRules, + NewSoundConfig->AttachmentRules, + false); AudioComponent->AttachToComponent( NewSoundConfig->AttachToComponent, - NewSoundConfig->AttachmentRules); + Rules); } AudioComponent->SetVolumeMultiplier(NewSoundConfig->VolumeMultiplier); AudioComponent->SetPitchMultiplier(NewSoundConfig->PitchMultiplier); - + if (IdStack.IsEmpty()) { + UpdateStackSize(); + } int32 NewSoundId = IdStack.Pop(); ActiveSounds.Add(NewSoundId, AudioComponent); @@ -79,7 +95,7 @@ int32 UG2IGameSoundManager::AddSound(TObjectPtr NewSoundConfig) bool UG2IGameSoundManager::PlaySound(int32 SoundId) { - TObjectPtr Component = GetAudioById(SoundId); + UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); return false; @@ -92,7 +108,7 @@ bool UG2IGameSoundManager::PlaySound(int32 SoundId) bool UG2IGameSoundManager::StopSound(int32 SoundId) { - TObjectPtr Component = GetAudioById(SoundId); + UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); return false; @@ -116,7 +132,7 @@ void UG2IGameSoundManager::StopAllSounds() bool UG2IGameSoundManager::ChangeSoundVolume(int32 SoundId, float NewVolume) { - TObjectPtr Component = GetAudioById(SoundId); + UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); return false; @@ -130,7 +146,7 @@ bool UG2IGameSoundManager::ChangeSoundVolume(int32 SoundId, float NewVolume) bool UG2IGameSoundManager::ChangeSoundPitch(int32 SoundId, float NewPitch) { - TObjectPtr Component = GetAudioById(SoundId); + UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); return false; @@ -144,7 +160,7 @@ bool UG2IGameSoundManager::ChangeSoundPitch(int32 SoundId, float NewPitch) bool UG2IGameSoundManager::ChangeSoundLocation(int32 SoundId, FVector NewLocation) { - TObjectPtr Component = GetAudioById(SoundId); + UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); return false; @@ -156,8 +172,8 @@ bool UG2IGameSoundManager::ChangeSoundLocation(int32 SoundId, FVector NewLocatio } bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, - TObjectPtr NewAttachmentComponent, - FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform) + USceneComponent* NewAttachmentComponent, + FAttachmentTransformRules AttachmentRules) { if (!ensure(NewAttachmentComponent)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager got wrong component to attach")); @@ -175,8 +191,8 @@ bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, } bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, - TObjectPtr NewAttachmentActor, - FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform) + AActor* NewAttachmentActor, + FAttachmentTransformRules AttachmentRules) { if (!ensure(NewAttachmentActor)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager got wrong actor to attach")); @@ -215,14 +231,23 @@ bool UG2IGameSoundManager::RemoveSound(int32 SoundId) void UG2IGameSoundManager::RemoveAllSounds() { - for (const auto& Pair : ActiveSounds) + for (auto Pair = ActiveSounds.CreateIterator(); Pair; ++Pair) { - if (ensure(Pair.Value)) + if (ensure(Pair.Value())) { - Pair.Value->Stop(); - Pair.Value->DestroyComponent(); - IdStack.Push(Pair.Key); - ActiveSounds.Remove(Pair.Key); + Pair.Value()->Stop(); + Pair.Value()->DestroyComponent(); } } + ActiveSounds.Empty(); + IdStack.Empty(); + CurrentNumberAvailable = STARTSIZE; + UpdateStackSize(); } + +void UG2IGameSoundManager::UpdateStackSize() { + for (int32 i = CurrentNumberAvailable; i < CurrentNumberAvailable * 2; ++i) { + IdStack.Push(i); + } + CurrentNumberAvailable *= 2; +} \ No newline at end of file diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h index 93326b5..84e260c 100644 --- a/Source/G2I/Public/Game/G2IGameSoundManager.h +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -1,13 +1,13 @@ #pragma once - - #include "CoreMinimal.h" #include "Subsystems/WorldSubsystem.h" #include "G2IGameSoundManager.generated.h" class UAudioComponent; +class USceneComponent; +class USoundCue; USTRUCT(BlueprintType) struct FSoundConfig @@ -23,7 +23,7 @@ struct FSoundConfig TObjectPtr AttachToComponent; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment") - FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::SnapToTargetIncludingScale; + EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play", meta = (ClampMin = "0.0", ClampMax = "1.0")) float VolumeMultiplier = 1.0f; @@ -44,22 +44,26 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem UPROPERTY() TArray IdStack; - + + int32 CurrentNumberAvailable; + UPROPERTY() TMap> ActiveSounds; + void UpdateStackSize(); + protected: - TObjectPtr GetAudioById(int32 SoundId); + UAudioComponent* GetAudioById(int32 SoundId); public: - UFUNCTION(BlueprintPure, Category = "Sound Manager") - static TObjectPtr Get(UObject* WorldContextObject); + UFUNCTION(BlueprintPure, Category = "Sound Manager", meta = (WorldContext = "WorldContextObject")) + static UG2IGameSoundManager* Get(UObject* WorldContextObject); virtual void Initialize(FSubsystemCollectionBase& Collection) override; virtual void Deinitialize() override; - int32 AddSound(TObjectPtr NewSoundConfig); + int32 AddSound(const FSoundConfig* NewSoundConfig); bool RemoveSound(int32 SoundId); void RemoveAllSounds(); @@ -71,9 +75,9 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem bool ChangeSoundPitch(int32 SoundId, float NewPitch); bool ChangeSoundLocation(int32 SoundId, FVector NewLocation); bool ChangeSoundAttachment(int32 SoundId, - TObjectPtr NewAttachementComponent, - FAttachmentTransformRules AttachmentRules); + USceneComponent* NewAttachementComponent, + FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform); bool ChangeSoundAttachment(int32 SoundId, - TObjectPtr NewAttachementActor, - FAttachmentTransformRules AttachmentRules); + AActor* NewAttachementActor, + FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform); }; From 7aa4ef15900979b404357caa844c63aba7c83f56 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 20 Mar 2026 01:04:59 +0300 Subject: [PATCH 05/22] Added Sound Component --- .../Private/Components/G2ISoundComponent.cpp | 133 ++++++++++++++++++ .../G2I/Private/Game/G2IGameSoundManager.cpp | 17 +-- .../G2I/Public/Components/G2ISoundComponent.h | 41 ++++++ Source/G2I/Public/Game/G2IGameSoundManager.h | 4 +- 4 files changed, 184 insertions(+), 11 deletions(-) create mode 100644 Source/G2I/Private/Components/G2ISoundComponent.cpp create mode 100644 Source/G2I/Public/Components/G2ISoundComponent.h diff --git a/Source/G2I/Private/Components/G2ISoundComponent.cpp b/Source/G2I/Private/Components/G2ISoundComponent.cpp new file mode 100644 index 0000000..a961989 --- /dev/null +++ b/Source/G2I/Private/Components/G2ISoundComponent.cpp @@ -0,0 +1,133 @@ +#include "Components/G2ISoundComponent.h" +#include "Game/G2IGameSoundManager.h" +#include "G2I.h" + +void UG2ISoundComponent::BeginPlay() +{ + Super::BeginPlay(); + + UWorld* World = GetWorld(); + if (!ensure(World)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the World in %s"), *GetName()); + return; + } + SoundManager = World->GetSubsystem(); + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + } +} + + +int32 UG2ISoundComponent::AddSound(const FSoundConfig* NewSoundConfig) +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return -1; + } + if (!NewSoundConfig) { + UE_LOG(LogG2I, Warning, TEXT("Got empty config in %s"), *GetName()); + return -1; + } + + FSoundConfig ConfigToSend = *NewSoundConfig; + + ConfigToSend.ResolvedAttachComponent = Cast(ConfigToSend.AttachToComponent.GetComponent(GetOwner())); + + return SoundManager->AddSound(&ConfigToSend); +} + +bool UG2ISoundComponent::PlaySound(int32 SoundId) +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->PlaySound(SoundId); +} + +bool UG2ISoundComponent::StopSound(int32 SoundId) +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->StopSound(SoundId); +} + +void UG2ISoundComponent::StopAllSounds() +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return; + } + SoundManager->StopAllSounds(); +} + +bool UG2ISoundComponent::ChangeSoundVolume(int32 SoundId, float NewVolume) +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->ChangeSoundVolume(SoundId, NewVolume); +} + +bool UG2ISoundComponent::ChangeSoundPitch(int32 SoundId, float NewPitch) +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->ChangeSoundPitch(SoundId, NewPitch); +} + +bool UG2ISoundComponent::ChangeSoundLocation(int32 SoundId, FVector NewLocation) +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->ChangeSoundLocation(SoundId, NewLocation); +} + +bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, + USceneComponent* NewAttachmentComponent, + FAttachmentTransformRules AttachmentRules) +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->ChangeSoundAttachment(SoundId, NewAttachmentComponent, AttachmentRules); +} + +bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, + AActor* NewAttachmentActor, + FAttachmentTransformRules AttachmentRules) +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->ChangeSoundAttachment(SoundId, NewAttachmentActor, AttachmentRules); +} + + +bool UG2ISoundComponent::RemoveSound(int32 SoundId) +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->RemoveSound(SoundId); +} + +void UG2ISoundComponent::RemoveAllSounds() +{ + if (!ensure(SoundManager)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return; + } + SoundManager->RemoveAllSounds(); +} + diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index 343ea70..2f3f95d 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -53,40 +53,37 @@ void UG2IGameSoundManager::Deinitialize() int32 UG2IGameSoundManager::AddSound(const FSoundConfig* NewSoundConfig) { - if (!ensure(NewSoundConfig)) { + if (!ensure(NewSoundConfig) || !ensure(NewSoundConfig->Sound)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager didn't get sound")); return -1; } - if (!ensure(NewSoundConfig->Sound)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager didn't get sound")); - return -1; - } - UAudioComponent* AudioComponent = NewObject(); + UAudioComponent* AudioComponent = NewObject(GetWorld()); if (!ensure(AudioComponent)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager can't create an audio component")); return -1; } - AudioComponent->RegisterComponent(); AudioComponent->SetSound(NewSoundConfig->Sound.Get()); AudioComponent->SetWorldLocation(NewSoundConfig->WorldLocation); - if (NewSoundConfig->AttachToComponent) { + if (NewSoundConfig->ResolvedAttachComponent) { FAttachmentTransformRules Rules( NewSoundConfig->AttachmentRules, NewSoundConfig->AttachmentRules, NewSoundConfig->AttachmentRules, false); AudioComponent->AttachToComponent( - NewSoundConfig->AttachToComponent, + NewSoundConfig->ResolvedAttachComponent, Rules); } - AudioComponent->SetVolumeMultiplier(NewSoundConfig->VolumeMultiplier); AudioComponent->SetPitchMultiplier(NewSoundConfig->PitchMultiplier); + AudioComponent->RegisterComponent(); + if (IdStack.IsEmpty()) { UpdateStackSize(); } + int32 NewSoundId = IdStack.Pop(); ActiveSounds.Add(NewSoundId, AudioComponent); diff --git a/Source/G2I/Public/Components/G2ISoundComponent.h b/Source/G2I/Public/Components/G2ISoundComponent.h new file mode 100644 index 0000000..6e80c8a --- /dev/null +++ b/Source/G2I/Public/Components/G2ISoundComponent.h @@ -0,0 +1,41 @@ +#pragma once + +#include "CoreMinimal.h" +#include "Components/SceneComponent.h" +#include "G2ISoundComponent.generated.h" + +class UG2IGameSoundManager; +struct FSoundConfig; + +UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) +class G2I_API UG2ISoundComponent : public USceneComponent +{ + GENERATED_BODY() + +private: + TWeakObjectPtr SoundManager; +protected: + virtual void BeginPlay() override; + +public: + UPROPERTY(EditDefaultsOnly, Category = "G2I Sounds") + TMap SetupSounds; + + int32 AddSound(const FSoundConfig* NewSoundConfig); + bool RemoveSound(int32 SoundId); + void RemoveAllSounds(); + + bool PlaySound(int32 SoundId); + bool StopSound(int32 SoundId); + void StopAllSounds(); + + bool ChangeSoundVolume(int32 SoundId, float NewVolume); + bool ChangeSoundPitch(int32 SoundId, float NewPitch); + bool ChangeSoundLocation(int32 SoundId, FVector NewLocation); + bool ChangeSoundAttachment(int32 SoundId, + USceneComponent* NewAttachementComponent, + FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform); + bool ChangeSoundAttachment(int32 SoundId, + AActor* NewAttachementActor, + FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform); +}; diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h index 84e260c..91dbf76 100644 --- a/Source/G2I/Public/Game/G2IGameSoundManager.h +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -20,7 +20,9 @@ struct FSoundConfig FVector WorldLocation = FVector::ZeroVector; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment") - TObjectPtr AttachToComponent; + FComponentReference AttachToComponent; + + USceneComponent* ResolvedAttachComponent = nullptr; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment") EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget; From d554593c445ff250312042a6f88e6e1a485ac1c6 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 20 Mar 2026 01:13:54 +0300 Subject: [PATCH 06/22] SmalFix --- .../Private/Components/G2ISoundComponent.cpp | 26 +++++++++---------- .../G2I/Public/Components/G2ISoundComponent.h | 5 ++-- Source/G2I/Public/Game/G2IGameSoundManager.h | 1 + 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Source/G2I/Private/Components/G2ISoundComponent.cpp b/Source/G2I/Private/Components/G2ISoundComponent.cpp index a961989..cc17356 100644 --- a/Source/G2I/Private/Components/G2ISoundComponent.cpp +++ b/Source/G2I/Private/Components/G2ISoundComponent.cpp @@ -1,5 +1,5 @@ #include "Components/G2ISoundComponent.h" -#include "Game/G2IGameSoundManager.h" + #include "G2I.h" void UG2ISoundComponent::BeginPlay() @@ -12,7 +12,7 @@ void UG2ISoundComponent::BeginPlay() return; } SoundManager = World->GetSubsystem(); - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); } } @@ -20,7 +20,7 @@ void UG2ISoundComponent::BeginPlay() int32 UG2ISoundComponent::AddSound(const FSoundConfig* NewSoundConfig) { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return -1; } @@ -38,7 +38,7 @@ int32 UG2ISoundComponent::AddSound(const FSoundConfig* NewSoundConfig) bool UG2ISoundComponent::PlaySound(int32 SoundId) { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } @@ -47,7 +47,7 @@ bool UG2ISoundComponent::PlaySound(int32 SoundId) bool UG2ISoundComponent::StopSound(int32 SoundId) { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } @@ -56,7 +56,7 @@ bool UG2ISoundComponent::StopSound(int32 SoundId) void UG2ISoundComponent::StopAllSounds() { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return; } @@ -65,7 +65,7 @@ void UG2ISoundComponent::StopAllSounds() bool UG2ISoundComponent::ChangeSoundVolume(int32 SoundId, float NewVolume) { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } @@ -74,7 +74,7 @@ bool UG2ISoundComponent::ChangeSoundVolume(int32 SoundId, float NewVolume) bool UG2ISoundComponent::ChangeSoundPitch(int32 SoundId, float NewPitch) { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } @@ -83,7 +83,7 @@ bool UG2ISoundComponent::ChangeSoundPitch(int32 SoundId, float NewPitch) bool UG2ISoundComponent::ChangeSoundLocation(int32 SoundId, FVector NewLocation) { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } @@ -94,7 +94,7 @@ bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, USceneComponent* NewAttachmentComponent, FAttachmentTransformRules AttachmentRules) { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } @@ -105,7 +105,7 @@ bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, AActor* NewAttachmentActor, FAttachmentTransformRules AttachmentRules) { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } @@ -115,7 +115,7 @@ bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, bool UG2ISoundComponent::RemoveSound(int32 SoundId) { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } @@ -124,7 +124,7 @@ bool UG2ISoundComponent::RemoveSound(int32 SoundId) void UG2ISoundComponent::RemoveAllSounds() { - if (!ensure(SoundManager)) { + if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return; } diff --git a/Source/G2I/Public/Components/G2ISoundComponent.h b/Source/G2I/Public/Components/G2ISoundComponent.h index 6e80c8a..acdfce2 100644 --- a/Source/G2I/Public/Components/G2ISoundComponent.h +++ b/Source/G2I/Public/Components/G2ISoundComponent.h @@ -2,11 +2,10 @@ #include "CoreMinimal.h" #include "Components/SceneComponent.h" +//TODO Move FSoundConfig to new .h file to resolve confilct +#include "Game/G2IGameSoundManager.h" #include "G2ISoundComponent.generated.h" -class UG2IGameSoundManager; -struct FSoundConfig; - UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) class G2I_API UG2ISoundComponent : public USceneComponent { diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h index 91dbf76..d559940 100644 --- a/Source/G2I/Public/Game/G2IGameSoundManager.h +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -22,6 +22,7 @@ struct FSoundConfig UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment") FComponentReference AttachToComponent; + UPROPERTY() USceneComponent* ResolvedAttachComponent = nullptr; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment") From 0e6c35c679fb80340a6ddb6218506c286fd9065a Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 20 Mar 2026 14:21:09 +0300 Subject: [PATCH 07/22] Fix --- .../Private/Components/G2ISoundComponent.cpp | 17 ++++---- .../G2I/Private/Game/G2IGameSoundManager.cpp | 42 ++++++++++++------- .../G2I/Public/Components/G2ISoundComponent.h | 28 ++++++++++--- Source/G2I/Public/Game/G2IGameSoundManager.h | 17 ++++---- 4 files changed, 64 insertions(+), 40 deletions(-) diff --git a/Source/G2I/Private/Components/G2ISoundComponent.cpp b/Source/G2I/Private/Components/G2ISoundComponent.cpp index cc17356..6dfbd25 100644 --- a/Source/G2I/Private/Components/G2ISoundComponent.cpp +++ b/Source/G2I/Private/Components/G2ISoundComponent.cpp @@ -18,22 +18,17 @@ void UG2ISoundComponent::BeginPlay() } -int32 UG2ISoundComponent::AddSound(const FSoundConfig* NewSoundConfig) +int32 UG2ISoundComponent::AddSound(const FSoundConfig& NewSoundConfig) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return -1; } - if (!NewSoundConfig) { - UE_LOG(LogG2I, Warning, TEXT("Got empty config in %s"), *GetName()); - return -1; - } - - FSoundConfig ConfigToSend = *NewSoundConfig; + FSoundConfig ConfigToSend = NewSoundConfig; ConfigToSend.ResolvedAttachComponent = Cast(ConfigToSend.AttachToComponent.GetComponent(GetOwner())); - return SoundManager->AddSound(&ConfigToSend); + return SoundManager->AddSound(ConfigToSend); } bool UG2ISoundComponent::PlaySound(int32 SoundId) @@ -92,23 +87,25 @@ bool UG2ISoundComponent::ChangeSoundLocation(int32 SoundId, FVector NewLocation) bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, USceneComponent* NewAttachmentComponent, - FAttachmentTransformRules AttachmentRules) + EAttachmentRule AttachmentRules) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } + return SoundManager->ChangeSoundAttachment(SoundId, NewAttachmentComponent, AttachmentRules); } bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, AActor* NewAttachmentActor, - FAttachmentTransformRules AttachmentRules) + EAttachmentRule AttachmentRules) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } + return SoundManager->ChangeSoundAttachment(SoundId, NewAttachmentActor, AttachmentRules); } diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index 2f3f95d..ca8a7a4 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -51,9 +51,9 @@ void UG2IGameSoundManager::Deinitialize() Super::Deinitialize(); } -int32 UG2IGameSoundManager::AddSound(const FSoundConfig* NewSoundConfig) +int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) { - if (!ensure(NewSoundConfig) || !ensure(NewSoundConfig->Sound)) { + if (!ensure(NewSoundConfig.Sound)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager didn't get sound")); return -1; } @@ -63,21 +63,21 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig* NewSoundConfig) return -1; } - AudioComponent->SetSound(NewSoundConfig->Sound.Get()); - AudioComponent->SetWorldLocation(NewSoundConfig->WorldLocation); + AudioComponent->SetSound(NewSoundConfig.Sound.Get()); + AudioComponent->SetWorldLocation(NewSoundConfig.WorldLocation); - if (NewSoundConfig->ResolvedAttachComponent) { + if (NewSoundConfig.ResolvedAttachComponent) { FAttachmentTransformRules Rules( - NewSoundConfig->AttachmentRules, - NewSoundConfig->AttachmentRules, - NewSoundConfig->AttachmentRules, + NewSoundConfig.AttachmentRules, + NewSoundConfig.AttachmentRules, + NewSoundConfig.AttachmentRules, false); AudioComponent->AttachToComponent( - NewSoundConfig->ResolvedAttachComponent, + NewSoundConfig.ResolvedAttachComponent, Rules); } - AudioComponent->SetVolumeMultiplier(NewSoundConfig->VolumeMultiplier); - AudioComponent->SetPitchMultiplier(NewSoundConfig->PitchMultiplier); + AudioComponent->SetVolumeMultiplier(NewSoundConfig.VolumeMultiplier); + AudioComponent->SetPitchMultiplier(NewSoundConfig.PitchMultiplier); AudioComponent->RegisterComponent(); if (IdStack.IsEmpty()) { @@ -170,7 +170,7 @@ bool UG2IGameSoundManager::ChangeSoundLocation(int32 SoundId, FVector NewLocatio bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, USceneComponent* NewAttachmentComponent, - FAttachmentTransformRules AttachmentRules) + EAttachmentRule AttachmentRules) { if (!ensure(NewAttachmentComponent)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager got wrong component to attach")); @@ -182,14 +182,20 @@ bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, return false; } - Component->AttachToComponent(NewAttachmentComponent, AttachmentRules); + FAttachmentTransformRules Rules( + AttachmentRules, + AttachmentRules, + AttachmentRules, + false); + + Component->AttachToComponent(NewAttachmentComponent, Rules); return true; } bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, AActor* NewAttachmentActor, - FAttachmentTransformRules AttachmentRules) + EAttachmentRule AttachmentRules) { if (!ensure(NewAttachmentActor)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager got wrong actor to attach")); @@ -204,8 +210,14 @@ bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, if (!ensure(Component)) { return false; } + + FAttachmentTransformRules Rules( + AttachmentRules, + AttachmentRules, + AttachmentRules, + false); - Component->AttachToComponent(RootComponent, AttachmentRules); + Component->AttachToComponent(RootComponent, Rules); return true; } diff --git a/Source/G2I/Public/Components/G2ISoundComponent.h b/Source/G2I/Public/Components/G2ISoundComponent.h index acdfce2..7da8248 100644 --- a/Source/G2I/Public/Components/G2ISoundComponent.h +++ b/Source/G2I/Public/Components/G2ISoundComponent.h @@ -1,8 +1,7 @@ #pragma once #include "CoreMinimal.h" -#include "Components/SceneComponent.h" -//TODO Move FSoundConfig to new .h file to resolve confilct +//TODO Move FSoundConfig to new .h file to resolve conflict #include "Game/G2IGameSoundManager.h" #include "G2ISoundComponent.generated.h" @@ -17,24 +16,41 @@ class G2I_API UG2ISoundComponent : public USceneComponent virtual void BeginPlay() override; public: - UPROPERTY(EditDefaultsOnly, Category = "G2I Sounds") + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sounds") TMap SetupSounds; - int32 AddSound(const FSoundConfig* NewSoundConfig); + + UFUNCTION(BlueprintCallable, Category = "Sounds") + int32 AddSound(const FSoundConfig& NewSoundConfig); + + UFUNCTION(BlueprintCallable, Category = "Sounds") bool RemoveSound(int32 SoundId); + + UFUNCTION(BlueprintCallable, Category = "Sounds") void RemoveAllSounds(); + UFUNCTION(BlueprintCallable, Category = "Sounds") bool PlaySound(int32 SoundId); + + UFUNCTION(BlueprintCallable, Category = "Sounds") bool StopSound(int32 SoundId); + + UFUNCTION(BlueprintCallable, Category = "Sounds") void StopAllSounds(); + UFUNCTION(BlueprintCallable, Category = "Sounds") bool ChangeSoundVolume(int32 SoundId, float NewVolume); + + UFUNCTION(BlueprintCallable, Category = "Sounds") bool ChangeSoundPitch(int32 SoundId, float NewPitch); + + UFUNCTION(BlueprintCallable, Category = "Sounds") bool ChangeSoundLocation(int32 SoundId, FVector NewLocation); + bool ChangeSoundAttachment(int32 SoundId, USceneComponent* NewAttachementComponent, - FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform); + EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); bool ChangeSoundAttachment(int32 SoundId, AActor* NewAttachementActor, - FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform); + EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); }; diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h index d559940..36d69e1 100644 --- a/Source/G2I/Public/Game/G2IGameSoundManager.h +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -2,15 +2,14 @@ #include "CoreMinimal.h" #include "Subsystems/WorldSubsystem.h" +#include "Engine/EngineTypes.h" +#include "Components/AudioComponent.h" +#include "Components/SceneComponent.h" +#include "Sound/SoundCue.h" #include "G2IGameSoundManager.generated.h" - -class UAudioComponent; -class USceneComponent; -class USoundCue; - USTRUCT(BlueprintType) -struct FSoundConfig +struct FSoundConfig { GENERATED_BODY() UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Sound") @@ -66,7 +65,7 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem virtual void Initialize(FSubsystemCollectionBase& Collection) override; virtual void Deinitialize() override; - int32 AddSound(const FSoundConfig* NewSoundConfig); + int32 AddSound(const FSoundConfig& NewSoundConfig); bool RemoveSound(int32 SoundId); void RemoveAllSounds(); @@ -79,8 +78,8 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem bool ChangeSoundLocation(int32 SoundId, FVector NewLocation); bool ChangeSoundAttachment(int32 SoundId, USceneComponent* NewAttachementComponent, - FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform); + EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); bool ChangeSoundAttachment(int32 SoundId, AActor* NewAttachementActor, - FAttachmentTransformRules AttachmentRules = FAttachmentTransformRules::KeepRelativeTransform); + EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); }; From 39c5f194c2e3c5dd164de317825b636e83571037 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 20 Mar 2026 14:46:32 +0300 Subject: [PATCH 08/22] Update --- Source/G2I/Private/Game/G2IGameSoundManager.cpp | 3 ++- Source/G2I/Public/Components/G2ISoundComponent.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index ca8a7a4..4d18b5f 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -57,7 +57,7 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) UE_LOG(LogG2I, Warning, TEXT("The sound manager didn't get sound")); return -1; } - UAudioComponent* AudioComponent = NewObject(GetWorld()); + UAudioComponent* AudioComponent = NewObject(GetWorld()->GetWorldSettings()); if (!ensure(AudioComponent)) { UE_LOG(LogG2I, Warning, TEXT("The sound manager can't create an audio component")); return -1; @@ -78,6 +78,7 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) } AudioComponent->SetVolumeMultiplier(NewSoundConfig.VolumeMultiplier); AudioComponent->SetPitchMultiplier(NewSoundConfig.PitchMultiplier); + AudioComponent->RegisterComponent(); if (IdStack.IsEmpty()) { diff --git a/Source/G2I/Public/Components/G2ISoundComponent.h b/Source/G2I/Public/Components/G2ISoundComponent.h index 7da8248..4066ed4 100644 --- a/Source/G2I/Public/Components/G2ISoundComponent.h +++ b/Source/G2I/Public/Components/G2ISoundComponent.h @@ -16,7 +16,7 @@ class G2I_API UG2ISoundComponent : public USceneComponent virtual void BeginPlay() override; public: - UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sounds") + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Sounds") TMap SetupSounds; From af9bb9db273b950f743c4a85d61d2224a20166ca Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Thu, 2 Apr 2026 18:00:37 +0300 Subject: [PATCH 09/22] Added global sounds multipliers, looping; getters/setters; code refact + rename; added 2d sound ; added fade to play and stop --- .../Private/Components/G2ISoundComponent.cpp | 102 ++++++- .../G2I/Private/Game/G2IGameSoundManager.cpp | 276 ++++++++++++++++-- .../G2I/Public/Components/G2ISoundComponent.h | 34 ++- Source/G2I/Public/Game/G2IGameSoundManager.h | 66 ++++- 4 files changed, 432 insertions(+), 46 deletions(-) diff --git a/Source/G2I/Private/Components/G2ISoundComponent.cpp b/Source/G2I/Private/Components/G2ISoundComponent.cpp index 6dfbd25..222818a 100644 --- a/Source/G2I/Private/Components/G2ISoundComponent.cpp +++ b/Source/G2I/Private/Components/G2ISoundComponent.cpp @@ -26,7 +26,20 @@ int32 UG2ISoundComponent::AddSound(const FSoundConfig& NewSoundConfig) } FSoundConfig ConfigToSend = NewSoundConfig; - ConfigToSend.ResolvedAttachComponent = Cast(ConfigToSend.AttachToComponent.GetComponent(GetOwner())); + AActor* Owner = GetOwner(); + if (!ensure(Owner)) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the owner of component in %s"), *GetName()); + return -1; + } + + + + ConfigToSend.ResolvedAttachComponent = Cast(ConfigToSend.AttachToComponent.GetComponent(Owner)); + + if (!ConfigToSend.ResolvedAttachComponent && !ConfigToSend.bIs2D) + { + ConfigToSend.ResolvedAttachComponent = this; + } return SoundManager->AddSound(ConfigToSend); } @@ -40,13 +53,13 @@ bool UG2ISoundComponent::PlaySound(int32 SoundId) return SoundManager->PlaySound(SoundId); } -bool UG2ISoundComponent::StopSound(int32 SoundId) +bool UG2ISoundComponent::StopSound(int32 SoundId, float FadeOutTime) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } - return SoundManager->StopSound(SoundId); + return SoundManager->StopSound(SoundId, FadeOutTime); } void UG2ISoundComponent::StopAllSounds() @@ -58,34 +71,34 @@ void UG2ISoundComponent::StopAllSounds() SoundManager->StopAllSounds(); } -bool UG2ISoundComponent::ChangeSoundVolume(int32 SoundId, float NewVolume) +bool UG2ISoundComponent::SetSoundVolume(int32 SoundId, float NewVolume) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } - return SoundManager->ChangeSoundVolume(SoundId, NewVolume); + return SoundManager->SetSoundVolume(SoundId, NewVolume); } -bool UG2ISoundComponent::ChangeSoundPitch(int32 SoundId, float NewPitch) +bool UG2ISoundComponent::SetSoundPitch(int32 SoundId, float NewPitch) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } - return SoundManager->ChangeSoundPitch(SoundId, NewPitch); + return SoundManager->SetSoundPitch(SoundId, NewPitch); } -bool UG2ISoundComponent::ChangeSoundLocation(int32 SoundId, FVector NewLocation) +bool UG2ISoundComponent::SetSoundLocation(int32 SoundId, FVector NewLocation) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); return false; } - return SoundManager->ChangeSoundLocation(SoundId, NewLocation); + return SoundManager->SetSoundLocation(SoundId, NewLocation); } -bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, +bool UG2ISoundComponent::SetSoundAttachment(int32 SoundId, USceneComponent* NewAttachmentComponent, EAttachmentRule AttachmentRules) { @@ -94,10 +107,10 @@ bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, return false; } - return SoundManager->ChangeSoundAttachment(SoundId, NewAttachmentComponent, AttachmentRules); + return SoundManager->SetSoundAttachment(SoundId, NewAttachmentComponent, AttachmentRules); } -bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, +bool UG2ISoundComponent::SetSoundAttachment(int32 SoundId, AActor* NewAttachmentActor, EAttachmentRule AttachmentRules) { @@ -106,10 +119,9 @@ bool UG2ISoundComponent::ChangeSoundAttachment(int32 SoundId, return false; } - return SoundManager->ChangeSoundAttachment(SoundId, NewAttachmentActor, AttachmentRules); + return SoundManager->SetSoundAttachment(SoundId, NewAttachmentActor, AttachmentRules); } - bool UG2ISoundComponent::RemoveSound(int32 SoundId) { if (!SoundManager.IsValid()) { @@ -128,3 +140,65 @@ void UG2ISoundComponent::RemoveAllSounds() SoundManager->RemoveAllSounds(); } +bool UG2ISoundComponent::SetSoundLooping(int32 SoundId, bool bNewIsLooping) +{ + if (!SoundManager.IsValid()) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->SetSoundLooping(SoundId, bNewIsLooping); +} + +bool UG2ISoundComponent::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy) +{ + if (!SoundManager.IsValid()) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->SetSoundAutoDestroy(SoundId, bNewAutoDestroy); +} + +float UG2ISoundComponent::GetSoundVolume(int32 SoundId) +{ + if (!SoundManager.IsValid()) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return 0.0f; + } + return SoundManager->GetSoundVolume(SoundId); +} + +float UG2ISoundComponent::GetSoundPitch(int32 SoundId) +{ + if (!SoundManager.IsValid()) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return 1.0f; + } + return SoundManager->GetSoundPitch(SoundId); +} + +FVector UG2ISoundComponent::GetSoundLocation(int32 SoundId) +{ + if (!SoundManager.IsValid()) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return FVector::ZeroVector; + } + return SoundManager->GetSoundLocation(SoundId); +} + +bool UG2ISoundComponent::GetSoundLooping(int32 SoundId) +{ + if (!SoundManager.IsValid()) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->GetSoundLooping(SoundId); +} + +bool UG2ISoundComponent::GetSoundAutoDestroy(int32 SoundId) +{ + if (!SoundManager.IsValid()) { + UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + return false; + } + return SoundManager->GetSoundAutoDestroy(SoundId); +} \ No newline at end of file diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index 4d18b5f..59dc22b 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -3,8 +3,11 @@ #include "G2I.h" #include "Components/AudioComponent.h" #include "Components/SceneComponent.h" +#include "Kismet/GameplayStatics.h" #include "Sound/SoundCue.h" +static const FName LoopingTag("LoopingSound"); + UAudioComponent* UG2IGameSoundManager::GetAudioById(int32 SoundId) { if (SoundId < 0) { @@ -66,19 +69,31 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) AudioComponent->SetSound(NewSoundConfig.Sound.Get()); AudioComponent->SetWorldLocation(NewSoundConfig.WorldLocation); - if (NewSoundConfig.ResolvedAttachComponent) { - FAttachmentTransformRules Rules( - NewSoundConfig.AttachmentRules, - NewSoundConfig.AttachmentRules, - NewSoundConfig.AttachmentRules, - false); - AudioComponent->AttachToComponent( - NewSoundConfig.ResolvedAttachComponent, - Rules); + if (!NewSoundConfig.bIs2D) + { + AudioComponent->SetWorldLocation(NewSoundConfig.WorldLocation); + + if (NewSoundConfig.ResolvedAttachComponent) { + FAttachmentTransformRules Rules( + NewSoundConfig.AttachmentRules, + NewSoundConfig.AttachmentRules, + NewSoundConfig.AttachmentRules, + false); + AudioComponent->AttachToComponent(NewSoundConfig.ResolvedAttachComponent, Rules); + } + } + else + { + AudioComponent->bAllowSpatialization = false; } AudioComponent->SetVolumeMultiplier(NewSoundConfig.VolumeMultiplier); AudioComponent->SetPitchMultiplier(NewSoundConfig.PitchMultiplier); + if (NewSoundConfig.bIsLooping) { + AudioComponent->ComponentTags.Add(LoopingTag); + } + AudioComponent->bAutoDestroy = NewSoundConfig.bIsLooping ? false : NewSoundConfig.bAutoDestroy; + AudioComponent->RegisterComponent(); if (IdStack.IsEmpty()) { @@ -87,11 +102,12 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) int32 NewSoundId = IdStack.Pop(); ActiveSounds.Add(NewSoundId, AudioComponent); + AudioComponent->OnAudioFinishedNative.AddUObject(this, &UG2IGameSoundManager::OnSoundFinished, NewSoundId); return NewSoundId; } -bool UG2IGameSoundManager::PlaySound(int32 SoundId) +bool UG2IGameSoundManager::PlaySound(int32 SoundId, float FadeInTime) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { @@ -99,12 +115,12 @@ bool UG2IGameSoundManager::PlaySound(int32 SoundId) return false; } - Component->Play(); + Component->FadeIn(FadeInTime, Component->VolumeMultiplier); return true; } -bool UG2IGameSoundManager::StopSound(int32 SoundId) +bool UG2IGameSoundManager::StopSound(int32 SoundId, float FadeOutTime) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { @@ -112,7 +128,7 @@ bool UG2IGameSoundManager::StopSound(int32 SoundId) return false; } - Component->Stop(); + Component->FadeOut(FadeOutTime, 0.0f); return true; } @@ -128,7 +144,7 @@ void UG2IGameSoundManager::StopAllSounds() } } -bool UG2IGameSoundManager::ChangeSoundVolume(int32 SoundId, float NewVolume) +bool UG2IGameSoundManager::SetSoundVolume(int32 SoundId, float NewVolume) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { @@ -142,7 +158,7 @@ bool UG2IGameSoundManager::ChangeSoundVolume(int32 SoundId, float NewVolume) return true; } -bool UG2IGameSoundManager::ChangeSoundPitch(int32 SoundId, float NewPitch) +bool UG2IGameSoundManager::SetSoundPitch(int32 SoundId, float NewPitch) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { @@ -156,7 +172,7 @@ bool UG2IGameSoundManager::ChangeSoundPitch(int32 SoundId, float NewPitch) return true; } -bool UG2IGameSoundManager::ChangeSoundLocation(int32 SoundId, FVector NewLocation) +bool UG2IGameSoundManager::SetSoundLocation(int32 SoundId, FVector NewLocation) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { @@ -169,7 +185,7 @@ bool UG2IGameSoundManager::ChangeSoundLocation(int32 SoundId, FVector NewLocatio return true; } -bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, +bool UG2IGameSoundManager::SetSoundAttachment(int32 SoundId, USceneComponent* NewAttachmentComponent, EAttachmentRule AttachmentRules) { @@ -194,7 +210,7 @@ bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, return true; } -bool UG2IGameSoundManager::ChangeSoundAttachment(int32 SoundId, +bool UG2IGameSoundManager::SetSoundAttachment(int32 SoundId, AActor* NewAttachmentActor, EAttachmentRule AttachmentRules) { @@ -260,4 +276,228 @@ void UG2IGameSoundManager::UpdateStackSize() { IdStack.Push(i); } CurrentNumberAvailable *= 2; +} + +void UG2IGameSoundManager::InitGlobalAudio(USoundMix* _MainMix, TMap _SoundClasses) +{ + MainSoundMix = _MainMix; + SoundClasses = _SoundClasses; + UWorld* World = GetWorld(); + if (MainSoundMix && World) + { + UGameplayStatics::PushSoundMixModifier(World, MainSoundMix); + } + else { + UE_LOG(LogG2I, Error, TEXT("%s: Can't get the World"), *GetName()); + } +} + +void UG2IGameSoundManager::SetGlobalVolume(EG2IASoundType SoundType, float NewVolume) +{ + if (!MainSoundMix) + { + UE_LOG(LogG2I, Error, TEXT("%s: Can't get the MainSoundMix"), *GetName()); + return; + } + + if (!SoundClasses.Contains(SoundType)) + { + UE_LOG(LogG2I, Warning, TEXT("%s: SoundClasses map doesn't contain SoundType (ID: %d)"), *GetName(), static_cast(SoundType)); + return; + } + + USoundClass* TargetClass = SoundClasses[SoundType]; + if (!TargetClass) + { + UE_LOG(LogG2I, Error, TEXT("%s: Can't get Sound class for the SoundType (ID: %d)"), *GetName(), static_cast(SoundType)); + return; + } + + float ClampedVolume = FMath::Clamp(NewVolume, 0.0f, 1.0f); + + switch (SoundType) + { + case EG2IASoundType::DefaultSound: + { + GeneralSoundMultiplier = ClampedVolume; + break; + } + case EG2IASoundType::EffectSound: + { + EffectsSoundMultiplier = ClampedVolume; + break; + } + case EG2IASoundType::MusicSound: + { + MusicSoundMultiplier = ClampedVolume; + break; + } + case EG2IASoundType::SpeechSound: + { + SpeechSoundMultiplier = ClampedVolume; + break; + } + } + + if (UWorld* World = GetWorld()) { + UGameplayStatics::SetSoundMixClassOverride( + World, + MainSoundMix, + TargetClass, + ClampedVolume, + 1.0f, + 0.0f, + true + ); + } + else { + UE_LOG(LogG2I, Error, TEXT("%s: Can't get the World"), *GetName()); + } +} + +float UG2IGameSoundManager::GetGlobalVolume(EG2IASoundType SoundType) const { + switch (SoundType) { + case EG2IASoundType::DefaultSound: + { + return GeneralSoundMultiplier; + } + case EG2IASoundType::EffectSound: + { + return EffectsSoundMultiplier; + } + case EG2IASoundType::MusicSound: + { + return MusicSoundMultiplier; + } + case EG2IASoundType::SpeechSound: + { + return SpeechSoundMultiplier; + } + default: { + return 0.0f; + } + } +} + +void UG2IGameSoundManager::OnSoundFinished(UAudioComponent* AudioComponent, int32 SoundId) +{ + if (AudioComponent && AudioComponent->ComponentHasTag("LoopingSound")) { + AudioComponent->Play(); + } + else { + RemoveSound(SoundId); + } +} + +bool UG2IGameSoundManager::SetSoundLooping(int32 SoundId, bool bNewIsLooping) +{ + UAudioComponent* Component = GetAudioById(SoundId); + if (!ensure(Component)) { + UE_LOG(LogG2I, Warning, TEXT("%s: can't get an audio component (ID: %d)"), *GetName(), SoundId); + return false; + } + + if (bNewIsLooping) + { + Component->ComponentTags.AddUnique(LoopingTag); + Component->bAutoDestroy = false; + } + else + { + Component->ComponentTags.Remove(LoopingTag); + } + + return true; +} + +float UG2IGameSoundManager::GetSoundVolume(int32 SoundId) +{ + if (UAudioComponent* Component = GetAudioById(SoundId)) + { + return Component->VolumeMultiplier; + } + + UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + return 0.0f; +} + +float UG2IGameSoundManager::GetSoundPitch(int32 SoundId) +{ + if (UAudioComponent* Component = GetAudioById(SoundId)) + { + return Component->PitchMultiplier; + } + + UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + return 1.0f; +} + +FVector UG2IGameSoundManager::GetSoundLocation(int32 SoundId) +{ + if (UAudioComponent* Component = GetAudioById(SoundId)) + { + return Component->GetComponentLocation(); + } + + UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + return FVector::ZeroVector; +} + +USceneComponent* UG2IGameSoundManager::GetSoundAttachment(int32 SoundId) +{ + if (UAudioComponent* Component = GetAudioById(SoundId)) + { + return Component->GetAttachParent(); + } + + UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + return nullptr; +} + +bool UG2IGameSoundManager::GetSoundLooping(int32 SoundId) +{ + if (UAudioComponent* Component = GetAudioById(SoundId)) + { + return Component->ComponentHasTag(LoopingTag); + } + + UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + return false; +} + +bool UG2IGameSoundManager::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy) +{ + UAudioComponent* Component = GetAudioById(SoundId); + if (!Component) + { + UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + return false; + } + + if (Component->ComponentHasTag(LoopingTag)) + { + if (bNewAutoDestroy) + { + UE_LOG(LogG2I, Warning, TEXT("%s: SoundId %d is looping"), *GetName(), SoundId); + } + + Component->bAutoDestroy = false; + } + else + { + Component->bAutoDestroy = bNewAutoDestroy; + } + + return true; +} + +bool UG2IGameSoundManager::GetSoundAutoDestroy(int32 SoundId) +{ + if (UAudioComponent* Component = GetAudioById(SoundId)) + { + return Component->bAutoDestroy; + } + + UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + return false; } \ No newline at end of file diff --git a/Source/G2I/Public/Components/G2ISoundComponent.h b/Source/G2I/Public/Components/G2ISoundComponent.h index 4066ed4..c76ddb1 100644 --- a/Source/G2I/Public/Components/G2ISoundComponent.h +++ b/Source/G2I/Public/Components/G2ISoundComponent.h @@ -1,7 +1,6 @@ #pragma once #include "CoreMinimal.h" -//TODO Move FSoundConfig to new .h file to resolve conflict #include "Game/G2IGameSoundManager.h" #include "G2ISoundComponent.generated.h" @@ -33,24 +32,45 @@ class G2I_API UG2ISoundComponent : public USceneComponent bool PlaySound(int32 SoundId); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool StopSound(int32 SoundId); + bool StopSound(int32 SoundId, float FadeOutTime = 0.0f); UFUNCTION(BlueprintCallable, Category = "Sounds") void StopAllSounds(); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool ChangeSoundVolume(int32 SoundId, float NewVolume); + bool SetSoundVolume(int32 SoundId, float NewVolume); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool ChangeSoundPitch(int32 SoundId, float NewPitch); + bool SetSoundPitch(int32 SoundId, float NewPitch); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool ChangeSoundLocation(int32 SoundId, FVector NewLocation); + bool SetSoundLocation(int32 SoundId, FVector NewLocation); - bool ChangeSoundAttachment(int32 SoundId, + bool SetSoundAttachment(int32 SoundId, USceneComponent* NewAttachementComponent, EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); - bool ChangeSoundAttachment(int32 SoundId, + bool SetSoundAttachment(int32 SoundId, AActor* NewAttachementActor, EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); + + UFUNCTION(BlueprintCallable, Category = "Sounds") + bool SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy); + + UFUNCTION(BlueprintCallable, Category = "Sounds") + bool SetSoundLooping(int32 SoundId, bool bNewIsLooping); + + UFUNCTION(BlueprintPure, Category = "Sounds") + float GetSoundVolume(int32 SoundId); + + UFUNCTION(BlueprintPure, Category = "Sounds") + bool GetSoundLooping(int32 SoundId); + + UFUNCTION(BlueprintPure, Category = "Sounds") + float GetSoundPitch(int32 SoundId); + + UFUNCTION(BlueprintPure, Category = "Sounds") + FVector GetSoundLocation(int32 SoundId); + + UFUNCTION(BlueprintPure, Category = "Sounds") + bool GetSoundAutoDestroy(int32 SoundId); }; diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h index 36d69e1..37f814f 100644 --- a/Source/G2I/Public/Game/G2IGameSoundManager.h +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -6,6 +6,8 @@ #include "Components/AudioComponent.h" #include "Components/SceneComponent.h" #include "Sound/SoundCue.h" +#include "Sound/SoundClass.h" +#include "Sound/SoundMix.h" #include "G2IGameSoundManager.generated.h" USTRUCT(BlueprintType) @@ -33,8 +35,29 @@ struct FSoundConfig UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play", meta = (ClampMin = "0.1", ClampMax = "2.0")) float PitchMultiplier = 1.0f; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play") + bool bIsLooping = false; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Lifetime") bool bAutoDestroy = true; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Location") + bool bIs2D = false; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play", meta = (ClampMin = "0.0")) + float FadeInTime = 0.0f; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play", meta = (ClampMin = "0.0")) + float FadeOutTime = 0.0f; +}; + +UENUM(BlueprintType) +enum class EG2IASoundType: uint8 +{ + SpeechSound UMETA(DisplayName = "Sound for dialogs"), + MusicSound UMETA(DisplayName = "Sound for music"), + EffectSound UMETA(DisplayName = "Sound for effects"), + DefaultSound UMETA(DisplayName = "Default sound") }; UCLASS() @@ -47,6 +70,9 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem UPROPERTY() TArray IdStack; + UPROPERTY() + TArray LoopingSoundsId; + int32 CurrentNumberAvailable; UPROPERTY() @@ -54,9 +80,22 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem void UpdateStackSize(); + float GeneralSoundMultiplier = 1.0f; + float EffectsSoundMultiplier = 1.0f; + float MusicSoundMultiplier = 1.0f; + float SpeechSoundMultiplier = 1.0f; + + UPROPERTY() + USoundMix* MainSoundMix; + + UPROPERTY() + TMap SoundClasses; + protected: UAudioComponent* GetAudioById(int32 SoundId); + + void OnSoundFinished(UAudioComponent* AudioComp, int32 SoundId); public: UFUNCTION(BlueprintPure, Category = "Sound Manager", meta = (WorldContext = "WorldContextObject")) @@ -69,17 +108,30 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem bool RemoveSound(int32 SoundId); void RemoveAllSounds(); - bool PlaySound(int32 SoundId); - bool StopSound(int32 SoundId); + bool PlaySound(int32 SoundId, float FadeInTime = 0.0f); + bool StopSound(int32 SoundId, float FadeOutTime = 0.0f); void StopAllSounds(); - bool ChangeSoundVolume(int32 SoundId, float NewVolume); - bool ChangeSoundPitch(int32 SoundId, float NewPitch); - bool ChangeSoundLocation(int32 SoundId, FVector NewLocation); - bool ChangeSoundAttachment(int32 SoundId, + bool SetSoundVolume(int32 SoundId, float NewVolume); + bool SetSoundPitch(int32 SoundId, float NewPitch); + bool SetSoundLocation(int32 SoundId, FVector NewLocation); + bool SetSoundAttachment(int32 SoundId, USceneComponent* NewAttachementComponent, EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); - bool ChangeSoundAttachment(int32 SoundId, + bool SetSoundAttachment(int32 SoundId, AActor* NewAttachementActor, EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); + bool SetSoundLooping(int32 SoundId, bool bNewIsLooping); + bool SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy); + + float GetSoundVolume(int32 SoundId); + float GetSoundPitch(int32 SoundId); + FVector GetSoundLocation(int32 SoundId); + USceneComponent* GetSoundAttachment(int32 SoundId); + bool GetSoundLooping(int32 SoundId); + bool GetSoundAutoDestroy(int32 SoundId); + + void InitGlobalAudio(USoundMix* _MainMix, TMap _SoundClasses); + void SetGlobalVolume(EG2IASoundType SoundType, float NewVolume); + float GetGlobalVolume(EG2IASoundType SoundType) const; }; From 40c155b7e637703e28c3db1a01d72782d92dc83e Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Thu, 2 Apr 2026 18:45:19 +0300 Subject: [PATCH 10/22] Log refact; added sound classes; added one time playing feature; added zone which triggers sound TODO: Complete zone Refact log in sound component --- .../G2I_Game/Audio/System/SCM_MainMix.uasset | Bin 0 -> 2776 bytes .../G2I_Game/Audio/System/SC_Effects.uasset | Bin 0 -> 1267 bytes .../G2I_Game/Audio/System/SC_General.uasset | Bin 0 -> 1600 bytes Content/G2I_Game/Audio/System/SC_Music.uasset | Bin 0 -> 1257 bytes .../G2I_Game/Audio/System/SC_Speech.uasset | Bin 0 -> 1262 bytes .../G2I/Private/Game/G2IGameSoundManager.cpp | 111 ++++++++++++------ .../Gameplay/G2ISoundActivationZone.cpp | 39 ++++++ Source/G2I/Public/Game/G2IGameSoundManager.h | 10 ++ .../Public/Gameplay/G2ISoundActivationZone.h | 35 ++++++ 9 files changed, 161 insertions(+), 34 deletions(-) create mode 100644 Content/G2I_Game/Audio/System/SCM_MainMix.uasset create mode 100644 Content/G2I_Game/Audio/System/SC_Effects.uasset create mode 100644 Content/G2I_Game/Audio/System/SC_General.uasset create mode 100644 Content/G2I_Game/Audio/System/SC_Music.uasset create mode 100644 Content/G2I_Game/Audio/System/SC_Speech.uasset create mode 100644 Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp create mode 100644 Source/G2I/Public/Gameplay/G2ISoundActivationZone.h diff --git a/Content/G2I_Game/Audio/System/SCM_MainMix.uasset b/Content/G2I_Game/Audio/System/SCM_MainMix.uasset new file mode 100644 index 0000000000000000000000000000000000000000..7ef8291a9eb07ecf9c7e2ccf8a2bd948fcbdea2e GIT binary patch literal 2776 zcmdT`U2GIp6u#0QL@bsnC@TI1R4nc8LU&stn;rPWJD)Fmgm*Sv7Fz+&Ayd zW$ZrE{gE2_8 z)tlAb#ANJgYCSMUI9|-yv((o~Yov8Gkx<>5SR@jwjY#2exF#46*473iA!+sMJQ?fu zl#;{LU#G>ub()JF2E}}g=u{$nep5@W;8o z&Va`F<^_w$BCH}Tb~|>HF6C3+80QrbJlqulTwI_K56KD?;vwCELOk3Upb*bxP2~w0 zE-dgjo%bkvJ#zM*pNYK>{rHULzFEnIMWd z^wVwm*Hn16{4z0yjVakRJtJ>YD#8u|lQ_<8IN8ZE~U>mofVa+*#FgIksU~%Vfv5y@z{ax c>Q_#UAlF2NfDU-$a^0rl(2M1@-CjbBd literal 0 HcmV?d00001 diff --git a/Content/G2I_Game/Audio/System/SC_Effects.uasset b/Content/G2I_Game/Audio/System/SC_Effects.uasset new file mode 100644 index 0000000000000000000000000000000000000000..8bba5c80a1dfcca9223847c57db5d9691932092a GIT binary patch literal 1267 zcmX@utTpfZ|Ns9Jm>C$jm>3v-0%;KVa8blFaG`~v??Dwg=B}O?U;cU5rm`?FFaqV? zW^8!!(rL;U?pub79Cy3vX#mC4fmq)?F*jA;-N-W@%y2AC$;{Uet}HG|&D9Tfj(1H< zOHD2*1}bD=;059qkXb-*5@@X=kTx(gH+Qu(aWyt`H8OKFF)}eTG&6T`Hg+_2Gca&< zHijz(>6-_1C(t$BeK@1O| zefxnJ832R%KPU`A!iSOMSb==M{Jd0#FU&`I`GFk&q%2@a1r+5Mq!yJ_GC1De{7M!m z9FUltotU2Llb@WJlUbEml9`|92MqDzg2d!h2BDJ54j! zJ=o^Z>d6KahB&XJyka5;*vVjjF%-LZh;jq@NNGi<$fz4C1haNpcl&j)P=0A%3dEpy z9cS+X^?@T3njVNtQ6RT~0LT>}@D~_Epg=?iFm;?r5};T^2iVlHqbmaPVPY`5{sHX; z`3W6>QVg;f7nB21s|96)QWP!#OW`1SbhD84s3CLE*dQ~|!W+y*WN0y<3tFMBfU8DG zO92f)O$U0xh`0mU2GR>kEpPxWguoFB69ilE2Q5Gj0*kNd-C$jm>3v-0%;Hky*yLQ==7S-?U5;2C!g$fygu<%{3%uj21cOV z+l&oQUOG+r!hOqdk>hSRJq@6kIuPr-C+4Q=yBm4NgBgycDVh2D!Ii}&sk!>W&hhT4 zd8tK-IY5OB3_?KM0x}B-;(%5v0%=ooGdE*5Q*$FDBSS+23kwr-a}zT+Co^XkCl_NQ z3q!bKkiN?-3=AAVdNS0#&Oo<;0Eh+|2;;j0`5+t)#N8ocPWQW}J1?q@^tvT|{}#v` z5C%Dhm4OY&{*t+r3B=RZ4s|uv4ob~QO)O5;HPtf%%QG;%cbJ4j9;gWn6sE^(ff%(w z`*_$OVlWaI%>O}Q2t*75NOF8YzGG2QVr4*4enDzcNhO2lhR2z_KvCz6%$yYGoW$bd z)MADfm4($Ht$z7=sSID3kMi;ZIsQpmsmUd9jgGfBzmf$C2P7tEC#I+RTQWL71X zWaj7j0VAilATc?WL8zp%gNXQYO-lnBUCh9$S{YA-a#-~4zjKCJ73c&4slm6jI5U|+ z_N8OH8ezr31*xgY84SxA7z#u{s)Lh@G7C!do%4%QLm?r+@WWHr4iq3TIoG`OOkg;* zIkb9$f*Kr%3?=0i6G5p48axce?j54sKt3>a!hEY!WYi56g867#cl&ith$@JychA~l z#|069x-5fNAOx%{zcddLVGQp&&fW#80jDrvVg&=Dk~zpK5CEA60>6RDMi9be0On9Q zfT`nwi$IbVh)YNvJ5&!Lb)dik0Yd6Pp#cKO>R@6pxBmnB1C*#i0H3|&Kwtd=;vYba m9KZ*Gb;0y+OB?j8k6Z+a%Yd~Zh_ZVYfh?!?YZ^d>KC$jm>3v-0%;K7l{qotpUpYlpZQz3yWiLii5BW5I?(@(#~V{*lG813qQAiey$*MKp5m0 zRt7d8`%C6hCLthCTRYU%R68g&CpED+Ro7I{3@p#U@ZMn(4tby^Fc8?@vl+y20NS?| zh>-y>kpF{15G1@4NsblB_sh>qW%$B;l$RgK@lVQ1O)d#2$}dPQDyd{}yuJCAEKoQg zF*!RiJ=G^aIWZ@*DzPLpKhF;s-o*up$*BxNC6ygKU`0i#c_q#{iN(bX5_gwIs{_Rd zM2mZBUTRTd4#WOCXPAkOMcJ2*?IIwvgOiIg3rh5z^NUhLAr5Ew;i+o}@&`=LH7`9A z=)E?FR!=sdFvM{seCHM7e={U^;<0Xj*ssbxw#7Bm^zR{U;rudU_!w30|ChD*x|AW z222cQ*FT`WTnK463zS;m93TVaZx8^{T3{9f11L4&0Kz%AhjrxQa}OJ6rdN3h&PZ;Af2Fe0te8d2OOC&L9p?E&;sKiu+WC$jm>3v-0%;JqeZ}_h`|T6ne4fM@{*|HQ61(ui9}`#@7#M+a zZ!L-o|v1e?{4H74`w))rex;p2UixCq~_`eJI4nX zq^2fk0M#)t@B(o&$S5E<2DDWXNE8l#^vwo36X+U--$2bEzzyO+X^??1K0i<#gyn(wTf>`Ac~74BOjSzV&DSz*0>~T? z204b6fepz1lDU*g2*}gc4s|uv4ob~QO)O5;HPtf%%QG;%cbJ4j9;gWnA~!pNjAU>D z+P4dckpVE2|AT@MB)ktvjupuF%g;+?_`-aYmmkRSPs&P7E(s{gFGwvasbp}xz4?_a zP&gnlIXf{u)h9nWF(j#RZAUsSH9Tl^r}_MMbH3CC)jC#l;K~cb7)1 z1H}l$i+gHbYEfbi!~Q#Gn2C?ZDWC{yBG3y)#2m;jkXBGqfdgpy1CCCZAlUprXu)w1SaMDOwzNUd`p894 Xyvx8;L+P@67J)3M_G=nI`5^TGycqb$ literal 0 HcmV?d00001 diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index 59dc22b..a88c2db 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -7,12 +7,13 @@ #include "Sound/SoundCue.h" static const FName LoopingTag("LoopingSound"); +static const FName OneTimeTag("PlayingOneTime"); UAudioComponent* UG2IGameSoundManager::GetAudioById(int32 SoundId) { if (SoundId < 0) { - UE_LOG(LogG2I, Warning, TEXT("Sound manager got wrong SoundId")); - return TObjectPtr(); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Wrong SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); + return nullptr; } TObjectPtr* FoundAudio = ActiveSounds.Find(SoundId); @@ -20,7 +21,7 @@ UAudioComponent* UG2IGameSoundManager::GetAudioById(int32 SoundId) return *FoundAudio; } - UE_LOG(LogG2I, Warning, TEXT("Sound manager: SoundId %d not found"), SoundId); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: SoundId %d not found"), *GetName(), *FString(__FUNCTION__), SoundId); return nullptr; } @@ -57,12 +58,12 @@ void UG2IGameSoundManager::Deinitialize() int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) { if (!ensure(NewSoundConfig.Sound)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager didn't get sound")); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager didn't get sound"), *GetName(), *FString(__FUNCTION__)); return -1; } UAudioComponent* AudioComponent = NewObject(GetWorld()->GetWorldSettings()); if (!ensure(AudioComponent)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager can't create an audio component")); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager can't create an audio component"), *GetName(), *FString(__FUNCTION__)); return -1; } @@ -93,7 +94,7 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) AudioComponent->ComponentTags.Add(LoopingTag); } AudioComponent->bAutoDestroy = NewSoundConfig.bIsLooping ? false : NewSoundConfig.bAutoDestroy; - + AudioComponent->RegisterComponent(); if (IdStack.IsEmpty()) { @@ -111,7 +112,7 @@ bool UG2IGameSoundManager::PlaySound(int32 SoundId, float FadeInTime) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager can't get an audio component for ID: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return false; } @@ -124,7 +125,7 @@ bool UG2IGameSoundManager::StopSound(int32 SoundId, float FadeOutTime) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager can't get an audio component for ID: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return false; } @@ -148,13 +149,13 @@ bool UG2IGameSoundManager::SetSoundVolume(int32 SoundId, float NewVolume) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager can't get an audio component for ID: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return false; } NewVolume = FMath::Clamp(NewVolume, 0.0f, 1.0f); Component->SetVolumeMultiplier(NewVolume); - + return true; } @@ -162,13 +163,13 @@ bool UG2IGameSoundManager::SetSoundPitch(int32 SoundId, float NewPitch) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager can't get an audio component for ID: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return false; } NewPitch = FMath::Clamp(NewPitch, 0.0f, 2.0f); Component->SetPitchMultiplier(NewPitch); - + return true; } @@ -176,12 +177,11 @@ bool UG2IGameSoundManager::SetSoundLocation(int32 SoundId, FVector NewLocation) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); return false; } Component->SetWorldLocation(NewLocation); - + return true; } @@ -190,12 +190,12 @@ bool UG2IGameSoundManager::SetSoundAttachment(int32 SoundId, EAttachmentRule AttachmentRules) { if (!ensure(NewAttachmentComponent)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager got wrong component to attach")); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager got wrong component to attach"), *GetName(), *FString(__FUNCTION__)); return false; } TObjectPtr Component = GetAudioById(SoundId); if (!ensure(Component)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get an audio component")); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager can't get an audio component for ID: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return false; } @@ -215,19 +215,19 @@ bool UG2IGameSoundManager::SetSoundAttachment(int32 SoundId, EAttachmentRule AttachmentRules) { if (!ensure(NewAttachmentActor)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager got wrong actor to attach")); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager got wrong actor to attach"), *GetName(), *FString(__FUNCTION__)); return false; } TObjectPtr RootComponent = NewAttachmentActor->GetRootComponent(); if (!ensure(RootComponent)) { - UE_LOG(LogG2I, Warning, TEXT("The sound manager can't get root component from actor to attach")); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager can't get root component from actor to attach"), *GetName(), *FString(__FUNCTION__)); return false; } TObjectPtr Component = GetAudioById(SoundId); if (!ensure(Component)) { return false; } - + FAttachmentTransformRules Rules( AttachmentRules, AttachmentRules, @@ -288,28 +288,28 @@ void UG2IGameSoundManager::InitGlobalAudio(USoundMix* _MainMix, TMap(SoundType)); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: SoundClasses map doesn't contain SoundType (ID: %d)"), *GetName(), *FString(__FUNCTION__), static_cast(SoundType)); return; } USoundClass* TargetClass = SoundClasses[SoundType]; if (!TargetClass) { - UE_LOG(LogG2I, Error, TEXT("%s: Can't get Sound class for the SoundType (ID: %d)"), *GetName(), static_cast(SoundType)); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Can't get Sound class for the SoundType (ID: %d)"), *GetName(), *FString(__FUNCTION__), static_cast(SoundType)); return; } @@ -337,6 +337,11 @@ void UG2IGameSoundManager::SetGlobalVolume(EG2IASoundType SoundType, float NewVo SpeechSoundMultiplier = ClampedVolume; break; } + case EG2IASoundType::SpeechSound: + { + SpeechSoundMultiplier = ClampedVolume; + break; + } } if (UWorld* World = GetWorld()) { @@ -351,7 +356,7 @@ void UG2IGameSoundManager::SetGlobalVolume(EG2IASoundType SoundType, float NewVo ); } else { - UE_LOG(LogG2I, Error, TEXT("%s: Can't get the World"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Can't get the World"), *GetName(), *FString(__FUNCTION__)); } } @@ -376,6 +381,11 @@ float UG2IGameSoundManager::GetGlobalVolume(EG2IASoundType SoundType) const { default: { return 0.0f; } + return SpeechSoundMultiplier; + } + default: { + return 0.0f; + } } } @@ -393,7 +403,7 @@ bool UG2IGameSoundManager::SetSoundLooping(int32 SoundId, bool bNewIsLooping) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { - UE_LOG(LogG2I, Warning, TEXT("%s: can't get an audio component (ID: %d)"), *GetName(), SoundId); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Can't get an audio component (ID: %d)"), *GetName(), *FString(__FUNCTION__), SoundId); return false; } @@ -417,7 +427,7 @@ float UG2IGameSoundManager::GetSoundVolume(int32 SoundId) return Component->VolumeMultiplier; } - UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return 0.0f; } @@ -428,7 +438,7 @@ float UG2IGameSoundManager::GetSoundPitch(int32 SoundId) return Component->PitchMultiplier; } - UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return 1.0f; } @@ -439,7 +449,7 @@ FVector UG2IGameSoundManager::GetSoundLocation(int32 SoundId) return Component->GetComponentLocation(); } - UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return FVector::ZeroVector; } @@ -450,7 +460,7 @@ USceneComponent* UG2IGameSoundManager::GetSoundAttachment(int32 SoundId) return Component->GetAttachParent(); } - UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return nullptr; } @@ -461,7 +471,7 @@ bool UG2IGameSoundManager::GetSoundLooping(int32 SoundId) return Component->ComponentHasTag(LoopingTag); } - UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return false; } @@ -470,7 +480,7 @@ bool UG2IGameSoundManager::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestr UAudioComponent* Component = GetAudioById(SoundId); if (!Component) { - UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return false; } @@ -478,7 +488,7 @@ bool UG2IGameSoundManager::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestr { if (bNewAutoDestroy) { - UE_LOG(LogG2I, Warning, TEXT("%s: SoundId %d is looping"), *GetName(), SoundId); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: SoundId %d is looping, AutoDestroy forced to false"), *GetName(), *FString(__FUNCTION__), SoundId); } Component->bAutoDestroy = false; @@ -498,6 +508,39 @@ bool UG2IGameSoundManager::GetSoundAutoDestroy(int32 SoundId) return Component->bAutoDestroy; } - UE_LOG(LogG2I, Warning, TEXT("%s: Invalid SoundId %d"), *GetName(), SoundId); + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); + return false; +} + +bool UG2IGameSoundManager::GetSoundPlayingOneTime(int32 SoundId) +{ + if (UAudioComponent* Component = GetAudioById(SoundId)) + { + return Component->ComponentHasTag(OneTimeTag); + } + + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return false; +} + +bool UG2IGameSoundManager::SetSoundPlayingOneTime(int32 SoundId, bool bNewIsPlayingOneTime) +{ + UAudioComponent* Component = GetAudioById(SoundId); + if (!Component) + { + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); + return false; + } + + if (bNewIsPlayingOneTime) + { + Component->ComponentTags.AddUnique(OneTimeTag); + Component->bAutoDestroy = true; + } + else + { + Component->ComponentTags.Remove(OneTimeTag); + } + + return true; } \ No newline at end of file diff --git a/Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp b/Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp new file mode 100644 index 0000000..d4146a8 --- /dev/null +++ b/Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp @@ -0,0 +1,39 @@ +#include "Gameplay/G2ISoundActivationZone.h" +#include "Components/BoxComponent.h" +#include "Components/G2ISoundComponent.h" +#include "G2I.h" + +AG2ISoundActivationZone::AG2ISoundActivationZone() +{ + OverlapComp = CreateDefaultSubobject(TEXT("OverlapComponent")); + if (OverlapComp) { + RootComponent = OverlapComp; + OverlapComp->SetCollisionProfileName(TEXT("Trigger")); + OverlapComp->OnComponentBeginOverlap.AddDynamic(this, &AG2ISoundActivationZone::OnZoneBeginOverlap); + + SoundComp = CreateDefaultSubobject(TEXT("SoundComponent")); + if (SoundComp) { + SoundComp->SetupAttachment(RootComponent); + test = SoundComp->AddSound(*SoundComp->SetupSounds.Find(FName("ToPlay"))); + } + else { + UE_LOG(LogG2I, Error, TEXT("Can't create SoundComponent in %s"), *GetName()); + } + } + else { + UE_LOG(LogG2I, Error, TEXT("Can't create BoxComponent in %s"), *GetName()); + } +} + +void AG2ISoundActivationZone::OnZoneBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, + bool bFromSweep, const FHitResult& SweepResult) { + + if (!SoundComp) { + return; + } + + if (!SoundComp->PlaySound(test)) { + UE_LOG(LogG2I, Error, TEXT("Can't play test music in %s"), *GetName()); + } +} \ No newline at end of file diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h index 37f814f..4dd33f2 100644 --- a/Source/G2I/Public/Game/G2IGameSoundManager.h +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -37,6 +37,9 @@ struct FSoundConfig UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play") bool bIsLooping = false; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play") + bool bIsPlayingOneTime = false; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Lifetime") bool bAutoDestroy = true; @@ -123,6 +126,7 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); bool SetSoundLooping(int32 SoundId, bool bNewIsLooping); bool SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy); + bool SetSoundPlayingOneTime(int32 SoundId, bool bNewIsPlayingOneTime); float GetSoundVolume(int32 SoundId); float GetSoundPitch(int32 SoundId); @@ -130,8 +134,14 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem USceneComponent* GetSoundAttachment(int32 SoundId); bool GetSoundLooping(int32 SoundId); bool GetSoundAutoDestroy(int32 SoundId); + bool GetSoundPlayingOneTime(int32 SoundId); + UFUNCTION(BlueprintCallable, Category = "Sound|Global") void InitGlobalAudio(USoundMix* _MainMix, TMap _SoundClasses); + + UFUNCTION(BlueprintCallable, Category = "Sound|Global") void SetGlobalVolume(EG2IASoundType SoundType, float NewVolume); + + UFUNCTION(BlueprintPure, Category = "Sound|Global") float GetGlobalVolume(EG2IASoundType SoundType) const; }; diff --git a/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h b/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h new file mode 100644 index 0000000..400f7e6 --- /dev/null +++ b/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h @@ -0,0 +1,35 @@ + + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Actor.h" +#include "G2ISoundActivationZone.generated.h" + +class UG2ISoundComponent; +class UBoxComponent; + +UCLASS() +class G2I_API AG2ISoundActivationZone : public AActor +{ + GENERATED_BODY() + +private: + UFUNCTION() + void OnZoneBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, + bool bFromSweep, const FHitResult& SweepResult); + + int32 test; +public: + AG2ISoundActivationZone(); + + +protected: + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components") + TObjectPtr OverlapComp; + + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components") + TObjectPtr SoundComp; + +}; From efc2611eb9a9fd7da277c000ea32ed4ac06cb939 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 3 Apr 2026 00:12:16 +0300 Subject: [PATCH 11/22] Logs refact for sound component --- .../Private/Components/G2ISoundComponent.cpp | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/Source/G2I/Private/Components/G2ISoundComponent.cpp b/Source/G2I/Private/Components/G2ISoundComponent.cpp index 222818a..eb4995f 100644 --- a/Source/G2I/Private/Components/G2ISoundComponent.cpp +++ b/Source/G2I/Private/Components/G2ISoundComponent.cpp @@ -1,5 +1,4 @@ #include "Components/G2ISoundComponent.h" - #include "G2I.h" void UG2ISoundComponent::BeginPlay() @@ -8,12 +7,12 @@ void UG2ISoundComponent::BeginPlay() UWorld* World = GetWorld(); if (!ensure(World)) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the World in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the World"), *GetName(), *FString(__FUNCTION__)); return; } SoundManager = World->GetSubsystem(); if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); } } @@ -21,14 +20,14 @@ void UG2ISoundComponent::BeginPlay() int32 UG2ISoundComponent::AddSound(const FSoundConfig& NewSoundConfig) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return -1; } FSoundConfig ConfigToSend = NewSoundConfig; AActor* Owner = GetOwner(); if (!ensure(Owner)) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the owner of component in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the owner of component"), *GetName(), *FString(__FUNCTION__)); return -1; } @@ -47,7 +46,7 @@ int32 UG2ISoundComponent::AddSound(const FSoundConfig& NewSoundConfig) bool UG2ISoundComponent::PlaySound(int32 SoundId) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } return SoundManager->PlaySound(SoundId); @@ -56,7 +55,7 @@ bool UG2ISoundComponent::PlaySound(int32 SoundId) bool UG2ISoundComponent::StopSound(int32 SoundId, float FadeOutTime) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } return SoundManager->StopSound(SoundId, FadeOutTime); @@ -65,7 +64,7 @@ bool UG2ISoundComponent::StopSound(int32 SoundId, float FadeOutTime) void UG2ISoundComponent::StopAllSounds() { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return; } SoundManager->StopAllSounds(); @@ -74,7 +73,7 @@ void UG2ISoundComponent::StopAllSounds() bool UG2ISoundComponent::SetSoundVolume(int32 SoundId, float NewVolume) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } return SoundManager->SetSoundVolume(SoundId, NewVolume); @@ -83,7 +82,7 @@ bool UG2ISoundComponent::SetSoundVolume(int32 SoundId, float NewVolume) bool UG2ISoundComponent::SetSoundPitch(int32 SoundId, float NewPitch) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } return SoundManager->SetSoundPitch(SoundId, NewPitch); @@ -92,7 +91,7 @@ bool UG2ISoundComponent::SetSoundPitch(int32 SoundId, float NewPitch) bool UG2ISoundComponent::SetSoundLocation(int32 SoundId, FVector NewLocation) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } return SoundManager->SetSoundLocation(SoundId, NewLocation); @@ -103,7 +102,7 @@ bool UG2ISoundComponent::SetSoundAttachment(int32 SoundId, EAttachmentRule AttachmentRules) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } @@ -115,7 +114,7 @@ bool UG2ISoundComponent::SetSoundAttachment(int32 SoundId, EAttachmentRule AttachmentRules) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } @@ -125,7 +124,7 @@ bool UG2ISoundComponent::SetSoundAttachment(int32 SoundId, bool UG2ISoundComponent::RemoveSound(int32 SoundId) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } return SoundManager->RemoveSound(SoundId); @@ -134,7 +133,7 @@ bool UG2ISoundComponent::RemoveSound(int32 SoundId) void UG2ISoundComponent::RemoveAllSounds() { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return; } SoundManager->RemoveAllSounds(); @@ -143,7 +142,7 @@ void UG2ISoundComponent::RemoveAllSounds() bool UG2ISoundComponent::SetSoundLooping(int32 SoundId, bool bNewIsLooping) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } return SoundManager->SetSoundLooping(SoundId, bNewIsLooping); @@ -152,7 +151,7 @@ bool UG2ISoundComponent::SetSoundLooping(int32 SoundId, bool bNewIsLooping) bool UG2ISoundComponent::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } return SoundManager->SetSoundAutoDestroy(SoundId, bNewAutoDestroy); @@ -161,7 +160,7 @@ bool UG2ISoundComponent::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy float UG2ISoundComponent::GetSoundVolume(int32 SoundId) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return 0.0f; } return SoundManager->GetSoundVolume(SoundId); @@ -170,7 +169,7 @@ float UG2ISoundComponent::GetSoundVolume(int32 SoundId) float UG2ISoundComponent::GetSoundPitch(int32 SoundId) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return 1.0f; } return SoundManager->GetSoundPitch(SoundId); @@ -179,7 +178,7 @@ float UG2ISoundComponent::GetSoundPitch(int32 SoundId) FVector UG2ISoundComponent::GetSoundLocation(int32 SoundId) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return FVector::ZeroVector; } return SoundManager->GetSoundLocation(SoundId); @@ -188,7 +187,7 @@ FVector UG2ISoundComponent::GetSoundLocation(int32 SoundId) bool UG2ISoundComponent::GetSoundLooping(int32 SoundId) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } return SoundManager->GetSoundLooping(SoundId); @@ -197,7 +196,7 @@ bool UG2ISoundComponent::GetSoundLooping(int32 SoundId) bool UG2ISoundComponent::GetSoundAutoDestroy(int32 SoundId) { if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("Couldn't get the Sound Manager in %s"), *GetName()); + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); return false; } return SoundManager->GetSoundAutoDestroy(SoundId); From fd41aba725a0d6a574ba421bcfce2dcc626b3ef4 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 3 Apr 2026 00:27:34 +0300 Subject: [PATCH 12/22] Edit properties --- Source/G2I/Public/Gameplay/G2ISoundActivationZone.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h b/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h index 400f7e6..626b029 100644 --- a/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h +++ b/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h @@ -1,5 +1,3 @@ - - #pragma once #include "CoreMinimal.h" @@ -24,12 +22,10 @@ class G2I_API AG2ISoundActivationZone : public AActor public: AG2ISoundActivationZone(); - -protected: - UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components") + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components") TObjectPtr OverlapComp; - UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components") + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components") TObjectPtr SoundComp; }; From 4d4a6f22b275d158ab08ff7420cee46956197d02 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 3 Apr 2026 00:40:40 +0300 Subject: [PATCH 13/22] Removed 1 of switch case state --- Source/G2I/Private/Game/G2IGameSoundManager.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index a88c2db..3515068 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -337,11 +337,6 @@ void UG2IGameSoundManager::SetGlobalVolume(EG2IASoundType SoundType, float NewVo SpeechSoundMultiplier = ClampedVolume; break; } - case EG2IASoundType::SpeechSound: - { - SpeechSoundMultiplier = ClampedVolume; - break; - } } if (UWorld* World = GetWorld()) { @@ -381,11 +376,6 @@ float UG2IGameSoundManager::GetGlobalVolume(EG2IASoundType SoundType) const { default: { return 0.0f; } - return SpeechSoundMultiplier; - } - default: { - return 0.0f; - } } } From e6e8565712aaaa3088dd2abe1045bce3433587b8 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 3 Apr 2026 11:36:22 +0300 Subject: [PATCH 14/22] Fix for test --- .../Gameplay/G2ISoundActivationZone.cpp | 18 +++++++++++++++--- .../Public/Gameplay/G2ISoundActivationZone.h | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp b/Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp index d4146a8..7790616 100644 --- a/Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp +++ b/Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp @@ -14,14 +14,26 @@ AG2ISoundActivationZone::AG2ISoundActivationZone() SoundComp = CreateDefaultSubobject(TEXT("SoundComponent")); if (SoundComp) { SoundComp->SetupAttachment(RootComponent); - test = SoundComp->AddSound(*SoundComp->SetupSounds.Find(FName("ToPlay"))); } else { UE_LOG(LogG2I, Error, TEXT("Can't create SoundComponent in %s"), *GetName()); } } - else { - UE_LOG(LogG2I, Error, TEXT("Can't create BoxComponent in %s"), *GetName()); +} + +void AG2ISoundActivationZone::BeginPlay() +{ + Super::BeginPlay(); + if (SoundComp) + { + if (FSoundConfig* FoundConfig = SoundComp->SetupSounds.Find(FName("ToPlay"))) + { + test = SoundComp->AddSound(*FoundConfig); + } + else + { + UE_LOG(LogG2I, Warning, TEXT("[%s]: Sound key 'ToPlay' not found in SetupSounds map!"), *GetName()); + } } } diff --git a/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h b/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h index 626b029..3a205b0 100644 --- a/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h +++ b/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h @@ -19,6 +19,8 @@ class G2I_API AG2ISoundActivationZone : public AActor bool bFromSweep, const FHitResult& SweepResult); int32 test; +protected: + virtual void BeginPlay() override; public: AG2ISoundActivationZone(); From 757cccebb77c0427898e2f542ff5531d89c9496f Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 3 Apr 2026 12:05:08 +0300 Subject: [PATCH 15/22] Removed loop response from manager; sound component refact; sound maanger changed base sound --- .../Private/Components/G2ISoundComponent.cpp | 31 +------- .../G2I/Private/Game/G2IGameSoundManager.cpp | 73 +++++-------------- .../G2I/Public/Components/G2ISoundComponent.h | 19 ++--- Source/G2I/Public/Game/G2IGameSoundManager.h | 18 +---- 4 files changed, 32 insertions(+), 109 deletions(-) diff --git a/Source/G2I/Private/Components/G2ISoundComponent.cpp b/Source/G2I/Private/Components/G2ISoundComponent.cpp index eb4995f..bdf7a92 100644 --- a/Source/G2I/Private/Components/G2ISoundComponent.cpp +++ b/Source/G2I/Private/Components/G2ISoundComponent.cpp @@ -97,7 +97,7 @@ bool UG2ISoundComponent::SetSoundLocation(int32 SoundId, FVector NewLocation) return SoundManager->SetSoundLocation(SoundId, NewLocation); } -bool UG2ISoundComponent::SetSoundAttachment(int32 SoundId, +bool UG2ISoundComponent::SetSoundAttachmentComponent(int32 SoundId, USceneComponent* NewAttachmentComponent, EAttachmentRule AttachmentRules) { @@ -109,7 +109,7 @@ bool UG2ISoundComponent::SetSoundAttachment(int32 SoundId, return SoundManager->SetSoundAttachment(SoundId, NewAttachmentComponent, AttachmentRules); } -bool UG2ISoundComponent::SetSoundAttachment(int32 SoundId, +bool UG2ISoundComponent::SetSoundAttachmentActor(int32 SoundId, AActor* NewAttachmentActor, EAttachmentRule AttachmentRules) { @@ -130,24 +130,6 @@ bool UG2ISoundComponent::RemoveSound(int32 SoundId) return SoundManager->RemoveSound(SoundId); } -void UG2ISoundComponent::RemoveAllSounds() -{ - if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); - return; - } - SoundManager->RemoveAllSounds(); -} - -bool UG2ISoundComponent::SetSoundLooping(int32 SoundId, bool bNewIsLooping) -{ - if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); - return false; - } - return SoundManager->SetSoundLooping(SoundId, bNewIsLooping); -} - bool UG2ISoundComponent::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy) { if (!SoundManager.IsValid()) { @@ -184,15 +166,6 @@ FVector UG2ISoundComponent::GetSoundLocation(int32 SoundId) return SoundManager->GetSoundLocation(SoundId); } -bool UG2ISoundComponent::GetSoundLooping(int32 SoundId) -{ - if (!SoundManager.IsValid()) { - UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); - return false; - } - return SoundManager->GetSoundLooping(SoundId); -} - bool UG2ISoundComponent::GetSoundAutoDestroy(int32 SoundId) { if (!SoundManager.IsValid()) { diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index 3515068..1acc1a9 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -6,7 +6,6 @@ #include "Kismet/GameplayStatics.h" #include "Sound/SoundCue.h" -static const FName LoopingTag("LoopingSound"); static const FName OneTimeTag("PlayingOneTime"); UAudioComponent* UG2IGameSoundManager::GetAudioById(int32 SoundId) @@ -90,10 +89,9 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) AudioComponent->SetVolumeMultiplier(NewSoundConfig.VolumeMultiplier); AudioComponent->SetPitchMultiplier(NewSoundConfig.PitchMultiplier); - if (NewSoundConfig.bIsLooping) { - AudioComponent->ComponentTags.Add(LoopingTag); - } - AudioComponent->bAutoDestroy = NewSoundConfig.bIsLooping ? false : NewSoundConfig.bAutoDestroy; + bool bAssetIsLooping = NewSoundConfig.Sound->IsLooping(); + + AudioComponent->bAutoDestroy = bAssetIsLooping ? false : NewSoundConfig.bAutoDestroy; AudioComponent->RegisterComponent(); @@ -243,15 +241,18 @@ bool UG2IGameSoundManager::SetSoundAttachment(int32 SoundId, bool UG2IGameSoundManager::RemoveSound(int32 SoundId) { TObjectPtr Component = GetAudioById(SoundId); - if (!ensure(Component)) { + if (!Component) { return false; } + Component->OnAudioFinishedNative.RemoveAll(this); + Component->Stop(); Component->DestroyComponent(); ActiveSounds.Remove(SoundId); IdStack.Push(SoundId); + return true; } @@ -261,6 +262,8 @@ void UG2IGameSoundManager::RemoveAllSounds() { if (ensure(Pair.Value())) { + Pair.Value()->OnAudioFinishedNative.RemoveAll(this); + Pair.Value()->Stop(); Pair.Value()->DestroyComponent(); } @@ -381,35 +384,19 @@ float UG2IGameSoundManager::GetGlobalVolume(EG2IASoundType SoundType) const { void UG2IGameSoundManager::OnSoundFinished(UAudioComponent* AudioComponent, int32 SoundId) { - if (AudioComponent && AudioComponent->ComponentHasTag("LoopingSound")) { - AudioComponent->Play(); - } - else { - RemoveSound(SoundId); - } -} - -bool UG2IGameSoundManager::SetSoundLooping(int32 SoundId, bool bNewIsLooping) -{ - UAudioComponent* Component = GetAudioById(SoundId); - if (!ensure(Component)) { - UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Can't get an audio component (ID: %d)"), *GetName(), *FString(__FUNCTION__), SoundId); - return false; + if (!AudioComponent) { + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Can't get AudioComponent when sound with ID %d finished"), *GetName(), *FString(__FUNCTION__), SoundId); + ActiveSounds.Remove(SoundId); + IdStack.Push(SoundId); + return; } - if (bNewIsLooping) + if (AudioComponent->bAutoDestroy) { - Component->ComponentTags.AddUnique(LoopingTag); - Component->bAutoDestroy = false; + ActiveSounds.Remove(SoundId); + IdStack.Push(SoundId); } - else - { - Component->ComponentTags.Remove(LoopingTag); - } - - return true; } - float UG2IGameSoundManager::GetSoundVolume(int32 SoundId) { if (UAudioComponent* Component = GetAudioById(SoundId)) @@ -454,17 +441,6 @@ USceneComponent* UG2IGameSoundManager::GetSoundAttachment(int32 SoundId) return nullptr; } -bool UG2IGameSoundManager::GetSoundLooping(int32 SoundId) -{ - if (UAudioComponent* Component = GetAudioById(SoundId)) - { - return Component->ComponentHasTag(LoopingTag); - } - - UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); - return false; -} - bool UG2IGameSoundManager::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy) { UAudioComponent* Component = GetAudioById(SoundId); @@ -473,20 +449,7 @@ bool UG2IGameSoundManager::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestr UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Invalid SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return false; } - - if (Component->ComponentHasTag(LoopingTag)) - { - if (bNewAutoDestroy) - { - UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: SoundId %d is looping, AutoDestroy forced to false"), *GetName(), *FString(__FUNCTION__), SoundId); - } - - Component->bAutoDestroy = false; - } - else - { - Component->bAutoDestroy = bNewAutoDestroy; - } + Component->bAutoDestroy = bNewAutoDestroy; return true; } diff --git a/Source/G2I/Public/Components/G2ISoundComponent.h b/Source/G2I/Public/Components/G2ISoundComponent.h index c76ddb1..dcc841e 100644 --- a/Source/G2I/Public/Components/G2ISoundComponent.h +++ b/Source/G2I/Public/Components/G2ISoundComponent.h @@ -25,8 +25,7 @@ class G2I_API UG2ISoundComponent : public USceneComponent UFUNCTION(BlueprintCallable, Category = "Sounds") bool RemoveSound(int32 SoundId); - UFUNCTION(BlueprintCallable, Category = "Sounds") - void RemoveAllSounds(); + UFUNCTION(BlueprintCallable, Category = "Sounds") bool PlaySound(int32 SoundId); @@ -37,6 +36,8 @@ class G2I_API UG2ISoundComponent : public USceneComponent UFUNCTION(BlueprintCallable, Category = "Sounds") void StopAllSounds(); + + UFUNCTION(BlueprintCallable, Category = "Sounds") bool SetSoundVolume(int32 SoundId, float NewVolume); @@ -46,24 +47,24 @@ class G2I_API UG2ISoundComponent : public USceneComponent UFUNCTION(BlueprintCallable, Category = "Sounds") bool SetSoundLocation(int32 SoundId, FVector NewLocation); - bool SetSoundAttachment(int32 SoundId, + UFUNCTION(BlueprintCallable, Category = "Sounds") + bool SetSoundAttachmentComponent(int32 SoundId, USceneComponent* NewAttachementComponent, EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); - bool SetSoundAttachment(int32 SoundId, + + UFUNCTION(BlueprintCallable, Category = "Sounds") + bool SetSoundAttachmentActor(int32 SoundId, AActor* NewAttachementActor, EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); UFUNCTION(BlueprintCallable, Category = "Sounds") bool SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy); - UFUNCTION(BlueprintCallable, Category = "Sounds") - bool SetSoundLooping(int32 SoundId, bool bNewIsLooping); - UFUNCTION(BlueprintPure, Category = "Sounds") - float GetSoundVolume(int32 SoundId); + UFUNCTION(BlueprintPure, Category = "Sounds") - bool GetSoundLooping(int32 SoundId); + float GetSoundVolume(int32 SoundId); UFUNCTION(BlueprintPure, Category = "Sounds") float GetSoundPitch(int32 SoundId); diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h index 4dd33f2..b90b48d 100644 --- a/Source/G2I/Public/Game/G2IGameSoundManager.h +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -5,7 +5,7 @@ #include "Engine/EngineTypes.h" #include "Components/AudioComponent.h" #include "Components/SceneComponent.h" -#include "Sound/SoundCue.h" +#include "Sound/SoundBase.h" #include "Sound/SoundClass.h" #include "Sound/SoundMix.h" #include "G2IGameSoundManager.generated.h" @@ -15,7 +15,7 @@ struct FSoundConfig { GENERATED_BODY() UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Sound") - TObjectPtr Sound; + TObjectPtr Sound; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Location") FVector WorldLocation = FVector::ZeroVector; @@ -34,9 +34,6 @@ struct FSoundConfig UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play", meta = (ClampMin = "0.1", ClampMax = "2.0")) float PitchMultiplier = 1.0f; - - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play") - bool bIsLooping = false; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play") bool bIsPlayingOneTime = false; @@ -46,12 +43,6 @@ struct FSoundConfig UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Location") bool bIs2D = false; - - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play", meta = (ClampMin = "0.0")) - float FadeInTime = 0.0f; - - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play", meta = (ClampMin = "0.0")) - float FadeOutTime = 0.0f; }; UENUM(BlueprintType) @@ -73,9 +64,6 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem UPROPERTY() TArray IdStack; - UPROPERTY() - TArray LoopingSoundsId; - int32 CurrentNumberAvailable; UPROPERTY() @@ -124,7 +112,6 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem bool SetSoundAttachment(int32 SoundId, AActor* NewAttachementActor, EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); - bool SetSoundLooping(int32 SoundId, bool bNewIsLooping); bool SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy); bool SetSoundPlayingOneTime(int32 SoundId, bool bNewIsPlayingOneTime); @@ -132,7 +119,6 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem float GetSoundPitch(int32 SoundId); FVector GetSoundLocation(int32 SoundId); USceneComponent* GetSoundAttachment(int32 SoundId); - bool GetSoundLooping(int32 SoundId); bool GetSoundAutoDestroy(int32 SoundId); bool GetSoundPlayingOneTime(int32 SoundId); From a4038cb928baf258b2fdbfd6369e5fcb85fa32eb Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 3 Apr 2026 12:40:38 +0300 Subject: [PATCH 16/22] Removed sound zone --- .../Gameplay/G2ISoundActivationZone.cpp | 51 ------------------- .../Public/Gameplay/G2ISoundActivationZone.h | 33 ------------ 2 files changed, 84 deletions(-) delete mode 100644 Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp delete mode 100644 Source/G2I/Public/Gameplay/G2ISoundActivationZone.h diff --git a/Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp b/Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp deleted file mode 100644 index 7790616..0000000 --- a/Source/G2I/Private/Gameplay/G2ISoundActivationZone.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "Gameplay/G2ISoundActivationZone.h" -#include "Components/BoxComponent.h" -#include "Components/G2ISoundComponent.h" -#include "G2I.h" - -AG2ISoundActivationZone::AG2ISoundActivationZone() -{ - OverlapComp = CreateDefaultSubobject(TEXT("OverlapComponent")); - if (OverlapComp) { - RootComponent = OverlapComp; - OverlapComp->SetCollisionProfileName(TEXT("Trigger")); - OverlapComp->OnComponentBeginOverlap.AddDynamic(this, &AG2ISoundActivationZone::OnZoneBeginOverlap); - - SoundComp = CreateDefaultSubobject(TEXT("SoundComponent")); - if (SoundComp) { - SoundComp->SetupAttachment(RootComponent); - } - else { - UE_LOG(LogG2I, Error, TEXT("Can't create SoundComponent in %s"), *GetName()); - } - } -} - -void AG2ISoundActivationZone::BeginPlay() -{ - Super::BeginPlay(); - if (SoundComp) - { - if (FSoundConfig* FoundConfig = SoundComp->SetupSounds.Find(FName("ToPlay"))) - { - test = SoundComp->AddSound(*FoundConfig); - } - else - { - UE_LOG(LogG2I, Warning, TEXT("[%s]: Sound key 'ToPlay' not found in SetupSounds map!"), *GetName()); - } - } -} - -void AG2ISoundActivationZone::OnZoneBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, - bool bFromSweep, const FHitResult& SweepResult) { - - if (!SoundComp) { - return; - } - - if (!SoundComp->PlaySound(test)) { - UE_LOG(LogG2I, Error, TEXT("Can't play test music in %s"), *GetName()); - } -} \ No newline at end of file diff --git a/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h b/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h deleted file mode 100644 index 3a205b0..0000000 --- a/Source/G2I/Public/Gameplay/G2ISoundActivationZone.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include "CoreMinimal.h" -#include "GameFramework/Actor.h" -#include "G2ISoundActivationZone.generated.h" - -class UG2ISoundComponent; -class UBoxComponent; - -UCLASS() -class G2I_API AG2ISoundActivationZone : public AActor -{ - GENERATED_BODY() - -private: - UFUNCTION() - void OnZoneBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, - bool bFromSweep, const FHitResult& SweepResult); - - int32 test; -protected: - virtual void BeginPlay() override; -public: - AG2ISoundActivationZone(); - - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components") - TObjectPtr OverlapComp; - - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components") - TObjectPtr SoundComp; - -}; From 30957fe82a5cc39a7f4a636815916669aedc9090 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 3 Apr 2026 12:49:16 +0300 Subject: [PATCH 17/22] Changed variables to map --- .../G2I/Private/Game/G2IGameSoundManager.cpp | 53 ++++--------------- Source/G2I/Public/Game/G2IGameSoundManager.h | 7 +-- 2 files changed, 11 insertions(+), 49 deletions(-) diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index 1acc1a9..08abbe6 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -283,8 +283,15 @@ void UG2IGameSoundManager::UpdateStackSize() { void UG2IGameSoundManager::InitGlobalAudio(USoundMix* _MainMix, TMap _SoundClasses) { + GlobalSoundMultipliers.Empty(); MainSoundMix = _MainMix; SoundClasses = _SoundClasses; + + GlobalSoundMultipliers.Add(EG2IASoundType::SpeechSound, 1.0f); + GlobalSoundMultipliers.Add(EG2IASoundType::MusicSound, 1.0f); + GlobalSoundMultipliers.Add(EG2IASoundType::EffectSound, 1.0f); + GlobalSoundMultipliers.Add(EG2IASoundType::DefaultSound, 1.0f); + UWorld* World = GetWorld(); if (MainSoundMix && World) { @@ -318,29 +325,7 @@ void UG2IGameSoundManager::SetGlobalVolume(EG2IASoundType SoundType, float NewVo float ClampedVolume = FMath::Clamp(NewVolume, 0.0f, 1.0f); - switch (SoundType) - { - case EG2IASoundType::DefaultSound: - { - GeneralSoundMultiplier = ClampedVolume; - break; - } - case EG2IASoundType::EffectSound: - { - EffectsSoundMultiplier = ClampedVolume; - break; - } - case EG2IASoundType::MusicSound: - { - MusicSoundMultiplier = ClampedVolume; - break; - } - case EG2IASoundType::SpeechSound: - { - SpeechSoundMultiplier = ClampedVolume; - break; - } - } + GlobalSoundMultipliers[SoundType] = ClampedVolume; if (UWorld* World = GetWorld()) { UGameplayStatics::SetSoundMixClassOverride( @@ -359,27 +344,7 @@ void UG2IGameSoundManager::SetGlobalVolume(EG2IASoundType SoundType, float NewVo } float UG2IGameSoundManager::GetGlobalVolume(EG2IASoundType SoundType) const { - switch (SoundType) { - case EG2IASoundType::DefaultSound: - { - return GeneralSoundMultiplier; - } - case EG2IASoundType::EffectSound: - { - return EffectsSoundMultiplier; - } - case EG2IASoundType::MusicSound: - { - return MusicSoundMultiplier; - } - case EG2IASoundType::SpeechSound: - { - return SpeechSoundMultiplier; - } - default: { - return 0.0f; - } - } + return GlobalSoundMultipliers[SoundType]; } void UG2IGameSoundManager::OnSoundFinished(UAudioComponent* AudioComponent, int32 SoundId) diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h index b90b48d..64eb763 100644 --- a/Source/G2I/Public/Game/G2IGameSoundManager.h +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -70,11 +70,8 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem TMap> ActiveSounds; void UpdateStackSize(); - - float GeneralSoundMultiplier = 1.0f; - float EffectsSoundMultiplier = 1.0f; - float MusicSoundMultiplier = 1.0f; - float SpeechSoundMultiplier = 1.0f; + UPROPERTY() + TMap GlobalSoundMultipliers; UPROPERTY() USoundMix* MainSoundMix; From 54376ffe17b8011f54a02ac3754e746bfb88f47a Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Fri, 3 Apr 2026 12:51:41 +0300 Subject: [PATCH 18/22] Removed extra line --- Source/G2I/Private/Game/G2IGameSoundManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index 08abbe6..ed80e09 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -67,7 +67,6 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) } AudioComponent->SetSound(NewSoundConfig.Sound.Get()); - AudioComponent->SetWorldLocation(NewSoundConfig.WorldLocation); if (!NewSoundConfig.bIs2D) { @@ -86,6 +85,7 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) { AudioComponent->bAllowSpatialization = false; } + AudioComponent->SetVolumeMultiplier(NewSoundConfig.VolumeMultiplier); AudioComponent->SetPitchMultiplier(NewSoundConfig.PitchMultiplier); From a0c1d45d3c4c944e38f43fbdf28acebc473ed498 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Sat, 4 Apr 2026 00:49:55 +0300 Subject: [PATCH 19/22] Changed from sound manager from world sub system to gameinstance subsytem; moved system to new folder; constness and etc refact --- Source/G2I/G2I.Build.cs | 6 +- .../Private/Components/G2ISoundComponent.cpp | 4 +- .../G2I/Private/Game/G2IGameSoundManager.cpp | 230 +++++++++++++----- .../G2I/Public/Components/G2ISoundComponent.h | 5 +- Source/G2I/Public/Game/G2IGameSoundManager.h | 98 ++++---- 5 files changed, 230 insertions(+), 113 deletions(-) diff --git a/Source/G2I/G2I.Build.cs b/Source/G2I/G2I.Build.cs index ecf4561..bc6beca 100644 --- a/Source/G2I/G2I.Build.cs +++ b/Source/G2I/G2I.Build.cs @@ -50,7 +50,8 @@ public G2I(ReadOnlyTargetRules Target) : base(Target) "G2I/Public/UI/WidgetComponents", "G2I/Public/Components", "G2I/Public/Components/Camera", - "G2I/Public/Components/SteamGlove" + "G2I/Public/Components/SteamGlove", + "G2I/Public/Sound" }); PrivateIncludePaths.AddRange(new string[] { @@ -71,7 +72,8 @@ public G2I(ReadOnlyTargetRules Target) : base(Target) "G2I/Private/UI/WidgetComponents", "G2I/Private/Components", "G2I/Private/Components/Camera", - "G2I/Private/Components/SteamGlove" + "G2I/Private/Components/SteamGlove", + "G2I/Private/Sound" }); // Uncomment if you are using Slate UI diff --git a/Source/G2I/Private/Components/G2ISoundComponent.cpp b/Source/G2I/Private/Components/G2ISoundComponent.cpp index bdf7a92..9260689 100644 --- a/Source/G2I/Private/Components/G2ISoundComponent.cpp +++ b/Source/G2I/Private/Components/G2ISoundComponent.cpp @@ -5,12 +5,12 @@ void UG2ISoundComponent::BeginPlay() { Super::BeginPlay(); - UWorld* World = GetWorld(); + const UWorld* World = GetWorld(); if (!ensure(World)) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the World"), *GetName(), *FString(__FUNCTION__)); return; } - SoundManager = World->GetSubsystem(); + SoundManager = UG2IGameSoundManager::Get(World); if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); } diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Game/G2IGameSoundManager.cpp index ed80e09..875246c 100644 --- a/Source/G2I/Private/Game/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Game/G2IGameSoundManager.cpp @@ -1,21 +1,24 @@ #include "Game/G2IGameSoundManager.h" -#define STARTSIZE 20 +#define STARTSOUNDSTACKSIZE 20 #include "G2I.h" #include "Components/AudioComponent.h" #include "Components/SceneComponent.h" +#include "Sound/SoundBase.h" +#include "Sound/SoundClass.h" +#include "Sound/SoundMix.h" #include "Kismet/GameplayStatics.h" -#include "Sound/SoundCue.h" +#include "G2IGameInstance.h" static const FName OneTimeTag("PlayingOneTime"); -UAudioComponent* UG2IGameSoundManager::GetAudioById(int32 SoundId) +TObjectPtr UG2IGameSoundManager::GetAudioById(const int32 SoundId) const { if (SoundId < 0) { UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Wrong SoundId: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return nullptr; } - TObjectPtr* FoundAudio = ActiveSounds.Find(SoundId); + const TObjectPtr* FoundAudio = ActiveSounds.Find(SoundId); if (FoundAudio) { return *FoundAudio; } @@ -24,11 +27,18 @@ UAudioComponent* UG2IGameSoundManager::GetAudioById(int32 SoundId) return nullptr; } -UG2IGameSoundManager* UG2IGameSoundManager::Get(UObject* WorldContextObject) +UG2IGameSoundManager* UG2IGameSoundManager::Get(const UObject* WorldContextObject) { - if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::ReturnNull)) + if (!GEngine) { + return nullptr; + } + + if (const UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::ReturnNull)) { - return World->GetSubsystem(); + if (const UGameInstance* GameInstance = World->GetGameInstance()) + { + return GameInstance->GetSubsystem(); + } } return nullptr; @@ -38,18 +48,85 @@ void UG2IGameSoundManager::Initialize(FSubsystemCollectionBase& Collection) { Super::Initialize(Collection); - for (int32 i = 0; i < STARTSIZE; ++i) + for (int32 i = 0; i < STARTSOUNDSTACKSIZE; ++i) + { + IdStack.Push(i); + } + CurrentNumberAvailable = STARTSOUNDSTACKSIZE; + + GameInstance = Cast(GetGameInstance()); + if (!ensure(GameInstance)) + { + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't find %s"), *GetName(), *FString(__FUNCTION__), *UG2IGameInstance::StaticClass()->GetName()); + return; + } + + StartGameDelegateHandle = GameInstance->OnStartLevelInitDelegate.AddUObject(this, &ThisClass::InitializeInStartGame); +} + +void UG2IGameSoundManager::InitializeInStartGame() +{ + if (!ensure(GameInstance)) { + UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't find %s"), *GetName(), *FString(__FUNCTION__), *UG2IGameInstance::StaticClass()->GetName()); + return; + } + GameInstance->OnStartLevelInitDelegate.Remove(StartGameDelegateHandle); + + InitializeInStartLevel(); + + GameInstance->OnCloseLevelDelegate.AddUObject(this, &ThisClass::CloseLevelSound); + GameInstance->OnStartLevelInitDelegate.AddUObject(this, &ThisClass::InitializeInStartLevel); +} + +void UG2IGameSoundManager::InitializeInStartLevel() +{ + UWorld* NewWorld = GetWorld(); + + if (MainSoundMix && NewWorld) + { + UGameplayStatics::PushSoundMixModifier(NewWorld, MainSoundMix); + + for (const auto& Iterator : GlobalSoundMultipliers) + { + if (USoundClass* TargetClass = SoundClasses.FindRef(Iterator.Key)) + { + UGameplayStatics::SetSoundMixClassOverride( + NewWorld, + MainSoundMix, + TargetClass, + Iterator.Value, + 1.0f, + 0.0f, + true + ); + } + } + } +} + +void UG2IGameSoundManager::CloseLevelSound() +{ + ActiveSounds.Empty(); + IdStack.Empty(); + CurrentNumberAvailable = STARTSOUNDSTACKSIZE; + + for (int32 i = 0; i < CurrentNumberAvailable; ++i) { IdStack.Push(i); } - CurrentNumberAvailable = STARTSIZE; } void UG2IGameSoundManager::Deinitialize() { StopAllSounds(); - IdStack.Empty(); ActiveSounds.Empty(); + IdStack.Empty(); + + if (GameInstance) + { + GameInstance->OnStartLevelInitDelegate.RemoveAll(this); + GameInstance->OnCloseLevelDelegate.RemoveAll(this); + } Super::Deinitialize(); } @@ -60,7 +137,22 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager didn't get sound"), *GetName(), *FString(__FUNCTION__)); return -1; } - UAudioComponent* AudioComponent = NewObject(GetWorld()->GetWorldSettings()); + + const UWorld* World = GetWorld(); + if (!ensure(World)) + { + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: World is null"), *GetName(), *FString(__FUNCTION__)); + return -1; + } + + AWorldSettings* WorldSettings = World->GetWorldSettings(); + if (!ensure(WorldSettings)) + { + UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: Can't get WorldSettings"), *GetName(), *FString(__FUNCTION__)); + return -1; + } + + UAudioComponent* AudioComponent = NewObject(WorldSettings); if (!ensure(AudioComponent)) { UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager can't create an audio component"), *GetName(), *FString(__FUNCTION__)); return -1; @@ -73,7 +165,7 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) AudioComponent->SetWorldLocation(NewSoundConfig.WorldLocation); if (NewSoundConfig.ResolvedAttachComponent) { - FAttachmentTransformRules Rules( + const FAttachmentTransformRules Rules( NewSoundConfig.AttachmentRules, NewSoundConfig.AttachmentRules, NewSoundConfig.AttachmentRules, @@ -89,7 +181,7 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) AudioComponent->SetVolumeMultiplier(NewSoundConfig.VolumeMultiplier); AudioComponent->SetPitchMultiplier(NewSoundConfig.PitchMultiplier); - bool bAssetIsLooping = NewSoundConfig.Sound->IsLooping(); + const bool bAssetIsLooping = NewSoundConfig.Sound->IsLooping(); AudioComponent->bAutoDestroy = bAssetIsLooping ? false : NewSoundConfig.bAutoDestroy; @@ -99,14 +191,14 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) UpdateStackSize(); } - int32 NewSoundId = IdStack.Pop(); + const int32 NewSoundId = IdStack.Pop(); ActiveSounds.Add(NewSoundId, AudioComponent); AudioComponent->OnAudioFinishedNative.AddUObject(this, &UG2IGameSoundManager::OnSoundFinished, NewSoundId); return NewSoundId; } -bool UG2IGameSoundManager::PlaySound(int32 SoundId, float FadeInTime) +bool UG2IGameSoundManager::PlaySound(const int32 SoundId, const float FadeInTime) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { @@ -119,7 +211,7 @@ bool UG2IGameSoundManager::PlaySound(int32 SoundId, float FadeInTime) return true; } -bool UG2IGameSoundManager::StopSound(int32 SoundId, float FadeOutTime) +bool UG2IGameSoundManager::StopSound(const int32 SoundId, const float FadeOutTime) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { @@ -143,7 +235,7 @@ void UG2IGameSoundManager::StopAllSounds() } } -bool UG2IGameSoundManager::SetSoundVolume(int32 SoundId, float NewVolume) +bool UG2IGameSoundManager::SetSoundVolume(const int32 SoundId, float NewVolume) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { @@ -157,7 +249,7 @@ bool UG2IGameSoundManager::SetSoundVolume(int32 SoundId, float NewVolume) return true; } -bool UG2IGameSoundManager::SetSoundPitch(int32 SoundId, float NewPitch) +bool UG2IGameSoundManager::SetSoundPitch(const int32 SoundId, float NewPitch) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { @@ -165,13 +257,15 @@ bool UG2IGameSoundManager::SetSoundPitch(int32 SoundId, float NewPitch) return false; } - NewPitch = FMath::Clamp(NewPitch, 0.0f, 2.0f); + if (NewPitch <= 0.0f) { + NewPitch = 0.0f; + } Component->SetPitchMultiplier(NewPitch); return true; } -bool UG2IGameSoundManager::SetSoundLocation(int32 SoundId, FVector NewLocation) +bool UG2IGameSoundManager::SetSoundLocation(const int32 SoundId, const FVector& NewLocation) { UAudioComponent* Component = GetAudioById(SoundId); if (!ensure(Component)) { @@ -183,21 +277,22 @@ bool UG2IGameSoundManager::SetSoundLocation(int32 SoundId, FVector NewLocation) return true; } -bool UG2IGameSoundManager::SetSoundAttachment(int32 SoundId, +bool UG2IGameSoundManager::SetSoundAttachment(const int32 SoundId, USceneComponent* NewAttachmentComponent, - EAttachmentRule AttachmentRules) + const EAttachmentRule AttachmentRules) { if (!ensure(NewAttachmentComponent)) { UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager got wrong component to attach"), *GetName(), *FString(__FUNCTION__)); return false; } - TObjectPtr Component = GetAudioById(SoundId); + + const TObjectPtr Component = GetAudioById(SoundId); if (!ensure(Component)) { UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager can't get an audio component for ID: %d"), *GetName(), *FString(__FUNCTION__), SoundId); return false; } - FAttachmentTransformRules Rules( + const FAttachmentTransformRules Rules( AttachmentRules, AttachmentRules, AttachmentRules, @@ -208,25 +303,27 @@ bool UG2IGameSoundManager::SetSoundAttachment(int32 SoundId, return true; } -bool UG2IGameSoundManager::SetSoundAttachment(int32 SoundId, +bool UG2IGameSoundManager::SetSoundAttachment(const int32 SoundId, AActor* NewAttachmentActor, - EAttachmentRule AttachmentRules) + const EAttachmentRule AttachmentRules) { if (!ensure(NewAttachmentActor)) { UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager got wrong actor to attach"), *GetName(), *FString(__FUNCTION__)); return false; } + TObjectPtr RootComponent = NewAttachmentActor->GetRootComponent(); if (!ensure(RootComponent)) { UE_LOG(LogG2I, Warning, TEXT("[%s][%s]: The sound manager can't get root component from actor to attach"), *GetName(), *FString(__FUNCTION__)); return false; } - TObjectPtr Component = GetAudioById(SoundId); + + const TObjectPtr Component = GetAudioById(SoundId); if (!ensure(Component)) { return false; } - FAttachmentTransformRules Rules( + const FAttachmentTransformRules Rules( AttachmentRules, AttachmentRules, AttachmentRules, @@ -238,7 +335,7 @@ bool UG2IGameSoundManager::SetSoundAttachment(int32 SoundId, } -bool UG2IGameSoundManager::RemoveSound(int32 SoundId) +bool UG2IGameSoundManager::RemoveSound(const int32 SoundId) { TObjectPtr Component = GetAudioById(SoundId); if (!Component) { @@ -248,7 +345,6 @@ bool UG2IGameSoundManager::RemoveSound(int32 SoundId) Component->OnAudioFinishedNative.RemoveAll(this); Component->Stop(); - Component->DestroyComponent(); ActiveSounds.Remove(SoundId); IdStack.Push(SoundId); @@ -258,20 +354,23 @@ bool UG2IGameSoundManager::RemoveSound(int32 SoundId) void UG2IGameSoundManager::RemoveAllSounds() { - for (auto Pair = ActiveSounds.CreateIterator(); Pair; ++Pair) + for (auto Iterator = ActiveSounds.CreateIterator(); Iterator; ++Iterator) { - if (ensure(Pair.Value())) + if (Iterator.Value()) { - Pair.Value()->OnAudioFinishedNative.RemoveAll(this); + Iterator.Value()->OnAudioFinishedNative.RemoveAll(this); - Pair.Value()->Stop(); - Pair.Value()->DestroyComponent(); + Iterator.Value()->Stop(); + Iterator.RemoveCurrent(); } } + ActiveSounds.Empty(); IdStack.Empty(); - CurrentNumberAvailable = STARTSIZE; - UpdateStackSize(); + CurrentNumberAvailable = STARTSOUNDSTACKSIZE; + for (int32 i = 0; i < CurrentNumberAvailable; ++i) { + IdStack.Push(i); + } } void UG2IGameSoundManager::UpdateStackSize() { @@ -281,16 +380,21 @@ void UG2IGameSoundManager::UpdateStackSize() { CurrentNumberAvailable *= 2; } -void UG2IGameSoundManager::InitGlobalAudio(USoundMix* _MainMix, TMap _SoundClasses) +void UG2IGameSoundManager::InitGlobalAudio(USoundMix* _MainMix, const TMap& _SoundClasses) { - GlobalSoundMultipliers.Empty(); MainSoundMix = _MainMix; - SoundClasses = _SoundClasses; + + SoundClasses.Empty(); + for (auto& Iterator : _SoundClasses) + { + SoundClasses.Add(Iterator.Key, Iterator.Value); + } - GlobalSoundMultipliers.Add(EG2IASoundType::SpeechSound, 1.0f); - GlobalSoundMultipliers.Add(EG2IASoundType::MusicSound, 1.0f); - GlobalSoundMultipliers.Add(EG2IASoundType::EffectSound, 1.0f); - GlobalSoundMultipliers.Add(EG2IASoundType::DefaultSound, 1.0f); + GlobalSoundMultipliers.Empty(); + GlobalSoundMultipliers.Add(EG2ISoundType::SpeechSound, 1.0f); + GlobalSoundMultipliers.Add(EG2ISoundType::MusicSound, 1.0f); + GlobalSoundMultipliers.Add(EG2ISoundType::EffectSound, 1.0f); + GlobalSoundMultipliers.Add(EG2ISoundType::DefaultSound, 1.0f); UWorld* World = GetWorld(); if (MainSoundMix && World) @@ -302,7 +406,7 @@ void UG2IGameSoundManager::InitGlobalAudio(USoundMix* _MainMix, TMapVolumeMultiplier; } @@ -373,9 +477,9 @@ float UG2IGameSoundManager::GetSoundVolume(int32 SoundId) return 0.0f; } -float UG2IGameSoundManager::GetSoundPitch(int32 SoundId) +float UG2IGameSoundManager::GetSoundPitch(const int32 SoundId) const { - if (UAudioComponent* Component = GetAudioById(SoundId)) + if (const UAudioComponent* Component = GetAudioById(SoundId)) { return Component->PitchMultiplier; } @@ -384,9 +488,9 @@ float UG2IGameSoundManager::GetSoundPitch(int32 SoundId) return 1.0f; } -FVector UG2IGameSoundManager::GetSoundLocation(int32 SoundId) +FVector UG2IGameSoundManager::GetSoundLocation(const int32 SoundId) const { - if (UAudioComponent* Component = GetAudioById(SoundId)) + if (const UAudioComponent* Component = GetAudioById(SoundId)) { return Component->GetComponentLocation(); } @@ -395,9 +499,9 @@ FVector UG2IGameSoundManager::GetSoundLocation(int32 SoundId) return FVector::ZeroVector; } -USceneComponent* UG2IGameSoundManager::GetSoundAttachment(int32 SoundId) +TObjectPtr UG2IGameSoundManager::GetSoundAttachment(const int32 SoundId) const { - if (UAudioComponent* Component = GetAudioById(SoundId)) + if (const UAudioComponent* Component = GetAudioById(SoundId)) { return Component->GetAttachParent(); } @@ -406,7 +510,7 @@ USceneComponent* UG2IGameSoundManager::GetSoundAttachment(int32 SoundId) return nullptr; } -bool UG2IGameSoundManager::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy) +bool UG2IGameSoundManager::SetSoundAutoDestroy(const int32 SoundId, const bool bNewAutoDestroy) { UAudioComponent* Component = GetAudioById(SoundId); if (!Component) @@ -419,9 +523,9 @@ bool UG2IGameSoundManager::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestr return true; } -bool UG2IGameSoundManager::GetSoundAutoDestroy(int32 SoundId) +bool UG2IGameSoundManager::GetSoundAutoDestroy(const int32 SoundId) const { - if (UAudioComponent* Component = GetAudioById(SoundId)) + if (const UAudioComponent* Component = GetAudioById(SoundId)) { return Component->bAutoDestroy; } @@ -430,9 +534,9 @@ bool UG2IGameSoundManager::GetSoundAutoDestroy(int32 SoundId) return false; } -bool UG2IGameSoundManager::GetSoundPlayingOneTime(int32 SoundId) +bool UG2IGameSoundManager::GetSoundPlayingOneTime(const int32 SoundId) const { - if (UAudioComponent* Component = GetAudioById(SoundId)) + if (const UAudioComponent* Component = GetAudioById(SoundId)) { return Component->ComponentHasTag(OneTimeTag); } @@ -441,7 +545,7 @@ bool UG2IGameSoundManager::GetSoundPlayingOneTime(int32 SoundId) return false; } -bool UG2IGameSoundManager::SetSoundPlayingOneTime(int32 SoundId, bool bNewIsPlayingOneTime) +bool UG2IGameSoundManager::SetSoundPlayingOneTime(const int32 SoundId, const bool bNewIsPlayingOneTime) { UAudioComponent* Component = GetAudioById(SoundId); if (!Component) @@ -453,11 +557,11 @@ bool UG2IGameSoundManager::SetSoundPlayingOneTime(int32 SoundId, bool bNewIsPlay if (bNewIsPlayingOneTime) { Component->ComponentTags.AddUnique(OneTimeTag); - Component->bAutoDestroy = true; } else { Component->ComponentTags.Remove(OneTimeTag); + } return true; diff --git a/Source/G2I/Public/Components/G2ISoundComponent.h b/Source/G2I/Public/Components/G2ISoundComponent.h index dcc841e..e78bf15 100644 --- a/Source/G2I/Public/Components/G2ISoundComponent.h +++ b/Source/G2I/Public/Components/G2ISoundComponent.h @@ -10,6 +10,7 @@ class G2I_API UG2ISoundComponent : public USceneComponent GENERATED_BODY() private: + UPROPERTY() TWeakObjectPtr SoundManager; protected: virtual void BeginPlay() override; @@ -49,12 +50,12 @@ class G2I_API UG2ISoundComponent : public USceneComponent UFUNCTION(BlueprintCallable, Category = "Sounds") bool SetSoundAttachmentComponent(int32 SoundId, - USceneComponent* NewAttachementComponent, + USceneComponent* NewAttachemntComponent, EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); UFUNCTION(BlueprintCallable, Category = "Sounds") bool SetSoundAttachmentActor(int32 SoundId, - AActor* NewAttachementActor, + AActor* NewAttachemntActor, EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); UFUNCTION(BlueprintCallable, Category = "Sounds") diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Game/G2IGameSoundManager.h index 64eb763..75fbb87 100644 --- a/Source/G2I/Public/Game/G2IGameSoundManager.h +++ b/Source/G2I/Public/Game/G2IGameSoundManager.h @@ -1,32 +1,35 @@ #pragma once #include "CoreMinimal.h" -#include "Subsystems/WorldSubsystem.h" +#include "Subsystems/GameInstanceSubsystem.h" #include "Engine/EngineTypes.h" -#include "Components/AudioComponent.h" -#include "Components/SceneComponent.h" -#include "Sound/SoundBase.h" -#include "Sound/SoundClass.h" -#include "Sound/SoundMix.h" #include "G2IGameSoundManager.generated.h" +class USoundBase; +class USoundClass; +class USoundMix; +class USceneComponent; +class UAudioComponent; +class UG2IGameInstance; + USTRUCT(BlueprintType) struct FSoundConfig { GENERATED_BODY() + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Sound") TObjectPtr Sound; - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Location") + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Location", meta = (EditCondition = "!bIs2D", EditConditionHides)) FVector WorldLocation = FVector::ZeroVector; - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment") + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment", meta = (EditCondition = "!bIs2D", EditConditionHides)) FComponentReference AttachToComponent; - UPROPERTY() - USceneComponent* ResolvedAttachComponent = nullptr; + UPROPERTY(VisibleAnywhere) + TObjectPtr ResolvedAttachComponent = nullptr; - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment") + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Attachment", meta = (EditCondition = "!bIs2D", EditConditionHides)) EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound|Play", meta = (ClampMin = "0.0", ClampMax = "1.0")) @@ -46,7 +49,7 @@ struct FSoundConfig }; UENUM(BlueprintType) -enum class EG2IASoundType: uint8 +enum class EG2ISoundType: uint8 { SpeechSound UMETA(DisplayName = "Sound for dialogs"), MusicSound UMETA(DisplayName = "Sound for music"), @@ -55,11 +58,15 @@ enum class EG2IASoundType: uint8 }; UCLASS() -class G2I_API UG2IGameSoundManager : public UWorldSubsystem +class G2I_API UG2IGameSoundManager : public UGameInstanceSubsystem { GENERATED_BODY() private: + UPROPERTY() + TObjectPtr GameInstance; + + FDelegateHandle StartGameDelegateHandle; UPROPERTY() TArray IdStack; @@ -71,60 +78,63 @@ class G2I_API UG2IGameSoundManager : public UWorldSubsystem void UpdateStackSize(); UPROPERTY() - TMap GlobalSoundMultipliers; + TMap GlobalSoundMultipliers; UPROPERTY() - USoundMix* MainSoundMix; + TObjectPtr MainSoundMix; UPROPERTY() - TMap SoundClasses; + TMap> SoundClasses; protected: - - UAudioComponent* GetAudioById(int32 SoundId); + void InitializeInStartGame(); + void InitializeInStartLevel(); + void CloseLevelSound(); + + TObjectPtr GetAudioById(const int32 SoundId) const; - void OnSoundFinished(UAudioComponent* AudioComp, int32 SoundId); + void OnSoundFinished(UAudioComponent* AudioComp, const int32 SoundId); public: UFUNCTION(BlueprintPure, Category = "Sound Manager", meta = (WorldContext = "WorldContextObject")) - static UG2IGameSoundManager* Get(UObject* WorldContextObject); + static UG2IGameSoundManager* Get(const UObject* WorldContextObject); virtual void Initialize(FSubsystemCollectionBase& Collection) override; virtual void Deinitialize() override; int32 AddSound(const FSoundConfig& NewSoundConfig); - bool RemoveSound(int32 SoundId); + bool RemoveSound(const int32 SoundId); void RemoveAllSounds(); - bool PlaySound(int32 SoundId, float FadeInTime = 0.0f); - bool StopSound(int32 SoundId, float FadeOutTime = 0.0f); + bool PlaySound(const int32 SoundId, const float FadeInTime = 0.0f); + bool StopSound(const int32 SoundId, const float FadeOutTime = 0.0f); void StopAllSounds(); - bool SetSoundVolume(int32 SoundId, float NewVolume); - bool SetSoundPitch(int32 SoundId, float NewPitch); - bool SetSoundLocation(int32 SoundId, FVector NewLocation); - bool SetSoundAttachment(int32 SoundId, - USceneComponent* NewAttachementComponent, - EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); - bool SetSoundAttachment(int32 SoundId, - AActor* NewAttachementActor, - EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); - bool SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy); - bool SetSoundPlayingOneTime(int32 SoundId, bool bNewIsPlayingOneTime); - - float GetSoundVolume(int32 SoundId); - float GetSoundPitch(int32 SoundId); - FVector GetSoundLocation(int32 SoundId); - USceneComponent* GetSoundAttachment(int32 SoundId); - bool GetSoundAutoDestroy(int32 SoundId); - bool GetSoundPlayingOneTime(int32 SoundId); + bool SetSoundVolume(const int32 SoundId, const float NewVolume); + bool SetSoundPitch(const int32 SoundId, const float NewPitch); + bool SetSoundLocation(const int32 SoundId, const FVector& NewLocation); + bool SetSoundAttachment(const int32 SoundId, + USceneComponent* NewAttachemntComponent, + const EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); + bool SetSoundAttachment(const int32 SoundId, + AActor* NewAttachemntActor, + const EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); + bool SetSoundAutoDestroy(const int32 SoundId, const bool bNewAutoDestroy); + bool SetSoundPlayingOneTime(const int32 SoundId, const bool bNewIsPlayingOneTime); + + float GetSoundVolume(const int32 SoundId) const; + float GetSoundPitch(const int32 SoundId) const; + FVector GetSoundLocation(const int32 SoundId) const; + TObjectPtr GetSoundAttachment(const int32 SoundId) const; + bool GetSoundAutoDestroy(const int32 SoundId) const; + bool GetSoundPlayingOneTime(const int32 SoundId) const; UFUNCTION(BlueprintCallable, Category = "Sound|Global") - void InitGlobalAudio(USoundMix* _MainMix, TMap _SoundClasses); + void InitGlobalAudio(USoundMix* _MainMix, const TMap& _SoundClasses); UFUNCTION(BlueprintCallable, Category = "Sound|Global") - void SetGlobalVolume(EG2IASoundType SoundType, float NewVolume); + void SetGlobalVolume(const EG2ISoundType SoundType, const float NewVolume); UFUNCTION(BlueprintPure, Category = "Sound|Global") - float GetGlobalVolume(EG2IASoundType SoundType) const; + float GetGlobalVolume(const EG2ISoundType SoundType) const; }; From c91db0780cbf025d718fef7b47bcd235b459025b Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Sat, 4 Apr 2026 00:53:18 +0300 Subject: [PATCH 20/22] Added constness in sound comp --- .../Private/Components/G2ISoundComponent.cpp | 30 +++++++++--------- .../G2I/Public/Components/G2ISoundComponent.h | 31 +++++++++---------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/Source/G2I/Private/Components/G2ISoundComponent.cpp b/Source/G2I/Private/Components/G2ISoundComponent.cpp index 9260689..e821995 100644 --- a/Source/G2I/Private/Components/G2ISoundComponent.cpp +++ b/Source/G2I/Private/Components/G2ISoundComponent.cpp @@ -43,7 +43,7 @@ int32 UG2ISoundComponent::AddSound(const FSoundConfig& NewSoundConfig) return SoundManager->AddSound(ConfigToSend); } -bool UG2ISoundComponent::PlaySound(int32 SoundId) +bool UG2ISoundComponent::PlaySound(const int32 SoundId) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -52,7 +52,7 @@ bool UG2ISoundComponent::PlaySound(int32 SoundId) return SoundManager->PlaySound(SoundId); } -bool UG2ISoundComponent::StopSound(int32 SoundId, float FadeOutTime) +bool UG2ISoundComponent::StopSound(const int32 SoundId, const float FadeOutTime) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -70,7 +70,7 @@ void UG2ISoundComponent::StopAllSounds() SoundManager->StopAllSounds(); } -bool UG2ISoundComponent::SetSoundVolume(int32 SoundId, float NewVolume) +bool UG2ISoundComponent::SetSoundVolume(const int32 SoundId, const float NewVolume) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -79,7 +79,7 @@ bool UG2ISoundComponent::SetSoundVolume(int32 SoundId, float NewVolume) return SoundManager->SetSoundVolume(SoundId, NewVolume); } -bool UG2ISoundComponent::SetSoundPitch(int32 SoundId, float NewPitch) +bool UG2ISoundComponent::SetSoundPitch(const int32 SoundId, const float NewPitch) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -88,7 +88,7 @@ bool UG2ISoundComponent::SetSoundPitch(int32 SoundId, float NewPitch) return SoundManager->SetSoundPitch(SoundId, NewPitch); } -bool UG2ISoundComponent::SetSoundLocation(int32 SoundId, FVector NewLocation) +bool UG2ISoundComponent::SetSoundLocation(const int32 SoundId, const FVector& NewLocation) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -97,9 +97,9 @@ bool UG2ISoundComponent::SetSoundLocation(int32 SoundId, FVector NewLocation) return SoundManager->SetSoundLocation(SoundId, NewLocation); } -bool UG2ISoundComponent::SetSoundAttachmentComponent(int32 SoundId, +bool UG2ISoundComponent::SetSoundAttachmentComponent(const int32 SoundId, USceneComponent* NewAttachmentComponent, - EAttachmentRule AttachmentRules) + const EAttachmentRule AttachmentRules) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -109,9 +109,9 @@ bool UG2ISoundComponent::SetSoundAttachmentComponent(int32 SoundId, return SoundManager->SetSoundAttachment(SoundId, NewAttachmentComponent, AttachmentRules); } -bool UG2ISoundComponent::SetSoundAttachmentActor(int32 SoundId, +bool UG2ISoundComponent::SetSoundAttachmentActor(const int32 SoundId, AActor* NewAttachmentActor, - EAttachmentRule AttachmentRules) + const EAttachmentRule AttachmentRules) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -121,7 +121,7 @@ bool UG2ISoundComponent::SetSoundAttachmentActor(int32 SoundId, return SoundManager->SetSoundAttachment(SoundId, NewAttachmentActor, AttachmentRules); } -bool UG2ISoundComponent::RemoveSound(int32 SoundId) +bool UG2ISoundComponent::RemoveSound(const int32 SoundId) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -130,7 +130,7 @@ bool UG2ISoundComponent::RemoveSound(int32 SoundId) return SoundManager->RemoveSound(SoundId); } -bool UG2ISoundComponent::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy) +bool UG2ISoundComponent::SetSoundAutoDestroy(const int32 SoundId, const bool bNewAutoDestroy) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -139,7 +139,7 @@ bool UG2ISoundComponent::SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy return SoundManager->SetSoundAutoDestroy(SoundId, bNewAutoDestroy); } -float UG2ISoundComponent::GetSoundVolume(int32 SoundId) +float UG2ISoundComponent::GetSoundVolume(const int32 SoundId) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -148,7 +148,7 @@ float UG2ISoundComponent::GetSoundVolume(int32 SoundId) return SoundManager->GetSoundVolume(SoundId); } -float UG2ISoundComponent::GetSoundPitch(int32 SoundId) +float UG2ISoundComponent::GetSoundPitch(const int32 SoundId) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -157,7 +157,7 @@ float UG2ISoundComponent::GetSoundPitch(int32 SoundId) return SoundManager->GetSoundPitch(SoundId); } -FVector UG2ISoundComponent::GetSoundLocation(int32 SoundId) +FVector UG2ISoundComponent::GetSoundLocation(const int32 SoundId) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); @@ -166,7 +166,7 @@ FVector UG2ISoundComponent::GetSoundLocation(int32 SoundId) return SoundManager->GetSoundLocation(SoundId); } -bool UG2ISoundComponent::GetSoundAutoDestroy(int32 SoundId) +bool UG2ISoundComponent::GetSoundAutoDestroy(const int32 SoundId) { if (!SoundManager.IsValid()) { UE_LOG(LogG2I, Error, TEXT("[%s][%s]: Couldn't get the Sound Manager"), *GetName(), *FString(__FUNCTION__)); diff --git a/Source/G2I/Public/Components/G2ISoundComponent.h b/Source/G2I/Public/Components/G2ISoundComponent.h index e78bf15..74394a6 100644 --- a/Source/G2I/Public/Components/G2ISoundComponent.h +++ b/Source/G2I/Public/Components/G2ISoundComponent.h @@ -19,20 +19,19 @@ class G2I_API UG2ISoundComponent : public USceneComponent UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Sounds") TMap SetupSounds; - UFUNCTION(BlueprintCallable, Category = "Sounds") int32 AddSound(const FSoundConfig& NewSoundConfig); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool RemoveSound(int32 SoundId); + bool RemoveSound(const int32 SoundId); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool PlaySound(int32 SoundId); + bool PlaySound(const int32 SoundId); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool StopSound(int32 SoundId, float FadeOutTime = 0.0f); + bool StopSound(const int32 SoundId, const float FadeOutTime = 0.0f); UFUNCTION(BlueprintCallable, Category = "Sounds") void StopAllSounds(); @@ -40,39 +39,39 @@ class G2I_API UG2ISoundComponent : public USceneComponent UFUNCTION(BlueprintCallable, Category = "Sounds") - bool SetSoundVolume(int32 SoundId, float NewVolume); + bool SetSoundVolume(const int32 SoundId, const float NewVolume); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool SetSoundPitch(int32 SoundId, float NewPitch); + bool SetSoundPitch(const int32 SoundId, const float NewPitch); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool SetSoundLocation(int32 SoundId, FVector NewLocation); + bool SetSoundLocation(const int32 SoundId, const FVector& NewLocation); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool SetSoundAttachmentComponent(int32 SoundId, + bool SetSoundAttachmentComponent(const int32 SoundId, USceneComponent* NewAttachemntComponent, - EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); + const EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool SetSoundAttachmentActor(int32 SoundId, + bool SetSoundAttachmentActor(const int32 SoundId, AActor* NewAttachemntActor, - EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); + const EAttachmentRule AttachmentRules = EAttachmentRule::SnapToTarget); UFUNCTION(BlueprintCallable, Category = "Sounds") - bool SetSoundAutoDestroy(int32 SoundId, bool bNewAutoDestroy); + bool SetSoundAutoDestroy(const int32 SoundId, const bool bNewAutoDestroy); UFUNCTION(BlueprintPure, Category = "Sounds") - float GetSoundVolume(int32 SoundId); + float GetSoundVolume(const int32 SoundId); UFUNCTION(BlueprintPure, Category = "Sounds") - float GetSoundPitch(int32 SoundId); + float GetSoundPitch(const int32 SoundId); UFUNCTION(BlueprintPure, Category = "Sounds") - FVector GetSoundLocation(int32 SoundId); + FVector GetSoundLocation(const int32 SoundId); UFUNCTION(BlueprintPure, Category = "Sounds") - bool GetSoundAutoDestroy(int32 SoundId); + bool GetSoundAutoDestroy(const int32 SoundId); }; From a26ab1bc9fb71eff20fed6c7d4f77220c2509345 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Sun, 5 Apr 2026 23:38:01 +0300 Subject: [PATCH 21/22] Moved sounds to new folder --- Source/G2I/Private/{Game => Sound}/G2IGameSoundManager.cpp | 0 Source/G2I/Private/{Components => Sound}/G2ISoundComponent.cpp | 0 Source/G2I/Public/{Game => Sound}/G2IGameSoundManager.h | 0 Source/G2I/Public/{Components => Sound}/G2ISoundComponent.h | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename Source/G2I/Private/{Game => Sound}/G2IGameSoundManager.cpp (100%) rename Source/G2I/Private/{Components => Sound}/G2ISoundComponent.cpp (100%) rename Source/G2I/Public/{Game => Sound}/G2IGameSoundManager.h (100%) rename Source/G2I/Public/{Components => Sound}/G2ISoundComponent.h (100%) diff --git a/Source/G2I/Private/Game/G2IGameSoundManager.cpp b/Source/G2I/Private/Sound/G2IGameSoundManager.cpp similarity index 100% rename from Source/G2I/Private/Game/G2IGameSoundManager.cpp rename to Source/G2I/Private/Sound/G2IGameSoundManager.cpp diff --git a/Source/G2I/Private/Components/G2ISoundComponent.cpp b/Source/G2I/Private/Sound/G2ISoundComponent.cpp similarity index 100% rename from Source/G2I/Private/Components/G2ISoundComponent.cpp rename to Source/G2I/Private/Sound/G2ISoundComponent.cpp diff --git a/Source/G2I/Public/Game/G2IGameSoundManager.h b/Source/G2I/Public/Sound/G2IGameSoundManager.h similarity index 100% rename from Source/G2I/Public/Game/G2IGameSoundManager.h rename to Source/G2I/Public/Sound/G2IGameSoundManager.h diff --git a/Source/G2I/Public/Components/G2ISoundComponent.h b/Source/G2I/Public/Sound/G2ISoundComponent.h similarity index 100% rename from Source/G2I/Public/Components/G2ISoundComponent.h rename to Source/G2I/Public/Sound/G2ISoundComponent.h From ddad49e6fa4047baa23e26f837f9c2f4fc48d6a8 Mon Sep 17 00:00:00 2001 From: Keendaj <167191489+Keendaj@users.noreply.github.com> Date: Mon, 6 Apr 2026 12:17:55 +0300 Subject: [PATCH 22/22] Fixed includes and added registration to components --- Source/G2I/Private/Sound/G2IGameSoundManager.cpp | 16 ++++++++++++---- Source/G2I/Private/Sound/G2ISoundComponent.cpp | 2 +- Source/G2I/Public/Sound/G2ISoundComponent.h | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Source/G2I/Private/Sound/G2IGameSoundManager.cpp b/Source/G2I/Private/Sound/G2IGameSoundManager.cpp index 875246c..44a9bea 100644 --- a/Source/G2I/Private/Sound/G2IGameSoundManager.cpp +++ b/Source/G2I/Private/Sound/G2IGameSoundManager.cpp @@ -1,4 +1,4 @@ -#include "Game/G2IGameSoundManager.h" +#include "Sound/G2IGameSoundManager.h" #define STARTSOUNDSTACKSIZE 20 #include "G2I.h" #include "Components/AudioComponent.h" @@ -158,6 +158,8 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) return -1; } + AudioComponent->RegisterComponent(); + AudioComponent->SetSound(NewSoundConfig.Sound.Get()); if (!NewSoundConfig.bIs2D) @@ -170,6 +172,9 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) NewSoundConfig.AttachmentRules, NewSoundConfig.AttachmentRules, false); + if (!NewSoundConfig.ResolvedAttachComponent->IsRegistered()) { + NewSoundConfig.ResolvedAttachComponent->RegisterComponent(); + } AudioComponent->AttachToComponent(NewSoundConfig.ResolvedAttachComponent, Rules); } } @@ -185,8 +190,6 @@ int32 UG2IGameSoundManager::AddSound(const FSoundConfig& NewSoundConfig) AudioComponent->bAutoDestroy = bAssetIsLooping ? false : NewSoundConfig.bAutoDestroy; - AudioComponent->RegisterComponent(); - if (IdStack.IsEmpty()) { UpdateStackSize(); } @@ -298,6 +301,9 @@ bool UG2IGameSoundManager::SetSoundAttachment(const int32 SoundId, AttachmentRules, false); + if (!NewAttachmentComponent->IsRegistered()) { + NewAttachmentComponent->RegisterComponent(); + } Component->AttachToComponent(NewAttachmentComponent, Rules); return true; @@ -328,7 +334,9 @@ bool UG2IGameSoundManager::SetSoundAttachment(const int32 SoundId, AttachmentRules, AttachmentRules, false); - + if (!RootComponent->IsRegistered()) { + RootComponent->RegisterComponent(); + } Component->AttachToComponent(RootComponent, Rules); return true; diff --git a/Source/G2I/Private/Sound/G2ISoundComponent.cpp b/Source/G2I/Private/Sound/G2ISoundComponent.cpp index e821995..5ab1e29 100644 --- a/Source/G2I/Private/Sound/G2ISoundComponent.cpp +++ b/Source/G2I/Private/Sound/G2ISoundComponent.cpp @@ -1,4 +1,4 @@ -#include "Components/G2ISoundComponent.h" +#include "Sound/G2ISoundComponent.h" #include "G2I.h" void UG2ISoundComponent::BeginPlay() diff --git a/Source/G2I/Public/Sound/G2ISoundComponent.h b/Source/G2I/Public/Sound/G2ISoundComponent.h index 74394a6..ee62910 100644 --- a/Source/G2I/Public/Sound/G2ISoundComponent.h +++ b/Source/G2I/Public/Sound/G2ISoundComponent.h @@ -1,7 +1,7 @@ #pragma once #include "CoreMinimal.h" -#include "Game/G2IGameSoundManager.h" +#include "Sound/G2IGameSoundManager.h" #include "G2ISoundComponent.generated.h" UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )