I am trying to make a weapon switch system. I wanna change static mesh with an input but when I try It doesn’t work. Any help would be appreciated.
How do you trying to switch it?
Ok but how you currently trying to do it? ;p question sounds like you attempting it
When 1 press for inatance I want the static mesh to be a pistol. When I press 2 a shotgun and so on.
Static mesh is attached to camera by the way, if that makes a difference.
I cant give the code right now but I use constructorhelpers to get static mesh reference. Then I use setstaticmesh inside void Pistol. Problem is that it either crashes or doesnt do anything. When i set static mesh in constructor it works just fine.
Hi,
You will need to created some Inputs. Go to Project Settings >> Input >> Action Mappings.
Set as many as you need.
//In you character header file:
/** Set default weapon */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Weapon")
class UStaticMeshComponent* WeaponMesh;
/** Add inputs for weapon */
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
/** Weapon 1 */
UFUNCTION()
void ChangeWeapon1();
/** Weapon 2 */
UFUNCTION()
void ChangeWeapon2();
/** Weapon 1 */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon")
class UStaticMesh* Weapon01;
/** Weapon 2 */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon")
class UStaticMesh* Weapon02;
//In your CPP file
#include "Components/StaticMeshComponent.h"
#include "UObject/ConstructorHelpers.h"
#include "Engine/StaticMesh.h"
#include "Components/InputComponent.h"
ACHARACTER::ACHARACTER()
{
// set the mesh
WeaponMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("WEAPON"));
// attach to root component
WeaponMesh->SetupAttachment(GetRootComponent());
// set path for static mesh
static ConstructorHelpers::FObjectFinder<UStaticMesh> Shotgun(TEXT("'StaticMesh'/Engine/BasicShapes/Cylinder.Cylinder'"));
// check if path is valid
if (Shotgun.Succeeded())
{
// mesh = valid path
WeaponMesh->SetStaticMesh(Shotgun.Object);
// set relative location of mesh
WeaponMesh->SetRelativeLocation(FVector(70.f, 0.f, 0.f));
}
Weapon01 = CreateDefaultSubobject<UStaticMesh>("Weapon01");
static ConstructorHelpers::FObjectFinder<UStaticMesh> WeaponA(TEXT("'/Game/Geometry/Meshes/1M_Cube_Chamfer.1M_Cube_Chamfer'"));
// check if path is valid
if (WeaponA.Succeeded())
{
// mesh = valid path
Weapon01 = WeaponA.Object;
}
Weapon02 = CreateDefaultSubobject<UStaticMesh>("Weapon02");
static ConstructorHelpers::FObjectFinder<UStaticMesh> WeaponB(TEXT("'/Engine/BasicShapes/Cone.Cone'"));
// check if path is valid
if (WeaponB.Succeeded())
{
// mesh = valid path
Weapon02 = WeaponB.Object;
}
}
void ACHARACTER::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Set up gameplay key bindings
check(PlayerInputComponent);
PlayerInputComponent->BindAction("ChangeWeapon1", IE_Pressed, this, &ACHARACTER::ChangeWeapon1);
PlayerInputComponent->BindAction("ChangeWeapon2", IE_Pressed, this, &ACHARACTER::ChangeWeapon2);
}
void ACHARACTER::ChangeWeapon1()
{
WeaponMesh->SetStaticMesh(Weapon01);
}
void ACHARACTER::ChangeWeapon2()
{
WeaponMesh->SetStaticMesh(Weapon02);
}
God bless you man. I reall y hope you have multiple nice days.
You’re welcome. You too. All the best!
yes, I too am very thankful for this
Thanks Man :3, maybe exist another way to set the static Mesh in c++? or only way to do this is using FObjectFinder?
Hey no probs. Perhaps, but this is the only way I know how to do it.