hi,
I’m trying to add in C++ only (no blueprint) a weapon to my character.
Now I have this :
BaseWeapon.h
BaseWeapon.cpp
MyProject2Character.h
MyProject2Character.cpp
si, this is my BaseWeapon.h :
#pragma once
#include "Engine/StaticMeshActor.h"
#include "BaseWeapon.generated.h"
/**
*
*/
UCLASS()
class MYPROJECT2_API ABaseWeapon : public AStaticMeshActor
{
GENERATED_BODY()
public:
ABaseWeapon();
UStaticMeshComponent* mStaticMeshComponent;
UStaticMesh* mStaticMesh;
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "init")
void setObject(const FString & mesh_name);
void init(const FString & mesh_name);
};
Then this is my BaseWeapon.cpp
#include "MyProject2.h"
#include "BaseWeapon.h"
ABaseWeapon::ABaseWeapon()
{
UE_LOG(LogClass, Warning, TEXT(" CREATE BASE WEAPON "));
mStaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("weapon"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshOb_AW2(TEXT("StaticMesh'/Game/SWORD.SWORD'"));
mStaticMesh = StaticMeshOb_AW2.Object;
RootComponent = mStaticMeshComponent;
};
void ABaseWeapon::init(const FString & mesh_name)
{
setObject_Implementation(mesh_name);
}
void ABaseWeapon::setObject_Implementation(const FString & mesh_name)
{
UE_LOG(LogClass, Warning, TEXT(" init ADDING... "));
//static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshOb_AW2(TEXT(mesh_name)); // why Lmesh_name doesn't exist ???
mStaticMeshComponent->SetStaticMesh(mStaticMesh);
mStaticMeshComponent->SetCollisionProfileName(TEXT("h_weapon"));
mStaticMeshComponent->Mobility = EComponentMobility::Movable;
mStaticMeshComponent->OnComponentBeginOverlap.AddDynamic(this, &ABaseWeapon::OnBeginOverlap);
mStaticMeshComponent->bGenerateOverlapEvents = true;
UE_LOG(LogClass, Warning, TEXT(" init ok... "));
}
So in my character, this is what I do :
weapon = NewObject<ABaseWeapon>();
weapon->init(TEXT(""));
//RootComponent or not ?
weapon->AttachToComponent(GetMesh(), FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("Hand_RSocket"));
// or maybe ->
//weapon->mStaticMeshComponent->AttachToComponent(GetMesh(), FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("Hand_RSocket"));
So here, everything compiles, but nothing is showed in my Character,
Any idea somebody ?
thanks