Blueprint and C++

Hi, i want to know if someone can help me with this.

I create a Weapon Class with some functions, then i used that class to create a Blueprint Class to create a custom Weapon setting a weapon mesh, and then i want to get that blueprint in c++ and attach to to the pawn mesh but im not be able to do it.

For getting the blueprint i use:

static ConstructorHelpers::FObjectFinder<UBlueprint> Weapon(TEXT(“Blueprint’/Game/Weapons/Claire/ClaireGunBP.ClaireGunBP’”));
CurrentWeapon = (UClass*)Weapon.Object->GeneratedClass;

then i set this in a custom function:

CurrentWeaponActor = GetWorld()->SpawnActor<ARE2RGunBase>(CurrentWeapon, FVector(-690.0f,-60.0f,175.0f),FRotator(0,0,0));
CurrentWeaponActor->SetOwningPawn(this);
CurrentWeaponActor->AttachMeshToPawn();

and AttachMeshToPawn has:

DetachMeshFromPawn();
FName AttachPoint = OwnPawn->GetWeaponAttachPoint();
USkeletalMeshComponent* PawnMesh = OwnPawn->GetCharacterMesh();
Mesh->AttachTo(PawnMesh, AttachPoint);

The weapon apears on the world but, not attaching it to the mesh component of the pawn

Hi Larraz,

At first glance, I can’t see where your structure is failing. It looks pretty reasonable. Your blueprint loading looks correct, but you may be able to set up your weapons without having to use the ConstructorHelpers to grab a blueprint explicitly. Generally, I try to avoid doing that, since it limits your ability to reorganize your blueprint classes later on. Here’s an example of using TSubclassOf pointers to allow your users to select their weapon blueprints from the pawn blueprints’ defaults:

In the Pawn’s header, we set up two members: an array of weapon classes to spawn and an array of spawned weapon instances.

///////////////////////////////////////////////////////////////////////////
// Inventory
/** This pawn's weapon classes */
UPROPERTY(EditDefaultsOnly, Category=WarmachineGameplay)
TArray&lt;TSubclassOf&lt;class AWarmachineWeapon&gt; &gt; Weapons;

/** This pawn's weapons. */
UPROPERTY(Transient)
TArray&lt;class AWarmachineWeapon*&gt; WeaponInstances;

In our case, we handled our inventory in a component. Generally, I’d just recommend doing it in the pawn. From our component’s InitizlizeComponent(), we made a call to spawn the inventory:

void UWarmachinePawnStatsComponent::SpawnInventory()
{
AWarmachinePawn* OwnerWMPawn = Cast<AWarmachinePawn>(GetOwner());

// spawn weapons
for (int32 WeaponIdx = 0; WeaponIdx &lt; Weapons.Num(); WeaponIdx++)
{
	if (Weapons[WeaponIdx])
	{
		FActorSpawnParameters SpawnInfo;
		SpawnInfo.bNoCollisionFail = true;
		class AWarmachineWeapon* NewWeapon = GetWorld()-&gt;SpawnActor&lt;AWarmachineWeapon&gt;(Weapons[WeaponIdx], SpawnInfo);
		check(NewWeapon);
		NewWeapon-&gt;OnEnterInventory(OwnerWMPawn);
		WeaponInstances.Add(NewWeapon);
	}
}

// Equip the first weapon by default.
if (WeaponInstances.IsValidIndex(0))
{
	EquipWeapon(0);
}

}

The weapon’s OnEnterInventory() looks like this:

void AWarmachineWeapon::OnEnterInventory(AWarmachinePawn* NewOwner)
{
if (Role == ROLE_Authority)
{
Instigator = NewOwner;
MyPawn = NewOwner;

	// net owner for RPC calls
	SetOwner(NewOwner);
}

// warmachine weapons are always visibly attached to their pawns, either in a holstered or an equipped position.
AttachMeshToPawn();

}

Your attachment method looks right to me. For reference, here’s what ours looks like (in our case, our weapons each store the socket names on their owning pawns to which they should be attached when equipped, and when holstered).

(WeaponMeshComponentName is just a static const FName we’re declaring in the class implementation:

namespace WarmachineWeaponConstants
{
static const FName WeaponMeshComponentName = FName(TEXT(“WeaponMesh”));
}

)

void AWarmachineWeapon::AttachMeshToPawn()
{
if (MyPawn)
{
DetachMeshFromPawn();

	USceneComponent* DefaultWeaponMeshComponent = Cast&lt;USceneComponent&gt;(GetClass()-&gt;GetDefaultSubobjectByName(WarmachineWeaponConstants::WeaponMeshComponentName));
	Mesh-&gt;RelativeLocation = DefaultWeaponMeshComponent-&gt;RelativeLocation;
	Mesh-&gt;RelativeRotation = DefaultWeaponMeshComponent-&gt;RelativeRotation;
	Mesh-&gt;RelativeScale3D = DefaultWeaponMeshComponent-&gt;RelativeScale3D;

	FName AttachPoint;
	if (bIsEquipped)
	{
		AttachPoint = EquippedAttachPoint;
	} 
	else
	{
		AttachPoint = HolsteredAttachPoint;
	}
	Mesh-&gt;AttachTo(MyPawn-&gt;Mesh, AttachPoint);
	CurrentAttachPoint = AttachPoint;
	Mesh-&gt;SetHiddenInGame(false);
	if (bStartInvisible)
	{
		Mesh-&gt;SetVisibility(false);
	}
}

}

Sorry I wasn’t able to spot exactly where your setup was failing, but maybe something in the examples I’ve provided will help.

Good luck!