C++ project crashes when I try to add camera. Why?

Good afternoon.

Recently, I’ve got a strange and annoying situation.
Well, I wanted to create a C++ project from zero. Then, I was making a character class. And tried to add a camera.

FPCharacter.h

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Default")
class UCameraComponent* FPCamera;

FPCharacter.cpp:

FPCamera->CreateDefaultSubobject<UCameraComponent>(TEXT("FP Camera"));
FPCamera->SetupAttachment(GetCapsuleComponent());
FPCamera->bUsePawnControlRotation = true;

Which situation do I have? Well, when that piece of code in C++ file is deleted, Live Coding works ok. But when I add that code, I’m getting a crash of the engine.
The video below is demonstrating that.

So how to deal with it? And how to avoid that absurd moment?
Looking forward to your answer.

P.S.: btw, although I can fix “disappearing C++ classes in Unreal Editor” bug in my project easily, but why isn’t it fixed in UE 5.4?

Instead of FPCamera->CreateDefaultSubobject<UCameraComponent>(TEXT("FP Camera")); use FPCamera = CreateDefaultSubobject<UCameraComponent>(FName("FP Camera"));

FText snuck in by there instead of FName :). Also, don’t use → but = to initialize the object.

Full code:

FPCamera = CreateDefaultSubobject<UCameraComponent>(FName("FP Camera"));
if (FPCamera != nullptr)
{
	FPCamera->SetupAttachment(GetCapsuleComponent());		
	FPCamera->bUsePawnControlRotation = true;
}

I would suggest to use Spring arm, so you can do it like this

	SpringArm = CreateDefaultSubobject<USpringArmComponent>(FName("SpringArm"));
	if (SpringArm != nullptr)
	{
		SpringArm->SetupAttachment(GetCapsuleComponent());
	}

	FPCamera = CreateDefaultSubobject<UCameraComponent>(FName("FP Camera"));
	if (FPCamera != nullptr && SpringArm != nullptr)
	{
		FPCamera->SetupAttachment(SpringArm);		
		FPCamera->bUsePawnControlRotation = true;
	}

Unfortunately, it also didn’t work :frowning:
The same crash.

Anyways, thx for tips.

Live coding does not work with constructors properly. Exit Unreal Engine, build the code and then run the engine again. I tested this code in my project, and it worked. So it must work on yours. I didn’t just type it here :slight_smile:

What is the error you got now? Isn’t it different?

I’m laughing right now because I used the code with Spring Arm (although I wanted First Person View) and it worked correctly, without any crashes! :)))))
But if we look at FP template, we can see the camera without Spring Arm in the character class. Oh God, that’s really idiotic moment… :confused:

Yeah, for FPS, there is no need for SpringArm. Not sure if I understand you corretly, but I assume you resolved the issue? If yes, then awesome :slight_smile:

Solved but it was an eerie solution…
It needs to be reported. I need to write to the UE developers because it is really absurd.
The “disappearing C++ classes” bug isn’t still fixed. And SPRING ARM AS A SOLUTION KEY. Oh God, that’s really irritating moment. I don’t know why. I’m having a scream of my soul right now…

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