Hello!
I am trying to setup a basic function to equip a sword (Skeletal Mesh) to my character. I have setup all the code in c++ and have the function call in blueprints (for now). My parameter (AWeaponBase* WeaponToEquip) returns a nullptr and I’m not sure why.
I’m trying to call the function from my main character, HeroBase.cpp and my WeaponBase class is pretty basic. I’ve only created a Mesh/BoxComponent.
HeroBase.cpp
void AHeroBase::EquipWeapon(AWeaponBase* WeaponToEquip)
{
if (!WeaponToEquip)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(
-1,
15.f,
FColor::Red,
FString::Printf(TEXT("WEAPON NOT EQUIPPED!!"))
);
}
return;
}
const USkeletalMeshSocket* WeaponSocket = GetMesh()->GetSocketByName(FName("WeaponSocket_r"));
if (WeaponSocket)
{
WeaponSocket->AttachActor(WeaponToEquip, GetMesh());
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(
-1,
15.f,
FColor::Green,
FString::Printf(TEXT("WEAPON EQUIPPED!!"))
);
}
}
}
WeaponBase.h
UCLASS()
class OATHBOUND_API AWeaponBase : public AActor
{
GENERATED_BODY()
public:
AWeaponBase();
virtual void Tick(float DeltaTime) override;
protected:
virtual void BeginPlay() override;
virtual void PostInitializeComponents() override;
private:
/*
* Weapon Components
*/
UPROPERTY(VisibleAnywhere, Category = "Components")
USkeletalMeshComponent* WeaponMesh;
UPROPERTY(VisibleAnywhere, Category = "Components")
UBoxComponent* WeaponCollision;
public:
UFUNCTION()
void WeaponCollisionOnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void WeaponCollisionOnEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};
HeroBase BP call:
If there is anything I’m doing wrong or need to add, please let me know!
I apologize if this is something that all beginners should know, but I’ve been stuck for awhile now.
Thank you in advance!!
