Download

Attaching one component to another crash: 'template mismatch during attachment'

Hey guys,

So I’ve extended APlayerStart with my own, AShipStart, from which I inherit to create the blueprint BP_ShipStart.

I’m adding a UBoxComponent to it, which I’m using as a sort of spawn zone that must be clear of players before it’s considered valid/usable.

.h:



UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Components)
TSubobjectPtr<UBoxComponent> SpawnBoxComponent;


.cpp



AShipStart::AShipStart(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	SpawnBoxComponent = PCIP.CreateDefaultSubobject<UBoxComponent>(this, "Spawn Box");
	SpawnBoxComponent->SetWorldScale3D(FVector(200, 200, 200));
	SpawnBoxComponent->AttachTo(RootComponent);
}



After this, when I open the editor I get a message about a ‘bad component’ with reference to the above UBoxComponent.

It also often crashes when hitting ‘Play’ in the editor.

Any ideas? Never encountered this before.

RootComponent = SpawnBoxComponent;

Why would I want to replace the pre-existing capsule root component?

You didnt give any information about root component.

APlayerStart comes with a root capsule component.

Error says: root component is a template component, obviously you need both components to be templates or instances. You need to show information about how RootComponent is initialized and how Capsule Component is created.

What does that mean?

Have no idea what is template component ;d Perhaps i could say smth if you show how you initialize RootComponent and create Capsule Component.

UPD: wait, i found APlayerStart in ue4.

UPD2: well, i see nothing strange in RootComponent. May be you should try to remove VisibleDefaultsOnly specifier for your component?

PS: i am learning Components right now, thats why i am interested in your question too.

Aye, but the capsule component is initialized in ANavigationObjectBase:



CapsuleComponent = PCIP.CreateDefaultSubobject<UCapsuleComponent>(this, TEXT("CollisionCapsule"));
CapsuleComponent->ShapeColor = FColor(255, 138, 5, 255);
CapsuleComponent->bDrawOnlyIfSelected = true;
CapsuleComponent->InitCapsuleSize(50.0f, 50.0f);
CapsuleComponent->BodyInstance.bEnableCollision_DEPRECATED = false;
CapsuleComponent->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
CapsuleComponent->bShouldCollideWhenPlacing = true;
CapsuleComponent->bShouldUpdatePhysicsVolume = true;
CapsuleComponent->Mobility = EComponentMobility::Static;
RootComponent = CapsuleComponent;
bCollideWhenPlacing = true;


I don’t see what makes it a ‘template’ component.

Y me too, try to change your component’s specifiers, for example remove VisibleDefaultsOnly.
As i understand template component is a component which has flags RF_ArchetypeObject|RF_ClassDefaultObject.

UPD: i’ve tested UPROPERTY specifiers, they doesnt affect. Well, i suppose the problem is somewhere in blueprint, may be you instantiate SpawnBoxComponent somewhere or smth like that. Because i’ve tested to attach the same box to the same capsule and everything is fine. I am using UE4.4.1.

UPD2: if i create SpawnBoxComponent or CapsuleComponent without using any blueprints it is still IsTemplate()==false. So, afterall, i think the problem is about how UE4 engine using ANavigationObjectBase inside engine and why Capsule component is becoming Template for this object. Component is not supposed to be Template by default i think.

The question is what is Template Componnt and how it did become a Template.

Hey try.



UPROPERTY(VisibleAnywhere, Category = "MyComponents")
TSubobjectPtr<UBoxComponent> SpawnBoxComponent;



AShipStart::AShipStart(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	SpawnBoxComponent = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Spawn Box"));
        if (SpawnBoxComponent )
        {
	      SpawnBoxComponent->AttachParent = RootComponent;
	      SpawnBoxComponent->SetWorldScale3D(FVector(200, 200, 200));
        }
}

That definitly should work. The question is, why UE4 engine supports so strange methods of attaching, if they meaning absolutely the same, but behaves differently…

I made some observations of UE4 code, and i think AttachParent is used mostly inside constructors, while AttachTo is not used inside constructors.

Same error. When I open the editor/level:

When I hit play, and crash:

Line:744? What is your UE version?

Preview build 2289630.

AttachTo is auto-called inside OnRegister for AttachParent variable, so… yes, you still have the same error.
Try to use previous stable version.

**The question still is **how Component did become a Template.

Ok, im out. This is definitely the problem of UE4, not of your component.

Couple of shot gun answers to various things in the thread:

As was mentioned AttachParent should be used when setting up your components in the constructor because it will do the AttachTo and all the associated actions when the components get registered. Doing AttachTo in the constructor will generally not get you what you want.

Template component simply means that it is a component that belongs to the Class Default Object, which is the prototype/template object from which other instanced objects get their properties initialized from.

The ensure message means that when the attachment is being executed your SpawnBox component is trying to attach itself to the component in the CDO, not its own instanced version.

I tried to reproduce this ensure by creating a new subclass of ANavigationObjectBase and just putting in the subobject pointer as in WCode’s post, but I couldn’t reproduce it. It probably shouldn’t cause an issue, but try taking the space out of the name of the component?

If you’re able to isolate the problem down to a simple repro in a new project, please do post on answerhub and attach the project.

Hi Marc,

I’ve managed to reproduce this issue with Preview Build 2289630: Adding UBoxComponent to a custom PlayerStart class crashes gameplay - UE4 AnswerHub

Turns out it only happens when you place the custom PlayerStart in the world, then copy-paste it to make several instances. This process is fine with normal PlayerStart objects, but when you try to add your own components like I have above, and then copy-paste that custom PlayerStart, it crashes when PIE (haven’t tested standalone).

Hello everyone!
Who can solve my problem?

When i attach a actor to my character mesh(C++), error " template mismatch during attachment "

but it is correct in Blueprint
what’s wrong?

This is still present in unreal (4.24), so going to post this for future reference - compile and resaving the blueprint that the actor was generated from will fix this error. Would be great if unreal could warn about this kind of issue in a more understandable way.