레벨별로 아이템 확률을 조정한 데이터 테이블을 따로 만들어준다.

이제 아이템 기능을 구현한다.
플레이어의 체력 만들기
캐릭터의 체력은 캐릭터에서 관리한다. PlayerState는 멀티플레이로 가면 중요하고, 싱글 플레이에서는 그다지 중요하지 않다. 그래서 Character에서 관리해도 괜찮은 것이다.
Health와 MaxHealth 변수를 선언하고 관련 함수를 만든다
UFUNCTION(BlueprintPure, Category = "Health")
float GetHealth() const;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Health")
float MaxHealth;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Health")
float Health;
// 생성자
MaxHealth = 100.0f;
Health = MaxHealth;
// GetHealth
float ASpartaCharacter::GetHealth() const
{
return Health;
}
AddHealth
언리얼에서는 데미치 처리 시스템을 이미 제공한다.
- UGamePlaySattics::ApplyDamage(): 데미지 입히기
- 어떤 액터에 데미지를 입혔는지를 전달
- AActor::TakeDamage(): 데미지를 받기
- ApplyDamage에서 호출
- 오버라이드 가능하므로 각 액터마다 다르게 해줄 수 있음
virtual float TakeDamage(
float DamageAmount,
struct FDamageEvent const& DamageEvent,
AController* EventInstigator,
AActor* DamageCauser) override;
- DamageAmount → 들어온 데미지 값
- DamageEvent → 데미지 종류 (일반 / 포인트 / 범위 등)
- EventInstigator → 데미지를 준 컨트롤러 (플레이어 등)
- DamageCauser → 실제 데미지를 준 액터 (총알, 폭발 등)
이를 이용하여 Character 클래스에 TakeDamage 함수를 오버라이드해준다.
점수 만들기
GameState
전역정보를 저장하는 클래스. 진행시간, 스폰된 아이템 개수 등을 저장한다.

GameMode처럼 GameState도 GameStateBase와 GameState로 나뉜다. GameStateBase는 싱글모드, 단순한 게임에 많이 사용되고 GamaState는 멀티플레이어 환경을 고려할 때 사용된다.
GameStateBase는 GameModeBase와, GameState는 GameMode와 맞고 게임모드와 게임 스테이트가 다르다면 로그에 LogGameMode: Error: Mixing AGameStateBase with AGameMode is not compatible. Change AGameStateBase subclass (SpartaGameStateBase) to derive from AGameState, or make both derive from Base 라는 에러가 출력된다.
GameStateBase
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameStateBase.h"
#include "SpartaGameStateBase.generated.h"
/**
*
*/
UCLASS()
class SPARTAPROJECT_API ASpartaGameStateBase : public AGameStateBase
{
GENERATED_BODY()
public:
ASpartaGameStateBase();
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Score")
int32 Score; // 점수
UFUNCTION(BlueprintPure, Category = "Score")
int32 GetScore() const;
UFUNCTION(BlueprintCallable, Category = "Scores")
void AddScore(int32 Amount);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "SpartaGameStateBase.h"
ASpartaGameStateBase::ASpartaGameStateBase()
{
Score = 0;
}
int32 ASpartaGameStateBase::GetScore() const
{
return Score;
}
void ASpartaGameStateBase::AddScore(int32 Amount)
{
Score += Amount; // 점수 획득
}
GameMode에 GameState 설정
#include "SpartaGameMode.h"
#include "SpartaCharacter.h"
#include "SpartaPlayerController.h"
#include "SpartaGameStateBase.h"
ASpartaGameMode::ASpartaGameMode()
{
DefaultPawnClass = ASpartaCharacter::StaticClass(); // 코드로 디폴트 폰 클래스 적용. StaticClass는 객체를 생성하지 않고 메타데이터로 선언해준다. UCLASS 타입을 반환
PlayerControllerClass = ASpartaPlayerController::StaticClass();
GameStateClass = ASpartaGameStateBase::StaticClass();
}
CoinItem에서 점수 추가 호출
'게임 개발 > 학습 일지' 카테고리의 다른 글
| 언리얼 C++ - UI 위젯 설계와 실시간 데이터 연동하기 (0) | 2026.04.27 |
|---|---|
| 언리얼 C++ - State Machine 설계를 통한 캐릭터 동작 애니메이션 적용하기 (0) | 2026.04.24 |
| 언리얼 C++ - 아이템 스폰 및 레벨 데이터 관리하기 (0) | 2026.04.22 |
| 언리얼 C++ - 인터페이스 기반 아이템 클래스 설계하기 (1) | 2026.04.21 |
| 언리얼 엔진 심화 2주차 - 컨테이너와 포인터 (0) | 2026.04.19 |