Protecting Pointer Not Working

Hi,

As a self-imposed Mini chalenge, I wanted to protect my MuzzleFlash pointer in case I forgot to set it in blueprint. So I did the following code, but when I tried to play the game, it would crash straight away, instead of just quitting the code before the pointer is used, and giving a more meaningful error message.

Can anyone see or know why my protection to stop it crashing if I don’t set the particle effect in Blueprint isn’t working?

Here is my cpp code

// Called when the game starts or when spawned
void AGun::BeginPlay()
{
Super::BeginPlay();

CheckForMuzzleFlash();
}

void AGun::CheckForMuzzleFlash() const
{
if (!MuzzleFlash)
{
UE_LOG(LogTemp, Error, TEXT(“No MuzzleFlash particle effect has been attached to the %s.”), *GetOwner()->GetName());
}
}

void AGun::PullTriggerMethod()
{
if (!MuzzleFlash) { return; }
UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, Mesh, TEXT(“MuzzleFlashSocket”));
}

And In my Header file

UPROPERTY(EditAnywhere)
UParticleSystem* MuzzleFlash = nullptr;

Because GetOwner() Is Returning nullptr As No Owner Is set.
You Have To Call SetOwner For This Gun When You Are Creating It.

Judging by the log message, I assume they meant to log the name of the gun i.e. *GetName().

Thank you.

I thought I’d checked that reference, and tried running it without it, but maybe the compile hadn’t compiled properly, and so I thought the crashing was not caused by it.

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

Privacy & Terms