UWorld or GetWorld() Fatally Crashes tThe Editor C++

Hello, I am attempting to make a custom character class that uses Unreal’s Character as a base. I am trying to do a LineTrace but whenever I use the GetWorld() call, the editor crashes and causes the project to become unrecoverable, forcing me to create a fresh project. This also happens with a UWorld variable declaration with no other calls.

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


#include "CustomCharacter.h"

UWorld world;

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

	camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Main Camera"));

	camera->AttachToComponent(this->RootComponent, FAttachmentTransformRules::KeepWorldTransform);
	camera->bUsePawnControlRotation = true;
	camera->SetRelativeLocation(FVector(0.0f, 0.0f, 85.0f));
	camera->SetRelativeRotation(FRotator(0.0f, 0.0f, 0.0f));
}

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

}

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

// Called to bind functionality to input
void ACustomCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}

void ACustomCharacter::LookAround(float scale)
{
	if (!isInspecting)
	{
		AddControllerYawInput(scale);
	}
}

void ACustomCharacter::LookUp(float scale)
{
	if (!isInspecting)
	{
		AddControllerPitchInput(scale);
	}
}

void ACustomCharacter::MoveCharacter(FVector worldDirection, float scale)
{
	if (!isInspecting)
	{
		AddMovementInput(worldDirection, scale);
	}
}

If I comment the “UWorld world;” at the very top, everything will be okay. But if I build without commenting, the project crashes and I have to start over. Any idea why this happens? It also crashes with the following code:

float LineTraceDistance = 100.f;
FVector Start;
FVector End;
// get the camera view
FVector CameraLoc = FollowCamera->GetComponentLocation();
FRotator CameraRot = FollowCamera->GetComponentRotation();
Start = CameraLoc;
End = CameraLoc + (CameraRot.Vector() * LineTraceDistance);
  
// additional trace parameters
FCollisionQueryParams TraceParams(FName(TEXT("InteractTrace")), true, NULL);
TraceParams.bTraceComplex = true;
TraceParams.bReturnPhysicalMaterial = true;
//Re-initialize hit info
FHitResult HitDetails = FHitResult(ForceInit);
bool bIsHit = GetWorld()->LineTraceSingleByChannel(
  HitDetails,      // FHitResult object that will be populated with hit info
  Start,      // starting position
  End,        // end position
  ECC_GameTraceChannel3,  // collision channel - 3rd custom one
  TraceParams      // additional trace settings
);
if(bIsHit)
{
  // something was hit
}
else
{
  // we missed
}

There is no need to declare that variable. UE manages the creation of worlds for you. By declaring an extra world UE will try to create a default initialised world globally. Most likely this case was not taken into account by Epic so it results in unexpected behaviour.

To get the world you just have to call GetWorld() from an actor, as you are already correctly doing. The are other ways too but this is the most common one.

1 Like

Just to add: this isn’t a pointer to a world, this is actually a new world object!

1 Like