Hi, I want to spawn play switch sound when the weapon is in suppressor mode or in without suppressor mode… I can do it in blueprints very fine but how to do it in C++ correctly?
what I am trying is:
void AMyWeaponClass::PlayFireSound()
{
if (Muzzle != nullptr)
{
if (IsValid(AccMuzzleObject ))
{
if (AccMuzzleObject && AccMuzzleObject->Datas.IsSuppressor)
{
Audio->SetSound(Datas.FireSoundSuppressor);
}
if (AccMuzzleObject && !AccMuzzleObject->Datas.IsSuppressor)
{
Audio->SetSound(Datas.FireSound);
}
}
}
}
void AMyWeaponClass::PlayFireSound()
{
if (Muzzle != nullptr)
{
if (IsValid(AccMuzzleObject)) // dont need to double check for nullptr
{
if (AccMuzzleObject->Datas.IsSuppressor)
{
Audio->SetSound(Datas.FireSoundSuppressor);
if (IsValid(Audio))
{
Audio->Play(0.000000); // you are missing to play the sound after setting
}
}
if (AccMuzzleObject || !AccMuzzleObject && !AccMuzzleObject->Datas.IsSuppressor)
{
Audio->SetSound(Datas.FireSound);
if (IsValid(Audio))
{
Audio->Play(0.000000); // you are missing to play the sound after setting
}
}
}
}
}
Sir I have some problems now with the code, the first time it works and the second time when I change weapon and re pickup the silencer it play the normal sound and not the silencer sound.
Please can You look into this?
as you can see the bp is using do /while loop to make this work… but you don’t need this loop for this basic function to make this work, just do some basic checks and assignments to achieve this.
void AWeaponItem::PlayFireSound()
{
if (!IsValid(AccMuzzleObject))
{
if (::IsValid(Audio))
{
Audio->UAudioComponent::SetSound(Datas.FireSound);
Audio->Play(0.f);
}
}else
{
bool Local48 = false;
if (!((::IsValid(AccMuzzleObject)) ? (AccMuzzleObject->Datas.IsSuppressor) : (Local48)))
{
if (::IsValid(Audio))
{
Audio->UAudioComponent::SetSound(Datas.FireSound);
Audio->Play(0.f);
}
}else
{
if (::IsValid(Audio))
{
Audio->UAudioComponent::SetSound(Datas.FireSoundSuppressor);
Audio->Play(0.f);
}
}
}
}
this should work for you. if not , this time write the complete details of the error if possible post the log file.