Help attaching actor to character

How do i attach an actor class to a socket on the character in the character class for example

header file

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = backpack)
class ABackpack* bp; // this is an Actor

cpp file

ACCharacter::ACCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)

{

 //Im not sure what to do here in terms of adding the actor to the socket

}

well im doing it in C++ i made an actor class but i don’t know how to assign that to the characters socket

yes but i went into the skeletal mesh and added a socket to a bone in the spine

well im not using any blueprints right now im trying to get the ABackpack class from another cpp file and attach it to the character. when i ran that bit of code it gave me an unhandled exception

Hi,

If you wanted the bp member to be attached to your character, you could try:

bp->AttachRootComponentToActor(this, TheSocketName, EAttachLocation::SnapToTarget);

What it does is that it attaches your ABackpack’s root component to the ACharacter’s root component.
In fact, actors aren’t attached to each other, their root components are.

Hope this helps

I’m not sure to understand what you mean by the character socket ? Does your ACCharacter class inherit from the unreal ACharacter class ?

Ok so if you want “bp” to be attached to a specific socket of your skeletal mesh, here’s what you would need to do in your ACCharacter constructor:

For UE 4.7 and later:

bp->AttachRootComponentTo(GetMesh(), TheSocketName, EAttachLocation::SnapToTarget);

for previous versions:

bp->AttachRootComponentTo(Mesh, TheSocketName,  EAttachLocation::SnapToTarget);

Is it what you are looking for ?

What unhandled exception does it gives to you ? Is your “bp” member initialized ?

You would need to debug things a bit with visual studio but i’m pretty sure “bp” must be null, or your ABackpack’s root component is also not initialized.

Unhandled exception at 0x00007FF8468FF992 (UE4Editor-Engine.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000180.

and yes it is initialized

here is all the code between character and my jetpack class in case your wondering i switched to jetpack but same concept and problem
Link to source code

You are directly accessing “jet” line 181 but I don’t see where it is initialized. Try something like this just above:

For UE 4.7 and above:

jet = NewObject<AJetpack>(this, AJetpack::StaticClass());

or for previous versions:

jet = ConstructObject<AJetpack>(this, AJetpack::StaticClass());

EDIT: jet is not a property, so either you add the UPROPERTY() macro to your jet member, either you create your jet member with a simple new() allocator. The simplest solution is to replace line 97 by:

UPROPERTY()
AJetpack* jet;