x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Attach Object to Skeletal Mesh Socket C++

Hi guys!

I am kind of stuck at a problem I ran into. I want to attach my Weapon Blueprint to the Character Socket.
.cpp

     const USkeletalMeshSocket* weaponSocket;
     weaponSocket = GetMesh()->GetSocketByName(FName("WeaponSocket"));
     FName fnWeaponSocket = weaponSocket->GetFName();
 
     Debug::Log(fnWeaponSocket.ToString());
 
     m_cUClassWeapon = StaticLoadClass(UObject::StaticClass(), nullptr, TEXT("/Game/Blueprints/CharacterController/BP_Pistol.BP_Pistol_C"));
     m_cWeapon = GetWorld()->SpawnActor<AWeapon>(m_cUClassWeapon, spawnParams);
     m_cWeapon->AttachRootComponentToActor(this, fnWeaponSocket, EAttachLocation::SnapToTarget, true);

The problem is that Unreal obviously does not find the Socket which I attached to the Skeletal Mesh in Editor.

alt text

The engine always attaches the weapon to the center of my character.

alt text

When I print the Variable on Screen in Line 5 I get the Name "SkeletalMeshSocket_0", which is obviously not the name I gave the socket but seems to me like some kind of intern name? What really bugs me is the fact, that when I build the same structure with blueprints it works perfectly and the weapon snaps to the selected socket.

So I just want to know, did I miss something in code or is this some unreal related bug?

Thanks for helping!

Product Version: UE 4.9
Tags:
weaponspawn.png (730.1 kB)
socketlocation.png (458.2 kB)
more ▼

asked Jan 06 '16 at 02:02 PM in C++ Programming

avatar image

rdFoe
13 1 2 4

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

this should work

 FName fnWeaponSocket = TEXT("WeaponSocket");
 m_cUClassWeapon = StaticLoadClass(UObject::StaticClass(), nullptr, TEXT("/Game/Blueprints/CharacterController/BP_Pistol.BP_Pistol_C"));
 m_cWeapon = GetWorld()->SpawnActor<AWeapon>(m_cUClassWeapon, spawnParams);
 m_cWeapon->AttachRootComponentTo(GetMesh(), fnWeaponSocket, EAttachLocation::SnapToTarget, true);
more ▼

answered Jan 06 '16 at 02:38 PM

avatar image

Mhousse1247
3.4k 152 28 317

avatar image rdFoe Jan 06 '16 at 02:48 PM

Thanks for the advice. That was my first attempt in doing this and it didnt work, so I tried solving it with GetSocketByName()

avatar image Mhousse1247 Jan 06 '16 at 02:49 PM

look at the 4th line

avatar image rdFoe Jan 06 '16 at 02:54 PM

Oh, I am such a blind fool! Thank you so much, worked perfectly!

avatar image staticvoidlol Jan 06 '16 at 03:03 PM

Well spotted on the GetMesh().

(comments are locked)
10|2000 characters needed characters left

I just use the actual name when attaching to a socket, i.e.

 FName weaponSocketName = TEXT("WeaponSocket");
 
 m_cWeapon->AttachRootComponentToActor(this, weaponSocketName, EAttachLocation::SnapToTarget, true);
more ▼

answered Jan 06 '16 at 02:37 PM

avatar image

staticvoidlol
2.5k 93 40 111

avatar image rdFoe Jan 06 '16 at 02:46 PM

That was my first attemp in doing this and it didn't work. Its the reason why I tried to get the socket name with GetSocketByName().

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question