Detach (Attach) from (to) component C++

Hello, I’m a complete newbie. Tell me how to write correctly in C++?

	///attach component to component
	
	FAttachmentTransformRules* myAttachRules;
	myAttachRules->LocationRule = EAttachmentRule::KeepWorld;
	myAttachRules->RotationRule = EAttachmentRule::KeepWorld;
	myAttachRules->ScaleRule = EAttachmentRule::SnapToTarget;
	myAttachRules->bWeldSimulatedBodies = true;
	myBastard->AttachToComponent(myMesh,*myAttachRules,"None");

	///detach
	
	FDetachmentTransformRules* myDetachRules;
	myDetachRules->LocationRule = EDetachmentRule::KeepWorld;
	myDetachRules->RotationRule = EDetachmentRule::KeepWorld;
	myDetachRules->ScaleRule = EDetachmentRule::KeepRelative;
	myDetachRules->bCallModify = true;;
	myBastard->DetachFromComponent(*myDetachRules);
1 Like

Thank you, you know C++ very well!

But I have a problem. If I try to attach or detach a component, I get an error and the program closes.

I don’t even know what to do with this.

image

those are the lines where the error is crashing and 0x0000000000000 means you are trying to use an unnasigned pointer so some of your variables (mybastard or mesh) are empty/not assigned.

try to encapsulate the attach/detach in a validation before using it and see wat happens:

if (mybastard && mesh)
{
     code
}

just findout what is unnasigned.

1 Like

Same error.

I should probably create a SwordBastard class. Then create an object of the SwordBastard class. And then try to attach/detach it. Right?

idk…both variables mybastard and mesh must have something assigned…if one of those is empty then will crash.

how are you storing both components inside the variables?

1 Like
USkeletalMeshComponent* Mesh;
Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
Mesh->SetupAttachment(Capsula);
static ConstructorHelpers::FObjectFinder<USkeletalMesh> M(TEXT("/Game/Mordhau/Pers/Model/MordhauMesh.MordhauMesh"));
if (M.Succeeded()) Mesh->SetSkeletalMesh(M.Object);

USkeletalMeshComponent* myBastard;
myBastard = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("myBastard"));
myBastard->SetupAttachment(Capsula);
static ConstructorHelpers::FObjectFinder<USkeletalMesh> S(TEXT("/Game/Mordhau/Oruzhie/myBastard/myBastard.myBastard"));
if (S.Succeeded()) myBastard->SetSkeletalMesh(S.Object);		}