How to spawn a weapon sound correctly in different states?

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);
			}
		}
	}
}

blueprint version:

thank you

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
				}
			}
		}
	}
}

hope it helps, cheers!

2 Likes

Thank You sir for the quick and working solution, I appreciate it so much, Thank You very very much :slight_smile:

2 Likes

and according to your blueprint, the exact solution

void AMyWeaponActor::PlayFireSound()
{
	if (IsValid(AccMuzzleObject))
	{
		if (AccMuzzleObject->Datas.IsSuppressor)
		{
			Audio->SetSound(Datas.FireSoundSuppressor);
			if (IsValid(Audio))
			{
				Audio->Play(0.0f);
			}
		}
	}
	else
	{
		if (!IsValid(AccMuzzleObject) && !AccMuzzleObject->Datas.IsSuppressor)
		{
			Audio->SetSound(Datas.FireSound);
			if (IsValid(Audio))
			{
				Audio->Play(0.0f);
			}
		}
	}
}

hope it helps , cheers!

1 Like

Thank You very very much :slight_smile:

1 Like

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?

can you generate the native c++ code of your blueprint and post it here, I will look into it to figure it out how its setup out of the box

void AItemWeapon_C__pf1318635114::bpf__PlayFiringSound__pf()
{
	bool bpfv__CallFunc_IsValid_ReturnValue__pf{};
	int32 __CurrentState = 10;
	do
	{
		switch( __CurrentState )
		{
		case 10:
			{
			}
		case 1:
			{
			}
		case 2:
			{
			}
		case 3:
			{
				bpfv__CallFunc_IsValid_ReturnValue__pf = UKismetSystemLibrary::IsValid(bpv__AccMuzzleObj__pf);
				if (!bpfv__CallFunc_IsValid_ReturnValue__pf)
				{
					__CurrentState = 8;
					break;
				}
			}
		case 4:
			{
			}
		case 5:
			{
				bool  __Local__28 = false;
				if (!((::IsValid(bpv__AccMuzzleObj__pf)) ? (bpv__AccMuzzleObj__pf->bpv__Datas__pf.bpv__isSuppressorx_28_B6448D9447A88D2F897E96A44DCABF84__pfzy) : (__Local__28)))
				{
					__CurrentState = 9;
					break;
				}
			}
		case 6:
			{
				if(::IsValid(bpv__Audio__pf))
				{
					bpv__Audio__pf->UAudioComponent::SetSound(bpv__Datas__pf.bpv__FireSoundSuppressor_52_67F5BE704E08115CA1276EAA79F98E46__pf);
				}
			}
		case 7:
			{
				if(::IsValid(bpv__Audio__pf))
				{
					bpv__Audio__pf->Play(0.000000);
				}
				__CurrentState = -1;
				break;
			}
		case 8:
			{
			}
		case 9:
			{
				if(::IsValid(bpv__Audio__pf))
				{
					bpv__Audio__pf->UAudioComponent::SetSound(bpv__Datas__pf.bpv__FireSound_50_D649E6E84D0C83E321EA6C94AD299FF6__pf);
				}
				__CurrentState = 7;
				break;
			}
		default:
			break;
		}
	} while( __CurrentState != -1 );
}

Thank You Sir very very much

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.

hope it helps, cheers!

1 Like

this works very fine, if I will have problems with it I will post it for sure, Thank You Sir for help in my studies :slight_smile:

1 Like

glad to help

1 Like