Error for FAttachmentTransformRules

I’m trying to attach one character to another, for that I’m trying to use AttachToActor().
This is my implementation:
On the header:

FAttachmentTransformRules AttachRules;

On the .cpp file:

Survivor->AttachToActor(this, AttachRules);

Survivor is a variable that points to the other Character. But when I hit compile I get this error:

error C2512:
‘FAttachmentTransformRules’: no
appropriate default constructor
available

Anyone can point me on the right direction?

1 Like

The struct “FAttachmentTransformRules” does not have a default constructor, as the error is telling you, so you would be required to initialize it in your class’s constructor or, in the header file, you could give it a default value (“FAttachmentTransformRules AttachRules = …;”). The available defaults are listed in the “Constants” section of the documentation page.

1 Like

I see. How I should set it up on the class constructor? Sorry, I’m still learning. Can you provide me an example of the struct initialization?

Sure! What I’m talking about, so you know for the future, is called the “member initializer list”. If you look at the “Constructors” section in the documentation, there are two constructors you can use. The only difference between the two is the one with two parameters uses the same rule for location, rotation, and scale attachment rules. If you were going to use the first, your constructor would look something like this:

AMyActor::AMyActor()
    : AttachRules(EAttachmentRule::KeepRelative, true)
{
    // Rest of your constructor...
}

If you used the other contructor, then you would just supply two more EAttachmentRule arguments.

My contructor look like this now

 // Sets default values
    AKillerBase::AKillerBase() : AttachRules(EAttachmentRule::KeepRelative, true)
    {
     	// 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;

But I’m still getting the same error. Did I do something wrong?

‘FAttachmentTransformRules’: no
appropriate default constructor
available

Hmm, that seems like it should be fine… Does the compile error provide you with a specific line of code? If so, what does it look like?

I actually noticed that the error is no longer on the .cpp file, but rather the .gen.cpp

KillerBase.gen.cpp(297) : error C2512:
‘FAttachmentTransformRules’: no
appropriate default constructor
available

And this is line 297 on that file:

DEFINE_VTABLE_PTR_HELPER_CTOR(AKillerBase);

Looking into it more, “FAttachmentTransformRules” is not marked as a Blueprint type so I don’t believe it would show up in the details panel at all.

If you need to be able to edit the attach rules in editor, what you can do instead is expose three “EAttachmentRule” properties (one for each of position, rotation, and scale), or one property if all three rules would be the same, as well as a bool property for whether or not to weld simulated bodies together when attaching. You could then create the “FAttachmentTransformRules” object as a local variable when you set the attachment.

If you don’t need it to show up in the editor because the attachment will always be the same, then it does not need to be a class-level variable and can be created locally.

I don’t need it on Blueprints so I can try to declare it locally. So I’d go like this?

void AKillerBase::KillerInteraction()
{
	if (Survivor && bSurvivorInRange)
	{
		FAttachmentTransformRules AttachRules;
		Survivor->AttachToActor(this, AttachRules);
	}
}

Because like this I’m already getting the default constructor thing on VS and the engine. Thanks for the help btw

Kind of, yeah. However, like the error is telling you, doing it that way is still utilizing the default constructor (which doesn’t exist for that type). You could either supply the values yourself, like with the member initializer list, or you could use one of the constants like this:

Survivor->AttachToActor(this, FAttachmentTransformRules::KeepWorldTransform);

Finally it compiles. Thank you