UDN
Search public documentation:

MyFirstHUDKR
English Translation
日本語訳

Interested in the Unreal Engine?
Visit the Unreal Technology site.

Looking for jobs and company info?
Check out the Epic games site.

Questions about support via UDN?
Contact the UDN Staff

예제: My First HUD

2003년 11월 18일, 2226 빌드용으로 Chris Linder (DemiurgeStudios?) 작성. 최종 업데이트 Chris Linder (DemiurgeStudios?). 최종 업데이트 Michiel Hendriks – 간단한 v3323 업데이트.

관련 문서

My First Code, My First Pawn, My First Controller, My First GameInfo, HeadsUpDisplayReference?, Canvas Reference?

서론

My First Code 문서를 아직 읽지 않으셨다면 먼저 그것을 읽으십시오.

이 문서에서는 아바타가 “춤 춘” 횟수를 보고하는 간단한 헤드업 디스플레이를 작성하는 방법에 대해 살펴 봅니다 (자세한 내용은 My First Controller 참고). 이 예제에서는 이 목표를 이루기 위해 2개의 클래스를 사용합니다. 첫 번째 클래스인 ExampleHUD.ucHUD.uc 를 연장하며, 오른쪽 아래 구석에 아바타가 춤춘 횟수를 표시합니다. 'F1' 키를 누르면 HUD 대신 ScoreBoard.uc 를 연장하는 두 번째 클래스 ExampleScoreboard.uc 가 표시됩니다. 이 점수판은 현 게임에 있는 모든 사람들, 또는 적어도 화면에 나타나 있는 사람들의 이름과 점수를 보여줍니다.

ExampleHUD

위에서 언급한 것처럼, ExampleHUD 는 아바타가 춤춘 횟수를 오른쪽 아래 구석에 표시합니다. 아바타가 춤춘 횟수는 플레이어에 대한 PlayerReplicationInfoScore 변수에 보존됩니다. (상세한 내용은 My First Controller 문서를 참고하십시오.)

이전 빌드에서(2226), HUD를 그릴 수 있기 전에 가장 먼저 해야할 일은 PlayerReplicationInfo 를 구하는 것입니다. 아래의 코드는 안전하게 PlayerReplicationInfo 를 구하는 방법을 보여줍니다. 3323와 그 이후 빌드에는 베이스 클래스에 의해 이미 이것이 실행되어 있습니다.

var PlayerController PlayerOwner;
...


simulated function PostBeginPlay()
{
   Super.PostBeginPlay();
   PlayerOwner = PlayerController(Owner);
}


function DrawHUD(canvas Canvas)
{
   local Pawn PawnOwner;
   local PlayerReplicationInfo PawnOwnerPRI;
   ...

   // PlayerOwner 구하기
    if (PlayerOwner.ViewTarget == PlayerOwner)
   {
      PawnOwner = PlayerOwner.Pawn;
   }
   else if (PlayerOwner.ViewTarget.IsA('Pawn') && Pawn(PlayerOwner.ViewTarget).Controller != None)
   {
      PawnOwner = Pawn(PlayerOwner.ViewTarget);
   }
   else if ( PlayerOwner.Pawn != None )
   {
      PawnOwner = PlayerOwner.Pawn;
   }
   else
   {
      PawnOwner = None;
   }

   // PlayerReplication 구하기
   if ((PawnOwner != None) && (PawnOwner.Controller != None))
      PawnOwnerPRI = PawnOwner.PlayerReplicationInfo;
   else
      PawnOwnerPRI = PlayerOwner.PlayerReplicationInfo;

   ...
}

일단 PlayerReplicationInfo 를 구하면 PawnOwnerPRI.Score 에 접근함으로써 이 플레이어의 점수를 구할 수 있습니다.

그 다음 HUD를 그리기 전에 해야할 일은 사용할 폰트를 고르는 것입니다. Unreal에서는 폰트가 까다롭습니다. 크기가 조정되지 않고, 따라서 화면의 해상도를 바탕으로 원하는 폰트를 골라야 하기 때문입니다. 상세한 내용은 HeadsUpDisplay 참조? 와 폰트 튜토리얼?을 참고하십시오. FontArrayNames 배열에 명기된 폰트들은 내림차순으로 되어 있어서 인덱스 번호가 작을수록 폰트 사이즈가 큽니다. 폰트는 주어진 인덱스의 폰트를 반환하는 static function Font LoadFontStatic(int i) 함수를 통해 접근합니다. 다음의 코드 블록은 적절한 크기의 폰트를 고르는 방법을 나타냅니다.

function DrawHUD(canvas Canvas)
{
   ...

   //폰트 사이즈를 구함 (번호가 클수록 작은 폰트)
   if ( Canvas.ClipX <= 640 )
      FontSize=4;
   else if ( Canvas.ClipX <= 800 )
      FontSize=3;
   else if ( Canvas.ClipX <= 1024 )
      FontSize=2;
   else if ( Canvas.ClipX <= 1280 )
      FontSize=1;
   else if ( Canvas.ClipX <= 1600 )
      FontSize=0;
   else
      FontSize=0;
   Canvas.Font = LoadFontStatic(FontSize);

   ...
}

defaultproperties
{
   ...

   FontArrayNames(0)="ExampleFonts_T.ExampleFont38"
   FontArrayNames(1)="ExampleFonts_T.ExampleFont38"
   FontArrayNames(2)="ExampleFonts_T.ExampleFont34"
   FontArrayNames(3)="ExampleFonts_T.ExampleFont30"
   FontArrayNames(4)="ExampleFonts_T.ExampleFont26"
   FontArrayNames(5)="ExampleFonts_T.ExampleFont22"
   FontArrayNames(6)="ExampleFonts_T.ExampleFont18"
   FontArrayNames(7)="ExampleFonts_T.ExampleFont14"
   FontArrayNames(8)="ExampleFonts_T.ExampleFont10"

   ...
}

이제 Score 와 폰트가 갖춰졌으므로 실제로 HUD에 뭔가 그릴 수 있습니다. 아래의 코드 블록은 오른 쪽 아래 구석에 현재의 해상도를 바탕으로 크기가 조정된 텍스처를 그린 다음, 그 텍스처의 위에 현재의 점수를 그립니다. 점수 텍스트는 수동으로 StrLen 함수를 사용하여 오른쪽에 정렬됩니다. 아래에서 HUD를 그리기 위해 사용된 함수들에 대한 자세한 내용은 캔버스 참조? 문서를 참고하십시오.

function DrawHUD(canvas Canvas)
{
   ...

   // Hud를 그림

   Canvas.SetDrawColor(255,255,255);
   HUDScoreScaleX = Canvas.ClipX/800;
   HUDScoreScaleY = Canvas.ClipY/600;
   HUDScorePosX = Canvas.ClipX - 256*HUDScoreScaleX;
   HUDScorePosY = Canvas.ClipY - 128*HUDScoreScaleY;
   Canvas.SetPos(HUDScorePosX, HUDScorePosY);
   Canvas.DrawTileScaled(HUDScoreBox, HUDScoreScaleX, HUDScoreScaleY);

   Canvas.SetDrawColor(230,240,240);
   Score = PawnOwnerPRI.Score;
   ScoreString = ""$Score;
   Canvas.StrLen(ScoreString,XL,YL);
   Canvas.SetPos(HUDScorePosX + 235*HUDScoreScaleX - XL, HUDScorePosY + 70*HUDScoreScaleY);
   Canvas.DrawText(Score);
}

ExampleScoreboard

점수판은 ScoreBoard.uc 에 아래와 같이 정의된 GRI.PRIArray 에 접근함으로써 작용합니다:

var() GameReplicationInfo GRI;

GameReplicationInfo 클래스는 PRIArray 를 다음과 같이 정의합니다:

var() array PRIArray;

PRIArray 는 게임에 있는 모든 플레이어에 대한 복제 정보를 가지고 있습니다. 이 배열을 사용하면 점수판에 표시할 모든 플레이어의 이름과 점수를 얻을 수 있습니다.

실제로 점수판을 그리는 일은 비교적 간단하지만 그릴 것이 많기 때문에 코드가 꽤 깁니다. 코드는 문서화가 잘 되어 있으나, UnrealScript 파일 ExampleScoreboard.uc 을 살펴보는 것이 어떤 일들이 진행되는지 이해하는 가장 좋은 방법일 것입니다. 아래에서 HUD를 그리는데 사용된 함수들에 관한 자세한 내용은 HeadsUpDisplay 참조? 와 Canvas 참조?를 참고하십시오.