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 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 : alt text

alt text

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 ? :(

Product Version: UE 4.15
Tags:
more ▼

asked May 15 '17 at 12:47 PM in C++ Programming

avatar image

F4Kedj
1 1

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

1 answer: sort voted first

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.

more ▼

answered May 15 '17 at 01:04 PM

avatar image

Benergy
831 21 4 29

avatar image F4Kedj May 15 '17 at 01:09 PM

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

avatar image Benergy May 15 '17 at 02:59 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
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