Download

Explanation for "bool" functions?

Hey! I have almost fully understood C++ in unreal now. The only thing that I am still struggling with are bool functions. Can someone tell me what I am returning with this function (and just generally what it does)?



bool AShooterWeapon::CanReload() const
{
	bool bCanReload = (!MyPawn || MyPawn->CanReload());
	bool bGotAmmo = ( CurrentAmmoInClip < WeaponConfig.AmmoPerClip) && (CurrentAmmo - CurrentAmmoInClip > 0 || HasInfiniteClip());
	bool bStateOKToReload = ( ( CurrentState ==  EWeaponState::Idle ) || ( CurrentState == EWeaponState::Firing) );
	return ( ( bCanReload == true ) && ( bGotAmmo == true ) && ( bStateOKToReload == true) );	
}

I am not asking anyone to correct code or anything like that, just trying to learn something new. Thank you in advance!

You’re returning whether bCanReload, bGotAmmoand bStateOKToReload are all true.

However I don’t get why you’re comparing the bools with true. (boolhere == true) always equals boolhere.

Haha this is actually from the Shooter Sample game. So what you are saying is: If all of those are true, only then will this return a “true” value? I think I got it, thanks!

I assume you understand If(), you should understand bool as bool is argument of if() (same as while()), all operators you use in if (like ==, &&, ||) are returning bool