Attach Actor to socket via C++




	FVector SocketLocationR;
	SocketLocationR = Mesh->GetSocketLocation("WeaponR");
	FVector SocketLocationL;
	SocketLocationL = Mesh->GetSocketLocation("WeaponL");

	FActorSpawnParameters SpawnInfo;
	FVector SocketLocation;
	const FRotator CharRotation = GetControlRotation();

	AMyWeaponActor* Weapon = GetWorld()->SpawnActor<AMyWeaponActor>(WeaponClassSlot1, SocketLocationR, SocketRotationR); // spawn the weapon on the socket location

	Weapon->AttachTo(Mesh, SocketLocationR); // attach the weapon to our pawn and socket


-AMyWeaponActor is the weapon Class
-WeaponClassSlot1 is the weapon I want to spawn

Im getting an error , anyone know whats wrong?

Edit: it doesnt seem to like “Weapon->AttachTo(Mesh,SocketLocationR);”

“An error” is a little vague. What is the error message? Does it just crash to desktop? Does it mount without rotation?

I don’t know anything about the system, but I do know that it’s easier for people to help if you can provide more information…

it said -
Error 1 error C2027: use of undefined type ‘AMyWeaponActor’

for the line-
Weapon->AttachTo(Mesh, SocketLocationR);

Are you including the header file for that class? If not, that could be what is causing this problem.

Add this to the your includes and see if it works.



#include "MyWeaponActor.h"


I hope this helps.

still not working, but now im getting this error
Weapon->AttachParent(Mesh, SocketLocationR); // attach the weapon to our pawn and socket
Error 1 error C2039: ‘AttachParent’ : is not a member of ‘AMyWeaponActor’

I also tried

Weapon->Mesh->AttachParent(Mesh, SocketLocationR);
Error 1 error C2039: ‘Mesh’ : is not a member of ‘AMyWeaponActor’

Well, that’s plain enough - Weapon->AttachParent() doesn’t exist - you can’t do that because AMyWeaponActor does not have that method.

Maybe look at how weapons are equipped. How can I switch weapons in ShooterGame? - C++ - Unreal Engine Forums - a quick search of the documentation turned that up…

sorry, I’ve read the page and I dont see how the weapon gets attached to the player. All I see is; spawning the weapon and some phases that aren’t in the documentation like; Inventory.Num(), EquipWeapon(Inventory[0]), AddWeapon().

your going to have to spell this out for me, sry

Actually, I’m not “going to have to” do anything…

I don’t know how to do what you’re trying to do - but I’d guess that if you look at your own post above (where you mention “EquipWeapon()” ) and follow the code you’ll figure it out.

is how I attach my weapon to the FPS Mesh in my project.
I created a socket on the bone of the mesh and then spawn the Weapon Actor and then attach it.
Like this.



bool AMyProjectWeapon::AttachWeaponToPawn(APawn* WeaponOwner, FString SkeletalCompName)
{
	OwningPawn = WeaponOwner; // Sets the owner of this weapon. (Note: OwningPawn  is a APawn* member in my weapon class.)
	SetOwner(OwningPawn); // AActor::SetOwner(APawn*);

       // A method i created to get any skeletal component from the owning pawn. (Could be made a template to function to get any type.)
	USkeletalMeshComponent* ArmMesh = GetPawnSkeletalComp(SkeletalCompName); 

	if (ArmMesh) // Check to see if our rig Skeletal Mesh Component is good.
	{
                // AActor::AttachRootComponentTo(..)
		AttachRootComponentTo(ArmMesh, FName(TEXT("WeaponPoint")), EAttachLocation::SnapToTarget); // Attach the root component of our Weapon actor to the ArmMesh at the location of the socket.
		return true; // Note: We can only assume it is attached, since epic did not provide a return value.
	}
	else
	{
		return false;
	}
}

USkeletalMeshComponent* AMyProjectWeapon::GetPawnSkeletalComp(FString ComponentName)
{
	TArray<UActorComponent*> Components; // Array for Pawns components
	USkeletalMeshComponent* ArmMesh = NULL; // Will hold the skeletal mesh we want

	if (OwningPawn)
		OwningPawn->GetComponents(Components); // Get the components from the owning pawn

	for (UActorComponent* Comp : Components)
	{
		ArmMesh = Cast<USkeletalMeshComponent>(Comp); // Cast any component to Skeletal Comp.
		if (ArmMesh) // If we have a skeletal mesh
		{
			if (ArmMesh->GetName() == ComponentName) // Check to see if its the one we want.
				return ArmMesh;
		}
	}

	return NULL; // Finished with out finding the component we wanted.
}

Hope it helps.

@VegasRich, sry didnt mean it literally, it was just a figure of speech

Looks interesting but im not how to get it to work.

I replaced AMyProjectWeapon with AMyWeaponActor, in the header file I wrote



	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
		APawn* OwningPawn;
protected:
	bool AttachWeaponToPawn(APawn, FString);
        void GetPawnSkeletalComp(FString);


but im getting errors saying
error C2597: illegal reference to non-static member ‘AMyWeaponActor::OwningPawn’ (a bunch of times)
error C2511: ‘bool AMyWeaponActor::AttachWeaponToPawn(APawn *,FString)’ : overloaded member function not found in ‘AMyWeaponActor’

and a whole bunch of other stuff, what needs doing?

Hey

I am guessing OwningPawn is not present in your class, is not public, or you are passing in a none pointer.

The only thing you should need to do to get this to work is.
Change the names of them e.g:


bool AMyProjectWeapon::AttachWeaponToPawn(APawn* WeaponOwner, FString SkeletalCompName)
// to
bool YourWeaponClassNameHERE::AttachWeaponToPawn(APawn* WeaponOwner, FString SkeletalCompName)

Then add the


APawn* OwningPawn;

As a public member in YourWeaponClassNameHERE::
And last but not least add the declerations in the header file.



UCLASS()
class AMyProjectWeapon : public AActor
{
	GENERATED_UCLASS_BODY()

public:

	/*
		Attaches the weapon to the pawn and sets Current Weapon to this weapon.
		@param WeaponOwner: Is the pawn that the weapon belongs to.
		@param SkeletalCompName: The name of the component we want. 
		@return bool: True if attached the weapon to pawn was successfull.
	*/
	UFUNCTION()
	virtual bool AttachWeaponToPawn(APawn* WeaponOwner, FString SkeletalCompName);

	/*	
		Returns the Skeletal Component for the Pawn "Owner" of the Weapon.
		@return USkeletalMeshComponent* or NULL if not found.
	*/
	UFUNCTION()
	virtual USkeletalMeshComponent* GetPawnSkeletalComp(FString ComponentName);

	/* The pawn that the weapons belong to. (NULL if on the ground.) */
	UPROPERTY()
		APawn* OwningPawn;
       
        // ....
};



Let me know if you are stile having issues, should be a simple fix.

I’ve been trying to get this to work as well.

Im trying to call the function like this:
AMyWeapon wep;
wep.AttachWeaponToPawn(GetWorld()->GetFirstPlayerController()->GetPawn(), TEXT(“HeroTPP”));

but Im getting an error:
error C2512: ‘AMyWeapon’ : no appropriate default constructor available

It needs to be a pointer to a object.
Like so:


AMyWeapon* wep;
wep->AttachWeaponToPawn(GetWorld()->GetFirstPlayerController()->GetPawn(), TEXT("HeroTPP"));


That was my first thought, but I get a build error of:
error C4700: uninitialized local variable ‘wep’ used

if I try to declare wep in the header, I get another error.

It’s telling you that your variable AMyWeapon* wep; is not set to anything but garbage. That means functions can’t be called on it etc. (they can but will just result in an error/crash).

Your variable needs to be instantiated in some way, calling new on it, or calling a function that will return you an instantiated AMyWeapon*.

(I’m new to the C++ side of the engine, so I don’t precisely know how you need to create a new AMyWeapon, but this should help you look in the right place.)

Did you declare your AMyWeapon:: ?
If you copy pasted the code snip it should be.


AMyProjectWeapon* wep;

But am assuming you know that.

If you stile have problems post your code on paste bin and i will have a look, and see if i can help you out.

No,
AMyWeapon::AttachWeaponToPawn(APawn* WeaponOwner, FString SkeletalCompName);
can not be declared like that. a nonstatic member reference must be relative to a specific object.

http://pastebin.com/yNkRd2y3

Ok mate that don`t help at all. :stuck_out_tongue:
Post your header and cpp file for your class AMyWeapon::,

is the h and cpp of MyWeapon:
http://pastebin.com/MAE6t1wc

Ok first of I dont know what this is


class "LASERTAG_API" AMyWeapon

The only thing i can think of thats a problem is that you did not spawn the weapon,
in the world. Before you call AttachWeaponToPawn(…)

So



void ALaserTagCharacter::BeginPlay()
{
     AMyWeapon* wep = GetWorld()-SpawnActor<AMyWeapon>(....);
     if (wep)
     {
          wep->AttachWeaponToPawn(GetWorld()->GetFirstPlayerController()->GetPawn(), TEXT("HeroTPP"));
     }
}