Crash when creating a new Blueprint pawn from C++ pawn.

I originally posted this in the blueprint section, but no one answered me there. It might be more of a C++ problem anyway.

I’m creating a new pawn class in C++ that I want to create specific version of with Blueprints. It’s a simple class for moving a ball around. If I place a blueprint class based on this c++ class the editor crashes when I hit play. It appears that something is wrong with the RootComponent, but I’m not sure what. I’ve been trying for a few days to figure out what is wrong with no progress. Any Help appreciated.


Unknown exception - code 00000001 (first/second chance not available)

Fatal error: [File:\BuildFarm\buildmachine_++depot+UE4-Releases+4.7\Engine\Source\Runtime\Engine\Private\PrimitiveComponent.cpp] [Line: 1401]
Default__BallPawn_Normal_C MovedComponent RootComponent not initialized deleteme 0

KERNELBASE + 37901 bytes
UE4Editor_Core!FOutputDeviceWindowsError::Serialize() + 292 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\windows\windowsplatformoutputdevices.cpp:95]
UE4Editor_Core!FMsg::Logf__VA() + 463 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\misc\outputdevice.cpp:526]
UE4Editor_Engine!UPrimitiveComponent::MoveComponent() + 1835 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\primitivecomponent.cpp:1401]
UE4Editor_Engine!UMovementComponent::MoveUpdatedComponent() + 123 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\movementcomponent.cpp:440]
UE4Editor_Engine!UMovementComponent::SafeMoveUpdatedComponent() + 167 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\movementcomponent.cpp:460]
UE4Editor_First_1150!UBallPawnMovementComponent::TickComponent() + 2134 bytes [f:\projects\gamedevelopment\unreal\escapeball\source\first\ballpawnmovementcomponent.cpp:121]
UE4Editor_Engine!UActorComponent::ConditionalTickComponent() + 199 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\actorcomponent.cpp:1042]
UE4Editor_Engine!FActorComponentTickFunction::ExecuteTick() + 1001 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\actorcomponent.cpp:602]
UE4Editor_Engine!FTickTaskSequencer::FTickFunctionTask:oTask() + 214 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private icktaskmanager.cpp:322]
UE4Editor_Engine!TGraphTask<FTickTaskSequencer::FTickFunctionTask>::ExecuteTask() + 445 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\public\async askgraphinterfaces.h:671]
UE4Editor_Core!FTaskThread::ProcessTasks() + 3125 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\async askgraph.cpp:428]
UE4Editor_Core!FTaskThread::ProcessTasksUntilQuit() + 77 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\async askgraph.cpp:271]
UE4Editor_Core!FTaskGraphImplementation::WaitUntilTasksComplete() + 511 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\async askgraph.cpp:984]
UE4Editor_Engine!FTaskGraphInterface::WaitUntilTaskCompletes() + 367 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\public\async askgraphinterfaces.h:188]
UE4Editor_Engine!FTickTaskSequencer::ReleaseTickGroup() + 432 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private icktaskmanager.cpp:187]
UE4Editor_Engine!FTickTaskManager::RunTickGroup() + 1182 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private icktaskmanager.cpp:722]
UE4Editor_Engine!UWorld::RunTickGroup() + 102 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\leveltick.cpp:696]
UE4Editor_Engine!UWorld::Tick() + 2971 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\leveltick.cpp:1114]
UE4Editor_UnrealEd!UEditorEngine::Tick() + 5618 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\editor\unrealed\private\editor.cpp:1329]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() + 22 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\editor\unrealed\private\unrealedengine.cpp:347]
UE4Editor!FEngineLoop::Tick() + 4179 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\launchengineloop.cpp:2257]
UE4Editor!GuardedMain() + 1404 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\launch.cpp:142]
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]


The constructor for the Pawn

ABallPawn::ABallPawn()
{
// 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;

// Our root component will be a sphere that reacts to physics
sphereComponent = CreateDefaultSubobject&lt;USphereComponent&gt;(TEXT("RootComponent"));
RootComponent = sphereComponent;
sphereComponent-&gt;InitSphereRadius(40.0f);
sphereComponent-&gt;SetCollisionProfileName(TEXT("Pawn"));


// Create and position a mesh component so we can see where our sphere is
sphereVisual = CreateDefaultSubobject&lt;UStaticMeshComponent&gt;(TEXT("VisualRepresentation"));
sphereVisual-&gt;AttachTo(RootComponent);

// Create a particle system that we can activate or deactivate

// Use a spring arm to give the camera smooth, natural-feeling motion.
USpringArmComponent* SpringArm = CreateDefaultSubobject&lt;USpringArmComponent&gt;(TEXT("CameraAttachmentArm"));
SpringArm-&gt;AttachTo(RootComponent);
SpringArm-&gt;RelativeRotation = FRotator(-30.f, 0.f, 0.f);
SpringArm-&gt;TargetArmLength = 400.0f;
SpringArm-&gt;bEnableCameraLag = true;
SpringArm-&gt;CameraLagSpeed = 5.0f;


// Create a camera and attach to our spring arm
UCameraComponent* Camera = CreateDefaultSubobject&lt;UCameraComponent&gt;(TEXT("ActualCamera"));
Camera-&gt;AttachTo(SpringArm, USpringArmComponent::SocketName);

// Take control of the default player
AutoPossessPlayer = EAutoReceiveInput::Player0;

// Create an instance of our movement component, and tell it to update the root

ourMovementComponent = CreateDefaultSubobject&lt;UBallPawnMovementComponent&gt;(TEXT("CustomMovementComponent"));
ourMovementComponent-&gt;UpdatedComponent = RootComponent;

}

First thing that stands out to me, is you haven’t given a context object to the CreateDefaultSubobject function. You need to put “this” in the brackets, like so:



SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("VisualRepresentation"));


Also, why are you declaring Variables in the header like this? These should really be in the Header file.



USpringArmComponent* SpringArm = 


I personaly would’ve done all the sub-objects manipulations using ObjectInitializer and initializer lists. Something like that:



ABallPawn::ABallPawn(const class FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer.SetDefaultSubobjectClass<UBallPawnMovementComponent>(ABallPawn::CharacterMovementComponentName))
{
    // ...
}


Oh yeah, and you’re not using Object Initializer, that’s what looked wierd.

Ah okay. I’ll give that a try. As for why I didn’t do it that way. I was following this tutorial Components and Collision | Unreal Engine Documentation and it didn’t mention anything about Object Initializer. I’ll give it a try and let you know if that fixes the issue.

Well… still not working. I didn’t quite understand what your code was doing so I went with Jamsh. This will still error. However I did find a clue. I found the RootComponent is not registered when this class is the parent of a blueprint. So if I do a check UpdatedComponent->IsRegistered() in the movement class I can prevent the crash. But it also prevents the pawn from moving. Any idea why the component would not be registered… but only if its a blueprint parent?

This is what I now have in my constructor.

ABallPawn::ABallPawn(const FObjectInitializer& ObjectInitializer)
{
// 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;

// Set some defaults
targetMoveSpeed = 500.0f;
accelerationGround = 0.35f;
accelerationAir = 0.15f;
gravityForce = 16.0f;
jumpForce = 500.0f;

// Our root component will be a sphere that reacts to physics
sphereComponent = ObjectInitializer.CreateDefaultSubobject&lt;USphereComponent&gt;(this,TEXT("RootComponent"));
RootComponent = sphereComponent;
sphereComponent-&gt;InitSphereRadius(40.0f);
sphereComponent-&gt;SetCollisionProfileName(TEXT("Pawn"));
	
// Create and position a mesh component so we can see where our sphere is
sphereVisual = ObjectInitializer.CreateDefaultSubobject&lt;UStaticMeshComponent&gt;(this,TEXT("VisualRepresentation"));
sphereVisual-&gt;AttachTo(RootComponent);

// Use a spring arm to give the camera smooth, natural-feeling motion.
SpringArm = ObjectInitializer.CreateDefaultSubobject&lt;USpringArmComponent&gt;(this,TEXT("CameraAttachmentArm"));
SpringArm-&gt;AttachTo(RootComponent);
SpringArm-&gt;RelativeRotation = FRotator(-30.f, 0.f, 0.f);
SpringArm-&gt;TargetArmLength = 400.0f;
SpringArm-&gt;bEnableCameraLag = true;
SpringArm-&gt;CameraLagSpeed = 5.0f;

// Create a camera and attach to our spring arm
Camera = ObjectInitializer.CreateDefaultSubobject&lt;UCameraComponent&gt;(this, TEXT("ActualCamera"));
Camera-&gt;AttachTo(SpringArm, USpringArmComponent::SocketName);

// Take control of the default player
AutoPossessPlayer = EAutoReceiveInput::Player0;

// Create an instance of our movement component, and tell it to update the root

ourMovementComponent = ObjectInitializer.CreateDefaultSubobject&lt;UBallPawnMovementComponent&gt;(this, TEXT("CustomMovementComponent"));
ourMovementComponent-&gt;UpdatedComponent = RootComponent;

}

Update. It only happens if I play in new editor window… if I play in standalone game no problem. So I have a workaround at least now. It would be nice to know what is going wrong with the editor window however.