Sorry for a bit clickbait title. The problem for me is in fact Play() not playing sound attached to UAudioComponent. However I’d guess I just do something wrong, but can’t figure out what several days already. Does anyone have an idea?
Here is how the object is declared in my game instance class header:
And then it does play, but that’s rather bad of a workaround since I need to be able to stop the sound.
So once again, does anyone have any idea what am I doing wrong? (If any additional informations are required, just ask please, I will proivide them)
UE_LOG(LogTemp, Log, TEXT("ChangeMusicState call received"));
currentMusicState = newState;
// idleVillage->Activate(); Not sure if you need this
idleVillage->SetSound(Sound);
idleVillage->Play(0.f);
The sound you are trying to play could be optimized away, this happens when the volume is 0 or when you are far away I believe. On the sound asset editor configure it to play virtualized when silent.
I don’t use CreateDefaultSubobject or SetupAttachment there, I simply have an array of structs and each struct holds an UAudioComponent pointer among some other data.
Ok I found out what is wrong. It seems components added to GameInstance are kind of ignored, since they require World Context to exist. I tried attaching AudioComponent to a sub-Character class and call Play and everything works. It seems that Game Instance does not support this, sadly.
I’m using AudioComponent in a GameInstance for persistent music, make sure you are calling UGameplayStatics::CreateSound2D when a level is loaded ( BeginPlay for example ), not in the constructor.
I’m using UGameplayStatics::CreateSound2D in a function called by BeginPlay when the first level is loaded.