Code Causes Engine Crash

Hi dear Epic stuff

I was following this tutorial : Quick Start Guide to Player-Controlled Cameras in Unreal Engine CPP | Unreal Engine 5.2 Documentation

After finished with writing code. I tried opening project. But engine crashed when initializing.

Then i created a new blank project. And added the same class and only that class. Made necessery editing. Engine crashed when opening that project also.

VS2013 gives no error or warnings when building the project.

Engine Version 4.7.4, Os : Win 7 SP1, VS2013

It is a bit long, but i tried to give as much info as possible.

This is the crash report(For second blank project)

MachineId:839FFFF94FE0B8C176196598B3B1E235
EpicAccountId:806d36664f024562b2ddef02bfdb062d

Access violation - code c0000005 (first/second chance not available)

UE4Editor_Engine!USceneComponent::AttachTo() + 79 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\scenecomponent.cpp:864]
UE4Editor_MyProject2!APawnWithCamera::APawnWithCamera() + 320 bytes [c:\dosyalar\ue4projects\myproject2\source\myproject2\pawnwithcamera.cpp:16]
UE4Editor_CoreUObject!UClass::CreateDefaultObject() + 435 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\coreuobject\private\uobject\class.cpp:2535]
UE4Editor_CoreUObject!UObjectLoadAllCompiledInDefaultProperties() + 805 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\coreuobject\private\uobject\uobjectbase.cpp:739]
UE4Editor_CoreUObject!ProcessNewlyLoadedUObjects() + 146 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\coreuobject\private\uobject\uobjectbase.cpp:815]
UE4Editor_CoreUObject!TBaseStaticDelegateInstance<void __cdecl(void)>::ExecuteIfSafe() + 7 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\public\delegates\delegateinstancesimpl_variadics.inl:921]
UE4Editor_Core!TBaseMulticastDelegate<void>::Broadcast() + 149 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\public\delegates\delegatesignatureimpl_variadics.inl:1030]
UE4Editor_Core!FModuleManager::LoadModuleWithFailureReason() + 2194 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\modules\modulemanager.cpp:359]
UE4Editor_Projects!FModuleDescriptor::LoadModulesForPhase() + 784 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\projects\private\moduledescriptor.cpp:350]
UE4Editor_Projects!FProjectManager::LoadModulesForProject() + 581 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\projects\private\projectmanager.cpp:53]
UE4Editor!FEngineLoop::LoadStartupModules() + 705 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\launchengineloop.cpp:1863]
UE4Editor!FEngineLoop::PreInit() + 11141 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\launchengineloop.cpp:1400]
UE4Editor!GuardedMain() + 251 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\launch.cpp:110]
UE4Editor!GuardedMainWrapper() + 26 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() + 249 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\windows\launchwindows.cpp:202]
UE4Editor!__tmainCRTStartup() + 329 bytes [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c:618]

This is the code i wrote
First projects header
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Pawn.h"
#include "PawnWithCamera.generated.h"

UCLASS()
class MYPROJECT_API APawnWithCamera : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	APawnWithCamera();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

	USpringArmComponent* OurCameraSpringArm;

	UCameraComponent* OurCamera;

	FVector2D MovementInput;

	FVector2D CameraInput;

	float ZoomFactor;

	bool bZoomIn;
	
	void MoveForward(float AxisValue);

	void MoveRight(float AxisValue);

	void ZoomIn();

	void ZoomOut();

	void CameraPitch(float AxisValue);

	void CameraYaw(float AxisValue);

};

First projects Source
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyProject.h"
#include "PawnWithCamera.h"


// Sets default values
APawnWithCamera::APawnWithCamera()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));

	OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
	OurCamera->AttachTo(RootComponent);
	OurCamera->SetRelativeLocationAndRotation(FVector(0.f, 0.f, 50.f), FRotator(-60.f, 0.f, 0.f));
	OurCameraSpringArm->TargetArmLength = 400.f;
	OurCameraSpringArm->bEnableCameraLag = true;
	OurCameraSpringArm->CameraLagSpeed = 3.f;

	OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
	OurCamera->AttachTo(OurCameraSpringArm);

	AutoPossessPlayer = EAutoReceiveInput::Player0;

}

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

// Called every frame
void APawnWithCamera::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

	if (bZoomIn)
	{
		ZoomFactor += DeltaTime * 0.5f;
	}
	else
	{
		ZoomFactor -= DeltaTime * 0.25f;
	}
	ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.f, 1.f);

	OurCamera->FieldOfView = FMath::Lerp<float>(90.f, 60.f, ZoomFactor);
	OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.f, 300.f, ZoomFactor);

	{
		FRotator NewRotation = GetActorRotation();
		NewRotation.Yaw += CameraInput.X;
		SetActorRotation(NewRotation);
	}

	{
		FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
		NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.f, -15.f);
		OurCameraSpringArm->SetWorldRotation(NewRotation);
	}

	if (!MovementInput.IsZero())
	{
		MovementInput = MovementInput.GetSafeNormal() * 100.f;
		FVector NewLocation = GetActorLocation();
		NewLocation += GetActorForwardVector() * MovementInput.X * DeltaTime;
		NewLocation += GetActorRightVector() * MovementInput.Y * DeltaTime;
		SetActorLocation(NewLocation);
	}

}

// Called to bind functionality to input
void APawnWithCamera::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	Super::SetupPlayerInputComponent(InputComponent);

	InputComponent->BindAction("ZoomIn", IE_Pressed, this, &APawnWithCamera::ZoomIn);
	InputComponent->BindAction("ZoomIn", IE_Released, this, &APawnWithCamera::ZoomOut);

	InputComponent->BindAxis("MoveForward", this, &APawnWithCamera::MoveForward);
	InputComponent->BindAxis("MoveRight", this, &APawnWithCamera::MoveRight);
	InputComponent->BindAxis("CameraPitch", this, &APawnWithCamera::CameraPitch);
	InputComponent->BindAxis("CameraYaw", this, &APawnWithCamera::CameraYaw);
}

void APawnWithCamera::MoveForward(float AxisValue)
{
	MovementInput.X = FMath::Clamp(AxisValue, -1.f, 1.f);
}

void APawnWithCamera::MoveRight(float AxisValue)
{
	MovementInput.Y = FMath::Clamp(AxisValue, -1.f, 1.f);
}

void APawnWithCamera::ZoomIn()
{
	bZoomIn = true;
}

void APawnWithCamera::ZoomOut()
{
	bZoomIn = false;
}

void APawnWithCamera::CameraPitch(float AxisValue)
{
	CameraInput.Y = AxisValue;
}

void APawnWithCamera::CameraYaw(float AxisValue)
{
	CameraInput.X = AxisValue;
}

Header and Source for second blank test project
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Pawn.h"
#include "PawnWithCamera.generated.h"

UCLASS()
class MYPROJECT2_API APawnWithCamera : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	APawnWithCamera();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

	USpringArmComponent* OurCameraSpringArm;

	UCameraComponent* OurCamera;

	FVector2D MovementInput;

	FVector2D CameraInput;

	float ZoomFactor;

	bool bZoomIn;
	
	void MoveForward(float AxisValue);

	void MoveRight(float AxisValue);

	void ZoomIn();

	void ZoomOut();

	void CameraPitch(float AxisValue);

	void CameraYaw(float AxisValue);

};

This is the source for second project
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyProject2.h"
#include "PawnWithCamera.h"


// Sets default values
APawnWithCamera::APawnWithCamera()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));

	OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
	OurCamera->AttachTo(RootComponent);
	OurCamera->SetRelativeLocationAndRotation(FVector(0.f, 0.f, 50.f), FRotator(-60.f, 0.f, 0.f));
	OurCameraSpringArm->TargetArmLength = 400.f;
	OurCameraSpringArm->bEnableCameraLag = true;
	OurCameraSpringArm->CameraLagSpeed = 3.f;

	OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
	OurCamera->AttachTo(OurCameraSpringArm);

	AutoPossessPlayer = EAutoReceiveInput::Player0;

}

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

// Called every frame
void APawnWithCamera::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

	if (bZoomIn)
	{
		ZoomFactor += DeltaTime * 0.5f;
	}
	else
	{
		ZoomFactor -= DeltaTime * 0.25f;
	}
	ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.f, 1.f);

	OurCamera->FieldOfView = FMath::Lerp<float>(90.f, 60.f, ZoomFactor);
	OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.f, 300.f, ZoomFactor);

	{
		FRotator NewRotation = GetActorRotation();
		NewRotation.Yaw += CameraInput.X;
		SetActorRotation(NewRotation);
	}

	{
		FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
		NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.f, -15.f);
		OurCameraSpringArm->SetWorldRotation(NewRotation);
	}

	if (!MovementInput.IsZero())
	{
		MovementInput = MovementInput.GetSafeNormal() * 100.f;
		FVector NewLocation = GetActorLocation();
		NewLocation += GetActorForwardVector() * MovementInput.X * DeltaTime;
		NewLocation += GetActorRightVector() * MovementInput.Y * DeltaTime;
		SetActorLocation(NewLocation);
	}

}

// Called to bind functionality to input
void APawnWithCamera::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	Super::SetupPlayerInputComponent(InputComponent);

	InputComponent->BindAction("ZoomIn", IE_Pressed, this, &APawnWithCamera::ZoomIn);
	InputComponent->BindAction("ZoomIn", IE_Released, this, &APawnWithCamera::ZoomOut);

	InputComponent->BindAxis("MoveForward", this, &APawnWithCamera::MoveForward);
	InputComponent->BindAxis("MoveRight", this, &APawnWithCamera::MoveRight);
	InputComponent->BindAxis("CameraPitch", this, &APawnWithCamera::CameraPitch);
	InputComponent->BindAxis("CameraYaw", this, &APawnWithCamera::CameraYaw);
}

void APawnWithCamera::MoveForward(float AxisValue)
{
	MovementInput.X = FMath::Clamp(AxisValue, -1.f, 1.f);
}

void APawnWithCamera::MoveRight(float AxisValue)
{
	MovementInput.Y = FMath::Clamp(AxisValue, -1.f, 1.f);
}

void APawnWithCamera::ZoomIn()
{
	bZoomIn = true;
}

void APawnWithCamera::ZoomOut()
{
	bZoomIn = false;
}

void APawnWithCamera::CameraPitch(float AxisValue)
{
	CameraInput.Y = AxisValue;
}

void APawnWithCamera::CameraYaw(float AxisValue)
{
	CameraInput.X = AxisValue;
}

You are setting up you camera in the wrong order, create the subobject first before you operate on it:

OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
OurCamera->AttachTo(RootComponent);
OurCamera->SetRelativeLocationAndRotation(FVector(0.f, 0.f, 50.f), FRotator(-60.f, 0.f, 0.f));
OurCameraSpringArm->TargetArmLength = 400.f;
OurCameraSpringArm->bEnableCameraLag = true;
OurCameraSpringArm->CameraLagSpeed = 3.f;

Thanks for the answer

Actual problem is line 14 and 15 should be OurCameraSpringArm not OurCamera though. :slight_smile:

Oh, yes i see :slight_smile: well it was provoking the runtime error nonetheless.

Hey Corpse0327-

At what point in the tutorial did you find that you could no longer open the project? Did the problem only begin after working through the entire tutorial? What edits did you make when trying to copy this into a new project? Are you able to reproduce it by walking through the tutorial again rather than just copying the code over?

Cheers

gormed actually found the problem. (I checked gormed answer as correct actually, don’t know why it is not checked at the moment. Maybe i didn’t and thought i did.)

Basically not a bug.

I didn’t know when a project launches c++ assets also constructed(or whatever process it passed). I thought run when i pressed play button at top dock. So i never expected that the problem was from the code.

I now checked gormed’s answer as correct answer, and checked if i had checked. :slight_smile:

Man, i checked it just now as correct answer. But it is gone again!

What is going on?