Download

Anyone can help me with this issue?

Hey lovely People, So I created a Weapon pick up in C++ and I have just added Animations now however when I play animations and then play it again my weapon disappears. Here is the following File I created:

Weapon.H:

#pragma once

#include “CoreMinimal.h”
#include “Item.h”
#include “Weapon.generated.h”

UENUM(BlueprintType)
enum class EWeaponState : uint8
{
EWS_Pickup UMETA(DisplayName “Pickup”),
EWS_Equipped UMETA(DisplayName “Equipped”),

EWS_MAX UMETA(DisplayName “DefaultMax”)
};

/**
*
*/
UCLASS()
class FIRSTPROJECT_API AWeapon : public AItem
{
GENERATED_BODY()
public:

AWeapon();

UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = “Item”)
EWeaponState WeaponState;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Item | Particle”)
bool bWeaponParticle;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Item | Sound”)
class USoundCue* OnEquipSound;

UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = “SkeletalMesh”)
class USkeletalMeshComponent* SkeletalMesh;

virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult) override;
virtual void OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) override;

void Equip(class AMain* Char);

FORCEINLINE void SetWeaponState(EWeaponState State) { WeaponState = State; }
FORCEINLINE EWeaponState GetWeaponState() { return WeaponState; }
};

Weapon.CCP:

#include “Weapon.h”
#include “Components/SkeletalMeshComponent.h”
#include “Main.h”
#include “Engine/SkeletalMeshSocket.h”
#include “Sound/SoundCue.h”
#include “Kismet/GameplayStatics.h”
#include “Particles/ParticleSystemComponent.h”

AWeapon::AWeapon()
{
SkeletalMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT(“Skeletal Mesh”));
SkeletalMesh->SetupAttachment(GetRootComponent());

bWeaponParticle = false;

WeaponState = EWeaponState::EWS_Pickup;
}

void AWeapon::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
Super::OnOverlapBegin(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
if ((WeaponState == EWeaponState::EWS_Pickup) && OtherActor)
{
AMain* Main = Cast<AMain>(OtherActor);
if (Main)
{
Main->SetActiveOverlappingItem(this);
}
}
}

void AWeapon::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
Super::OnOverlapEnd(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
if (OtherActor)
{
AMain* Main = Cast<AMain>(OtherActor);
if (Main)
{
Main->SetActiveOverlappingItem(nullptr);
}
}
}

void AWeapon::Equip(AMain* Char)
{
if (Char)
{
SkeletalMesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
SkeletalMesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);

SkeletalMesh->SetSimulatePhysics(false);

const USkeletalMeshSocket* RightHandSocket = Char->GetMesh()->GetSocketByName(“RightHandSocket”);
if (RightHandSocket)
{
RightHandSocket->AttachActor(this, Char->GetMesh());
bRotate = false;
Char->SetEquippedWeapon(this);
Char->SetActiveOverlappingItem(nullptr);
}
if (OnEquipSound) UGameplayStatics::PlaySound2D(this, OnEquipSound);
if (!bWeaponParticle)
{
IdleParticlesComponent->Deactivate();
}
}
}