C++ AttachToComponent syntax

Hi there! When trying to attach a camera to my characters capsule component,

FirstPersonCameraComponent->AttachToComponent = GetCapsuleComponent;

I get the C++ error saying “=”: Function as left operand, which I can’t seem to figure out how to solve. Any help from you guys? Thank you ahead of time.

AttachToComponent is a function that you have to call, not a variable that you can set. The basic usage would be:

Child->AttachToComponent(Parent, FAttachmentTransformRules::KeepWorldTransform);

Where Child and Parent are both USceneComponents. The FAttachmentTransformRules is a Struct that specifies what happens when you attach it (for example if it stays where it is or moves to the parent, wether the scale stays the same and so on). You can either create one of these structs and set the members like this:

FAttachmentTransformRules rules;
rules.TheMemberYouWantToChange = TheValueYouWantItToHave;
Child->AttachToComponent(Parent, rules);

or use one of the presets as I did in the first codeblock. Check out the AttachToComponent Documentation and the FAttachmentTransformRules Documentation if you want more detailed information.

1 Like

If this solved your problem please accept the answer as correct!

The FAttachmentTransformRules won’t compile if you don’t use the constructor. For my case, I used the constructor which takes in the location rule, rotation rule, scale rule, and a bool for bWeldSimulatedBodies and it works.

FAttachmentTransformRules TransformRules = FAttachmentTransformRules( EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true);

You can also pass a single rule and it will apply to all of the attachment rules.

1 Like