x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Add StaticMeshActor to Character won't show

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

Product Version: UE 4.15
Tags:
more ▼

asked May 16 '17 at 07:52 AM in C++ Programming

avatar image

F4Kedj
1 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question