Any particular reason for not checking Gun pointer existence?

Hello,

Just a quick question, is there any reason for not checking for validity(existence) of a AGun* Gun?

in ShooterCharacter.cpp:

void AShooterCharacter::Shoot()
{
		Gun->PullTrigger();
}

Shouldn’t it be as we were taught:

void AShooterCharacter::Shoot()
{
	if (Gun)
	{
		Gun->PullTrigger();
	}
}

With how the game is designed it should be impossible for it to become nullptr. It’s created at BeginPlay before a player could call that function and is never destroyed during play so it should save a branch.

You could add check(Gun); which will alert you if it is ever nullptr during development and is removed in a shipping build.

Thanks for the answer, but why is it never desroyed during the game? I mean, later on we will be adding a death feature to our Character, so the Gun object could be destroyed as well?

It’s only single player so it will be destroyed when the game is. Though even if you implement respawning and having dead actors destroyed after x seconds you wouldn’t be calling through the destroyed gun.

Either the character and gun get destroyed and you detach from the character to a spectator view. And/or you respawn with a brand new gun along with it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms