[UE5 / C++] ๐Ÿ‘ฟ ์  ๋งŒ๋“ค๊ธฐ (Feat. FSM) (4)

Singery00ยท2024๋…„ 4์›” 29์ผ
0

UE5 C++

๋ชฉ๋ก ๋ณด๊ธฐ
14/20
post-thumbnail

๊ฐœ์š”

๐Ÿ’ก C++๋กœ ์ ์„ ๋งŒ๋“ค์–ด๋ณด์ž. ๋˜ํ•œ ์œ ํ•œ์ƒํƒœ๊ธฐ๊ณ„๋ฅผ ํฌํ•จํ•˜์—ฌ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ๋‹ค.

์ด๋ฒˆ์—๋Š” Enemy Manager๊ฐ€ Spawn Point๋ฅผ ์Šค์Šค๋กœ ์ฐพ์•„์„œ ํ• ๋‹นํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


๋ณธ๋ก 


FindSpawnPoints() ์„ ์–ธ

EnemyManager.h์—์„œ FindSpawnPoints()๋ฅผ ์„ ์–ธํ•ด์ฃผ์„ธ์š”.

public:
	// ์ƒ๋žต
	void CreateEnemy();


	// ์ถ”๊ฐ€
	void FindSpawnPoints();

FindSpawnPoints() ์ •์˜

EnemyManager.cpp์—์„œ FindSpawnPoints()๋ฅผ ์ •์˜ํ•ด์ฃผ์„ธ์š”.

#include <EngineUtils.h>

void AEnemyManager::FindSpawnPoints()
{

}

๋ฐฉ๋ฒ•1 : TActorIterator

1๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ TActorIterator๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

ํŠน์ • ์•กํ„ฐ๋ฅผ ์ฐพ๋Š”๋ฐ์— ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

FindSpawnPoints() ์ •์˜

์•„๋ž˜์™€ ๊ฐ™์ด TActorIterator๋ฅผ ์‚ฌ์šฉํ•ด ์›”๋“œ์˜ ์•กํ„ฐ๋ฅผ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค.

์ˆœํšŒ์ค‘์ธ ์•กํ„ฐ์—๊ฒŒ BP_EnemySpawnPoint๋ž€ ์ด๋ฆ„์ด ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด spawnPoints์— ์ถ”๊ฐ€ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ๋ณธ์ธ์ด Spawn Point์˜ ์ด๋ฆ„์„ ๋‹ค๋ฅด๊ฒŒ ํ–ˆ๋‹ค๋ฉด, ๊ทธ ์ด๋ฆ„์„ ๋„ฃ์–ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

#include <EngineUtils.h>

void AEnemyManager::FindSpawnPoints()
{
	for (TActorIterator<AActor>It(GetWorld());It;++It)
	{
		AActor* spawn = *It;

		if (spawn->GetName().Contains(TEXT("BP_EnemySpawnPoint")));
		{
			spawnPoints.Add(spawn);
		}
	}
}

ํ˜ธ์ถœ

BeginPlay()์—์„œ ํ˜ธ์ถœํ•ด์ฃผ์„ธ์š”.

void AEnemyManager::BeginPlay()
{
	Super::BeginPlay();
	
	CreateAlarm();
	FindSpawnPoints();
}

ํ• ๋‹น ์‚ญ์ œ

์šฐ๋ฆฌ๊ฐ€ ํ• ๋‹นํ–ˆ๋˜ Spawn Point๋“ค์€ ์‚ญ์ œํ•ด์ค๋‹ˆ๋‹ค.


โ—์ปดํŒŒ์ผ ์ €์žฅโ—ํ›„ ์‹คํ–‰ํ•ด์ฃผ์„ธ์š”.

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ Enemy Manager๋ฅผ ๋ณด๋ฉด ํ”Œ๋ ˆ์ด ์ค‘ ์Šค์Šค๋กœ Spawn Point๋“ค์„ ์ฐพ์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• 2 : GetAllActorsOfClass()

์ด๋ฒˆ์—” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด ๋‚ด์šฉ์€ ์ฃผ์„์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์„ธ์š”.

GetActorOfClass๋ฅผ ์‚ฌ์šฉํ•ด์„œ Player๋ฅผ ์ฐพ๊ณ  target์— ๋“ฑ๋กํ•˜์‹ ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์‹œ๋‚˜์š”?

์ด๋ฒˆ์—” GetAllActorsOfClass()๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ชจ๋“  Spawn Point๋“ค์„ ๊ฐ€์ ธ์™€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์›”๋“œ์— ๋ฐฐ์น˜๋œ ์•กํ„ฐ๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  GetAllActorsOfClass()๋ฅผ ํ†ตํ•ด ์›”๋“œ์˜ ๋ชจ๋“  ์•กํ„ฐ๋ฅผ ๊ฐ€์ ธ ์˜ต๋‹ˆ๋‹ค.

void AEnemyManager::FindSpawnPoints()
{
	TArray<AActor*> allActors;

	UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), allActors);

}

๊ทธ๋ฆฌ๊ณ  ์•กํ„ฐ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ด๋ฆ„์— "BP_EnemySpawnPoint"๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด, SpawnPoints์— ์ถ”๊ฐ€ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

์—ญ์‹œ ๋™์ผํ•˜๊ฒŒ Spawn Point์˜ ์ด๋ฆ„์„ ๋‹ค๋ฅด๊ฒŒ ํ–ˆ๋‹ค๋ฉด, ๊ทธ ์ด๋ฆ„์„ ๋„ฃ์–ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

void AEnemyManager::FindSpawnPoints()
{
	// ์ƒ๋žต
    
    // ์ถ”๊ฐ€
	for (auto spawn : allActors)
	{
		if (spawn->GetName().Contains(TEXT("BP_EnemySpawnPoint")))
		{
			spawnPoints.Add(spawn);
		}
	}
}

โ—์ปดํŒŒ์ผ ์ €์žฅโ—ํ•˜์—ฌ ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๋ฉด 1๋ฒˆ ๋ฐฉ๋ฒ•๊ณผ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งˆ๋ฌด๋ฆฌ

์—ฌ๊ธฐ๊นŒ์ง€ FSM์„ ์ ์šฉํ•œ ์ ์„ ๊ตฌํ˜„ํ•˜๊ณ , ์›”๋“œ์—์„œ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

profile
๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜์–ด๋ณด์ž

0๊ฐœ์˜ ๋Œ“๊ธ€