Attaching capsule to Root component crashes game

I am having trouble figuring out components in C++. I have an actor that I want to give a static mesh component and a capsule component for overlap checking.

Here’s my constructor:

AShipConsole::AShipConsole(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)  {
	PrimaryActorTick.bCanEverTick = true;

	Beacon = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("beacs"));
	Beacon->SetRelativeScale3D(FVector(2, 2, 0.01));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMesh_Sphere(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));
	Beacon->SetStaticMesh(StaticMesh_Sphere.Object);
	RootComponent = Beacon;

	UCapsuleComponent* ActivationCapsule = ObjectInitializer.CreateDefaultSubobject<UCapsuleComponent>(this, TEXT("dummy"));
	ActivationCapsule->SetRelativeScale3D(FVector(40, 40, 50));	
	/*ActivationCapsule->OnComponentBeginOverlap.AddDynamic(this, &AShipConsole::EnterConsole);
	ActivationCapsule->OnComponentEndOverlap.AddDynamic(this, &AShipConsole::ExitConsole);
	ActivationCapsule->SetMobility(EComponentMobility::Movable);*/
	ActivationCapsule->SetRelativeLocation(FVector(10000, 10000, 10000));
	if (ActivationCapsule != NULL && Beacon != NULL) {
		ActivationCapsule->AttachTo(Beacon);		// This can be changed to ActivationCapsule->AttachParent = Beacon and still crash the game
	}
	//RootComponent->AttachChildren.Add(ActivationCapsule);
}

The attach-row at the end crashes the game no matter if I refer to Beacon or RootComponet or if I use AttachTo or AttachParent. I’m really not sure if I am approaching this the right way, either way, it’s not working.

I dont’t get a proper stacktrace, but removing the row that attaches the capsule to the beacon removes the error. Here’s the stacktrace: http://hastebin.com/uxujohadoq.coffee

The reason I feel like I have to attach the capsule is that otherwise it wont follow the root component properly and will just stand static in the world.

Not sure if there’s anything else I should put here.

Hey Siretu-

Where exactly is this crash occurring for you? Is it when compiling the blueprint based on this class or when trying to play the level with the blueprint in it? Also, which version of the engine are you working in. Does the crash happen if you include the above code in a new project? Additionally if you could provide the log files from the crash it will help show where exactly the crash is occurring.

Cheers

The crash occurs when I try to play the level with the blueprint in it. I’m working in the 4.6.1 version of the editor.

I’m not sure where I can see the log files, but I’ll try out including the code in a new project and tell you the results.

I just replicated the crash in a new project.

  1. Create a C++ project based on the third-person template (I think it probably crashes regardless of templates, but I didn’t test it)
  2. Add code to the project in the form of a class called ShipConsole based on Actor.
  3. Replace ShipConsole.h with the contents here: http://hastebin.com/jezuxujetu.m
  4. Replace ShipConsole.cpp with the contents here: http://hastebin.com/uhahevodof.coffee
  5. Start the UE4 editor through Visual Studio and create a new blueprint based on ShipConsole.
  6. Place the new blueprint somewhere in the scene
  7. Play

At this point, the game crashes and a box pops up saying: "UE4Editor.exe has triggered a breakpoint. The callstack can be found in the original post. After pressing continue twice, the game can run, but the ship console doesn’t seem to work properly

Hey Siretu-

I was able to reproduce the breakpoint triggered in Visual Studio. However when the same code is not ran through debug mode, no break/crash occurs. It appears that the editor handles the break showing in VS. I have reported the triggered break (UE-9861) for further investigation as both debug mode and a full build should be able to handle this action in similar ways.

Cheers

Hi ,

Thanks for the update. However, I think the solution you suggested just hides the error. I’m still having trouble.

I attached an OnComponentBeginOverlap-event on my UCapsuleComponent (as can be seen in the previous code, although it was commented before) and I’m logging messages when an actor enters or leaves the capsule.

When I place the blueprint in the scene, it looks like the capsule is following the blueprint around and that everything is attached correctly. Picture: https://i.imgur.com/qDubFNC.jpg

However, when I play the game, the messages print when the character approaches (0,0,0) instead of the position of the blueprint. Picture: https://i.imgur.com/gvxfxrL.png

Therefore, it seems to me like the capsule component is not being attached correctly and is instead staying at (0,0,0) although it looks like it’s following along correctly in the editor.

This might be due to me just doing something wrong, but I have no idea what that would be. Until then, I’ll remove your answer as accepted, if that’s okay with you.

Hey Siretu-

You set the relative scale/location of your capsule before you actually attach it to the beacon which may be causing the offset issue (and may be causing the breakpoint as well). I would suggest attaching the capsule after creating it and then setting the relative information so that it knows what it’s supposed to be relative to.

This however is a separate issue from the crash you originally posted about. If you continue to have a problem with how the capsule component is behaving I would suggest opening a new post related to that specifically. This will help ensure that each issue gets proper attention and will make searching for information regarding each issue easier.

Hi,

I tried attaching the capsule right after creating it, and I even tried removing the row that set the relative scale. Unfortunately, the behavior is exactly the same.

I personally think these two issues are closely related. My theory is that it tries to attach the capsule to the static mesh and fails hard (causing the breakpoint). Since it failed to attach it, the component does not follow the static mesh and just stays at (0,0,0), causing the behavior I described.

However, if you want, I’ll create a new post, although I feel like it would be very similar since omitting the information about the crash in the new post would be bad.

After messing around some more with the code, I managed to fix both the crash and the the non-working attachment.

The problem was caused by my re-declaring the activation capsule in the constructor. Namely the following row:

UCapsuleComponent* ActivationCapsule = ObjectInitializer.CreateDefaultSubobject<UCapsuleComponent>(this, TEXT("dummy"));

When I changed that to:

ActivationCapsule = ObjectInitializer.CreateDefaultSubobject<UCapsuleComponent>(this, TEXT("dummy"));

everything suddenly started working without any problem. I’m not sure if this is intended behavior, and it might be worth looking into. At least this might help others running into the same problem.

Thanks for your help, it led me on the right path towards figuring it out.