언리얼/게임 프로젝트

GameplayAbilitySystem을 이용한 RPG 프로젝트 - (6) Enemy와 Player에서 ASC와 AS 설정하기

monstro 2024. 11. 18. 01:37
728x90
반응형

이제 적과 플레이어 클래스에서 ASC와 AS를 설정해보겠습니다.

 

1) AAuraBaseCharacter에서 상속된 적 클래스 AAuraEnemy

#pragma once

#include "CoreMinimal.h"
#include "Character/AuraCharacterBase.h"
#include "Interface/EnemyInterface.h"
#include "AuraEnemy.generated.h"

/**
 * 
 */
UCLASS()
class AURA_API AAuraEnemy : public AAuraCharacterBase, public IEnemyInterface
{
	GENERATED_BODY()

public:
	AAuraEnemy();

	virtual void BeginPlay() override;
	
protected:
	/*
	* EnemyInterface Section
	*/
	virtual void HighlightActor() override;

	virtual void UnHighlightActor() override;
};
#include "Enemy/AuraEnemy.h"
#include "AbilitySystem/AuraAbilitySystemComponent.h"
#include "AbilitySystem/AuraAttributeSet.h"
#include "Aura.h"

AAuraEnemy::AAuraEnemy()
{
	GetMesh()->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);

	AbilitySystemComponent = CreateDefaultSubobject<UAuraAbilitySystemComponent>(TEXT("AbilitySystemCompoenent"));
	AbilitySystemComponent->SetIsReplicated(true);
	AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Minimal);

	AttributeSet = CreateDefaultSubobject<UAuraAttributeSet>(TEXT("AttributeSet"));
}

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

	AbilitySystemComponent->InitAbilityActorInfo(this, this);
}

.
.
.

 

생성자에서는 ASC와 AS를 설정하였습니다.

그리고 ReplicationMode의 경우 Minimal로 설정하겠습니다.

또한 적 클래스의 경우 AI에 의해 조종되므로 BeginPlay에서 InitActorInfo 함수를 호출하겠습니다.

 

이제 플레이어 클래스를 진행해보겠습니다.

2) 플레이어 클래스인 AAuraPlayer

.
.
.
UCLASS()
class AURA_API AAuraPlayer : public AAuraCharacterBase
{
	GENERATED_BODY()
	
public:
	AAuraPlayer();

	virtual void PossessedBy(AController* InController) override;
	virtual void OnRep_PlayerState() override;

.
.
.

private:
	/*
	* member function
	*/
	void SetAbilityActorInfo();
};
.
.
.
#include "AbilitySystem/AuraAbilitySystemComponent.h"
#include "AbilitySystem/AuraAttributeSet.h"

.
.
.

void AAuraPlayer::PossessedBy(AController* InController)
{
	Super::PossessedBy(InController);

	SetAbilityActorInfo();
}

void AAuraPlayer::OnRep_PlayerState()
{
	Super::OnRep_PlayerState();

	SetAbilityActorInfo();
}

void AAuraPlayer::SetAbilityActorInfo()
{
	// 서버단계에서 ASC의 OwnerActor와 AvatarActor를 초기화 + BaseCharacter에서 상속된 멤버들을 PlayerState 값으로 초기화
	AAuraPlayerState* AuraPlayerState = CastChecked<AAuraPlayerState>(GetPlayerState());
	AuraPlayerState->GetAbilitySystemComponent()->InitAbilityActorInfo(AuraPlayerState, this);
	AbilitySystemComponent = AuraPlayerState->GetAbilitySystemComponent();
	AttributeSet = AuraPlayerState->GetAttributeSet();
}

 

플레이어는 로직이 조금 복잡합니다.

플레이어의 ASC와 AS은 BaseCharacter가 아닌 PlayerState로부터 가져와야 하므로

SetAbilityActorInfo라는 함수를 통해 이를 수행합니다.

 

SetAbilityActorInfo 함수에서는 AAuraPlayerState를 가져와

ASC의 OwnerActor와 AvatarActor를 설정합니다.

이때 전자는 PlayerState, 후자는 Player가 됩니다.

그후에 Player의 ASC와 AS를 초기화합니다.

 

또한 SetAbilitActorInfo 함수를 서버와 클라이언트 양쪽에서 처리할 수 있도록

PossessedBy 함수OnRep_PlayerState 함수를 오버라이드하여 

SetAbilityActorInfo 함수를 호출하도록 하였습니다.

728x90
반응형