본문 바로가기

Unreal/언리얼 엔진 5로 개발하는 멀티플레이어 게임(Book)

[Unreal] US_WeaponProjectileComponent.cpp 코드 분석

SceneComponent를 상속받은 스크립트 US_WeaponProjectileComponent를 생성하여 작성했습니다.

입력 연결

void UUS_WeaponProjectileComponent::BeginPlay()
{
    Super::BeginPlay();

    const ACharacter* Character = Cast<ACharacter>(GetOwner());
    if (!Character)
        return;

    if (const APlayerController* PlayerController = Cast<APlayerController>(Character->GetController()))
    {
        if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
        {
            Subsystem->AddMappingContext(WeaponMappingContext, 1);
        }

        if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerController->InputComponent))
        {
            EnhancedInputComponent->BindAction(ThrowAction, ETriggerEvent::Triggered, this, &UUS_WeaponProjectileComponent::Throw);
        }
    }
}

 

해당 코드는 이전 글에 작성된 부분과 다른 점이 적기 때문에 간략히 살펴보려고 한다.

Subsystem->AddMappingContext(WeaponMappingContext, 1): 매핑된 입력 액션을 가져온다.
EnhancedInputComponent->BindAction(): 트리거를 통해 바인딩한다.

액션

void UUS_WeaponProjectileComponent::Throw_Server_Implementation()
{
    if (ProjectileClass)
    {
        const auto Character = Cast<AUS_Character>(GetOwner());
        const auto ProjectileSpawnLocation = GetComponentLocation();
        const auto ProjectileSpawnRotation = GetComponentRotation();
        auto ProjectileSpawnParams = FActorSpawnParameters();
        ProjectileSpawnParams.Owner = GetOwner();
        ProjectileSpawnParams.Instigator = Character;

        GetWorld()->SpawnActor<AUS_BaseWeaponProjectile>(ProjectileClass, ProjectileSpawnLocation, ProjectileSpawnRotation, ProjectileSpawnParams);
    }
}

 

ProjectileSpawnParams = FActorSpawnParameters(): 언리얼 엔진에서 액터를 동적으로 생성할 때 추가적인 설정을 제공하기 위해 사용하는 구조체이다. 지금은 소유자 설정과 스폰할 액터의 주체를 정했다.

무기 변경

void UUS_WeaponProjectileComponent::SetProjectileClass(TSubclassOf<class AUS_BaseWeaponProjectile> NewProjectileClass)
{
    ProjectileClass = NewProjectileClass;
}