How to return actor/s inside a socket?

Hi all, I’m looking for a way to return the actor/component placed inside a socket on a skeletal mesh.

ie; is there a node I can use that can get the actor that currently inside a certain socket so that I can cast to it and destroy it, etc.

Right now I have an equipment system where when the player chooses to equip a weapon it spawns that weapon blueprint in their hand socket which contains a static mesh and blueprint key events (Left Mouse Button does an attack animation and Q unequips (destroys the actor)) the weapon blueprint is set to have input enabled to make this possible.

This works really well, except now I have a dual wield kind of system [(shown in this video)][1] and when I press Q to destroy them both it works most of the time except sometimes it only destroys one and ignores the other Q key press causing huge run-time problems to my system when I go to equip something else.

I’d much rather have the key events ONLY in the player character where everything else is, so I could press Q and the player character blueprint would get any components/actors in the sockets and destroy them both… or press left mouse button to get the weapon inside the socket/s and run their attack routine. I read on another answerhub question that this can be done using interfaces but the user didn’t go into anymore detail than that and I can’t figure out how to do this on my own.

So I’m looking for some help creating this particular weapons interface or something else if someone has a better/easier idea to handle the system I have. =)

Thanks in advance!

In a simple way do you mean how to attach something to a socket? If yes then try here: Unreal Engine 4 Tutorial: Attach to Socket (english) - YouTube
If not then I can’t help :expressionless:

I’ve already attached actors to sockets, I just want to detach (or destroy) them from outside of their blueprint, to do this I need to ‘get’ the current actor inside of the socket and destroy it, but I don’t know how to get it.

I’ve updated the title of my question so people don’t get confused.

Oh, sorry then I don’t know

I was able to resolve this using event dispatchers called from my player character and bound inside my weapons. Still, there doesn’t seem to be a way to return which component is inside of a socket, but my system works exactly as I want it to. :slight_smile:

I had this same question. Posted it up several months ago and have not heard back.

I use detach from parent then attach to component in my level blueprint.

The actors attached to a socket are added as children to the Skeletal Mesh Component and are kept in a list called AttachChildren. You can go through the list of children and check to see which child is attached to the socket in question.

Example:

	for (auto SceneComp : GetMesh()->AttachChildren)
	{
		if (SceneComp->AttachSocketName == TEXT("MySocketName"))
		{
			// we found it!
		}
	}

I hope this helps.

Based on the answer TooManyCrashes I would like to go more into detail on how to get the actors (read: any scenecomponent) of a socket:

//Loop through all the sockets of the mesh
	for (auto SceneComp : GetMesh()->AttachChildren)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, SceneComp->AttachSocketName.ToString());

//We want to get all children of a specific socket name
		if (SceneComp->AttachSocketName == this->HolsterGrenadeWeaponSocketName)
		{
//Now we found the socket that we want, so lets loop though all the children of this socket and print out each child's name
			for (auto x : GetMesh()->AttachChildren)
			{
				// we found an actor!
				GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, x->GetFullName());
			}
		}
	}

Thanks. Nice solution. But you got a little error I think. For the second iteration, you want to go over SceneComp->AttachChildren. Don’t you?

USkeletalMeshComponent *Mesh = GetMesh();
if ( Mesh )
{
	for ( auto SceneComp : Mesh->AttachChildren )
	{
		UE_LOG( LogTemp, Warning, TEXT( "%s" ), *SceneComp->AttachSocketName.ToString() );
		if ( SceneComp->AttachSocketName == TEXT("WeaponSocket") )
		{
			for ( auto AttachedChild : SceneComp->AttachChildren )
			{
				UE_LOG( LogTemp, Warning, TEXT( "** %s" ), *AttachedChild->GetFullName() );

			}
		}
	}
}

Edit

Also it doesn’t return an AActor but an USceneComponent, which you cannot cast to the actual AActor you originally attached to the socket.

Edit 2

If you change *AttachedChild->GetFullName() to *AttachedChild->GetPathName() you will get something like

/Game/ThirdPersonCPP/Maps/UEDPIE_3_ThirdPersonExampleMap.ThirdPersonExampleMap:PersistentLevel.Weapon1_BP_C_538.StaticMesh

Now you can theoretically split the string from reverse so that you end up just having Weapon1_BP_C_538. Then you could try using FindObject to get something out of it. Not sure if this works however.

You are right. I didn’t like the approach as I hit the same issue as you mentioned so I solved it by keeping a separate list of references to attached actors.

This worked for me.

In 2021, with 4.26.2 release, you can do the following, consider in my case I attach a static mesh, and so I return a static mesh here:

In your .h

UFUNCTION(BlueprintCallable, Category = "PlaneShift Utils")
static UStaticMeshComponent* GetMeshAttachedToSocket(USkeletalMeshComponent* mesh, FName socketName);

in your .cpp

UStaticMeshComponent* UBPUtils::GetMeshAttachedToSocket(USkeletalMeshComponent* mesh, FName socketName)
{
    for (USceneComponent* SceneComp : mesh->GetAttachChildren())
    {
        if (SceneComp && SceneComp->GetAttachSocketName() == socketName)
        {
            return Cast<UStaticMeshComponent>(SceneComp);
        }
    }

    return nullptr;
}