Add weapon to character - showed in blueprint but not in the game

Hi,

I tried to add a weapon to my character in C++.
Now I’ve added 2 weapons as simple mesh linked to a socket, and it moves with the character. Great.

But now I want to create a weapon.h and weapon.cpp, and add it to my player.
Today I have this files, and I’ve added the file into the blueprint of my character.
In the viewport, the weapon is showing vertically. But it wont be added nor visible in game.

so, this is my baseweapon.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Engine/StaticMeshActor.h"
#include "BaseWeapon.generated.h"

/**
 *
 */
UCLASS()
class MYPROJECT2_API ABaseWeapon : public AStaticMeshActor
{
	GENERATED_BODY()

	UStaticMeshComponent* mStaticMeshComponent;
	UStaticMesh* mStaticMesh;

	ABaseWeapon();

};

and my baseweapon.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyProject2.h"
#include "BaseWeapon.h"

ABaseWeapon::ABaseWeapon()
{
	mStaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BaseWeapon1start"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshOb_AW2(TEXT("StaticMesh'/Game/SWORD_START.SWORD_START'"));
	mStaticMesh = StaticMeshOb_AW2.Object;

	mStaticMeshComponent->SetStaticMesh(mStaticMesh);
	mStaticMeshComponent->SetCollisionProfileName(TEXT("h_weapon"));
	mStaticMeshComponent->Mobility = EComponentMobility::Movable;
	mStaticMeshComponent->RegisterComponent();
	mStaticMeshComponent->OnComponentBeginOverlap.AddDynamic(this, &ABaseWeapon::OnBeginOverlap);
	mStaticMeshComponent->bGenerateOverlapEvents = true;

  mStaticMeshComponent->AttachToComponent(GetRootComponent(), FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), "Hand_RSocket"); // Probably useless because it's already added in the blueprint of the character !?

 
}

and in my character blueprint :
137977-

So, then, as you we see the weapon is showing in the viewport, and the socket is respected.
However I can’t see this in the game…

Any idea ? :frowning:

Have you tried using the already existing StaticMeshComponent parameter of the AStaticMeshActor instead of your custom mStaticMeshComponent?

Also, in your last line where you call mStaticMeshComponent->AttachToComponent, you try to attach mStaticMeshComponent to the root component of your ABaseWeapon. I think this isn’t what you had in mind.

Hum, well I didn’t found this on tutorials,
How would you do use the StaticMeshComponent of AStaticMeshActor in c++ ?

Based on your code I would try:

 ABaseWeapon::ABaseWeapon()
 {
     static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshOb_AW2(TEXT("StaticMesh'/Game/SWORD_START.SWORD_START'"));
     mStaticMesh = StaticMeshOb_AW2.Object;
 
     StaticMeshComponent->SetStaticMesh(mStaticMesh);
     StaticMeshComponent->SetCollisionProfileName(TEXT("h_weapon"));
     StaticMeshComponent->Mobility = EComponentMobility::Movable;
     StaticMeshComponent->OnComponentBeginOverlap.AddDynamic(this, &ABaseWeapon::OnBeginOverlap);
     StaticMeshComponent->bGenerateOverlapEvents = true;
 }

Note that I removed RegisterComponent and AttachToComponent. There’s also the chance that you need to call SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics) if you want overlap events, but perhaps that’s enabled by default.