Hey, I am trying to “GetSocketByName()” in my custom EquipmentComponent.cpp
and it only gets nullptr but in HeroCharacter.cpp
, it can works well. I do not why.
In HeroCharacter.cpp
const USkeletalMeshSocket* HandSocket = GetMesh()->GetSocketByName(FName("hand_r_weapon"));
In EquipmentComponent.cpp
const USkeletalMeshSocket* HandSocket = HeroCharacter->GetMesh()->GetSocketByName(FName("hand_r_weapon"));
Here is the complete code。
HeroCharacter.cpp
#include "HeroCharacterBase.h"
#include "Engine/LocalPlayer.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/Controller.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "SpriteLand/Character/Type/Hero/Component/EquipmentComponent.h"
#include "SpriteLand/Systems/Feature/EquipmentSystem/Weapon/WeaponBase.h"
AHeroCharacterBase::AHeroCharacterBase()
{
PrimaryActorTick.bCanEverTick = true;
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f);
GetCharacterMovement()->JumpZVelocity = 700.f;
GetCharacterMovement()->AirControl = 0.35f;
GetCharacterMovement()->MaxWalkSpeed = 500.f;
GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;
GetCharacterMovement()->BrakingDecelerationFalling = 1500.0f;
GetCharacterMovement()->GravityScale = 1.5f;
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 400.0f;
CameraBoom->bUsePawnControlRotation = true;
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
EquipmentComponent = CreateDefaultSubobject<UEquipmentComponent>(TEXT("EquipmentComponent"));
}
void AHeroCharacterBase::BeginPlay()
{
Super::BeginPlay();
}
void AHeroCharacterBase::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AHeroCharacterBase::Move(const FInputActionValue& Value)
{
// input is a Vector2D
FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
// get right vector
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void AHeroCharacterBase::Look(const FInputActionValue& Value)
{
// input is a Vector2D
FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// add yaw and pitch input to controller
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
void AHeroCharacterBase::Dodge()
{
if (DodgeMontage != nullptr)
{
PlayAnimMontage(DodgeMontage);
}
}
void AHeroCharacterBase::JumpBegin()
{
Jump();
}
void AHeroCharacterBase::Equip()
{
if (WeaponClass)
{
AWeaponBase* Weapon = GetWorld()->SpawnActor<AWeaponBase>(WeaponClass);
if (EquipmentComponent || Weapon)
{
EquipmentComponent->EquipWeapon(Weapon);
}
}
}
EquipmentComponent.cpp
#include "EquipmentComponent.h"
#include "SpriteLand/Systems/Feature/EquipmentSystem/Weapon/WeaponBase.h"
#include "SpriteLand/Character/Type/Hero/HeroCharacterBase.h"
#include "Engine/SkeletalMeshSocket.h"
UEquipmentComponent::UEquipmentComponent()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UEquipmentComponent::BeginPlay()
{
Super::BeginPlay();
UE_LOG(LogTemp, Log, TEXT("Owner: %s"), *GetOwner()->GetName());
HeroCharacter = Cast<AHeroCharacterBase>(GetOwner());
}
void UEquipmentComponent::EquipWeapon(AWeaponBase* InWeapon)
{
if (InWeapon == nullptr) return;
if (HeroCharacter == nullptr)
HeroCharacter = Cast<AHeroCharacterBase>(GetOwner());
if (HeroCharacter)
{
if (Weapon)
{
Weapon->Destroy();
}
Weapon = InWeapon;
const USkeletalMeshSocket* HandSocket = HeroCharacter->GetMesh()->GetSocketByName(FName("hand_r_weapon"));
if (HandSocket)
{
HandSocket->AttachActor(Weapon, HeroCharacter->GetMesh());
}
}
}
void UEquipmentComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}