Download

Bool function causes crash.

Hello! I am following a camera lock on example and I am trying to implement it into my existing gameplay programming. But I am struggling with one particular function causing a crash when called.


Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x000002a8

The example version of the code both compiles and runs fine.

This is the problem function in question:


bool UTribesmenSpringArm::IsCameraLockedToTarget()
{
return CameraTarget != nullptr;
}

header:


/* True if the camera is currently locked to a target */
UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Lock On Camera")
bool IsCameraLockedToTarget();

How CameraTarget is found:


UTribesmenCameraTarget* UTribesmenSpringArm::GetLockTarget()
{
TArray<UTribesmenCameraTarget*> AvailableTargets = GetTargets();
if (AvailableTargets.Num() == 0)
return nullptr;

float ClosestDotToCenter = 0.f;
UTribesmenCameraTarget* Target = nullptr;

for (int32 i = 0; i < AvailableTargets.Num(); i++)
{
float Dot = FVector::DotProduct(GetForwardVector(), (AvailableTargets*->GetComponentLocation() - GetComponentLocation()).GetSafeNormal());
if (Dot > ClosestDotToCenter)
{
ClosestDotToCenter = Dot;
Target = AvailableTargets*;
}
}
return Target;
}



void UTribesmenSpringArm::ToggleCameraLock()
{
if (IsCameraLockedToTarget())
{
BreakTargetLock();
return;
}

UTribesmenCameraTarget* NewCameraTarget = GetLockTarget();

if (NewCameraTarget != nullptr)
{
LockToTarget(NewCameraTarget);
}
}

void UTribesmenSpringArm::LockToTarget(UTribesmenCameraTarget* NewTarget)
{
CameraTarget = NewTarget;
}




I’ve double checked, all functions and variables *should *be public, I can’t tell what the issue is.

You’re trying to access a nullptr somewhere. Likely whatever is calling the function is null.

Hmm, perhaps. My springarm is created and referenced in the character class like this:


CameraSpringArm = CreateDefaultSubobject<UTribesmenSpringArm>(TEXT("CameraSpringArm"));
CameraSpringArm->SetupAttachment((USceneComponent*)GetCapsuleComponent());

header:


UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UTribesmenSpringArm* CameraSpringArm;

And the function within the springarm is called from my character class like this:


void ATribesmenCharacter::LookY(float Val)
{
if (!CameraSpringArm->IsCameraLockedToTarget() && bCameraMode)
AddControllerPitchInput(Val);
}



Edit: You’re right! Any references to CameraSpringArm functions crashes the game, I used to spawn an reference it in a different manner, but adjusted it to match the example, and now they do not work.

Any ideas how to fix it?

Try changing the name in the CreateDefaultSubobject call from “CameraSpringArm” to something like “TribesSpringArm”. There may be some conflict behind the scenes if that name already exists on the asset, so it’s trying to do a cast behind the scene that is failing.

Tried that as well, to no avail. But! I think I’ve narrowed down the issue.

In the process of updating my springarm and character, I have made a custom spring arm class, and replaced the


CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));

with


CameraSpringArm = CreateDefaultSubobject<UTribesmenSpringArm>(TEXT("CameraSpringArm"));

My character in-game is a blueprint, not the character class directly. If I use the character class the function works, I can scroll, move around etc and do things that relies on the springarm without any issues. (Except for everything that the blueprint adds).

As soon as I use the blueprint again, it crashes. Could it be that the blueprint is struggling to access the springarm or is still expecting the old class somehow?

Edit: It’s definitely my existing blueprint causing the issue, if I create a new blueprint based on my character class, it works fine and I can access blueprint accessible variables in my spring arm. But I cannot access them in my original blueprint.
How to I “clean up” or “refresh” my blueprint to reflect the new class properly?

Having to remake the blueprint, all its variables and all of its setup each time I change something like this would be rather inconvenient.
Could it have something to do with the new class having a different “meta”?