[ CH4_Unreal Engine 멀티플레이어 게임 개발 ]
팀이 다시 짜여졌고
처음은 개인 과제로 시작을 하게 되었다.
녹화 강의를 통해 공부를 하는 시간이고
배운 내용을 적게 되었다.
프로젝트 이름(CFM : Chapter Four Multi)의 이름을 사용해서
생성하는 모든 파일들의 말머리에 CFM을 붙히는 규칙으로 정하였다.
[ 채팅 프로젝트 생성 ]
< CFMGameModeBase >
더보기
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "CFMGameModeBase.generated.h"
/**
*
*/
UCLASS()
class CFM_API ACFMGameModeBase : public AGameModeBase
{
GENERATED_BODY()
};
기본적으로 GameMode를 Custom을 할 수 있게 GameModeBase를 상속받아 C++ 파일을 생성하여 작업을 시작
< CFMPlayerController >
더보기
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "CFMPlayerController.generated.h"
/**
*
*/
class UCFMChatInput;
// 전방 선언으로 헤더 파일에 해당 내용들을 선언해도 문제생기지 않게 작성
UCLASS()
class CFM_API ACFMPlayerController : public APlayerController
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
void SetChatMessageString(const FString& InChatMessageString);
void PrintChatMessageString(const FString& InChatMessageString);
protected:
UPROPERTY(EditDefaultsOnly)
TSubclassOf<UCFMChatInput> ChatInputWidgetClass;
// WBP_ChatInput 과 연결
UPROPERTY()
TObjectPtr<UCFMChatInput> ChatInputWidgetInstance;
FString ChatMessageString;
// 채팅 내용
};
#include "Player/CFMPlayerController.h"
#include "UI/CFMChatInput.h"
#include "Kismet/KismetSystemLibrary.h"
void ACFMPlayerController::BeginPlay()
{
Super::BeginPlay();
// 입력 할당
FInputModeUIOnly InputModeUIOnly;
SetInputMode(InputModeUIOnly);
// ChatInputWidgetClass가 변수로 선언 돼있을 때 WBP_ChatInput을 연결
if (IsValid(ChatInputWidgetClass) == true)
{
ChatInputWidgetInstance = CreateWidget<UCFMChatInput>(this, ChatInputWidgetClass);
if (IsValid(ChatInputWidgetInstance) == true)
{
// 입력 내용을 Viewport에 출력할 수 있게 설정
ChatInputWidgetInstance->AddToViewport();
}
}
}
// ChatMessageString을 함수의 인자값으로 대입 후 출력
void ACFMPlayerController::SetChatMessageString(const FString& InChatMessageString)
{
ChatMessageString = InChatMessageString;
PrintChatMessageString(ChatMessageString);
}
// 화면에 String을 출력
void ACFMPlayerController::PrintChatMessageString(const FString& InChatMessageString)
{
UKismetSystemLibrary::PrintString(this, ChatMessageString, true, true, FLinearColor::Red, 5.0f);
}
< CFMChatInput >
더보기
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "CFMChatInput.generated.h"
class UEditableTextBox;
UCLASS()
class CFM_API UCFMChatInput : public UUserWidget
{
GENERATED_BODY()
public:
// WBP_ChatInput 의 팔레트 안에 있는 'EditableTextBox_ChatInput' 에 바인딩
UPROPERTY(meta=(BindWidget))
TObjectPtr<UEditableTextBox> EditableTextBox_ChatInput;
virtual void NativeConstruct() override;
virtual void NativeDestruct() override;
protected:
UFUNCTION()
void OnChatInputTextCommitted(const FText& Text, ETextCommit::Type CommitMethod);
};
#include "UI/CFMChatInput.h"
#include "Components/EditableTextBox.h"
#include "Player/CFMPlayerController.h"
// WBP_ChatInput 의 OnTextCommitted 이벤트와 Delegate로 바인딩 확인/연결
void UCFMChatInput::NativeConstruct()
{
Super::NativeConstruct();
if (EditableTextBox_ChatInput->OnTextCommitted.IsAlreadyBound(this, &ThisClass::OnChatInputTextCommitted) == false)
{
EditableTextBox_ChatInput->OnTextCommitted.AddDynamic(this, &ThisClass::OnChatInputTextCommitted);
}
}
// WBP_ChatInput 의 OnTextCommitted 이벤트 종료 시 바인딩 해제
void UCFMChatInput::NativeDestruct()
{
Super::NativeDestruct();
if (EditableTextBox_ChatInput->OnTextCommitted.IsAlreadyBound(this, &ThisClass::OnChatInputTextCommitted) == true)
{
EditableTextBox_ChatInput->OnTextCommitted.RemoveDynamic(this, &ThisClass::OnChatInputTextCommitted);
}
}
// Enter와 같은 키로 텍스트를 제출 했을 때 GetOwningPlayer 함수를 통해서 제출을 요청한 컴퓨터의 PlayerController를 가져오고
// 유효 했을 때 Cast 후 ChatMessageString 내용을 EditableTextBox_ChatInput 여기에 출력시키는 함수
// 이 후 빈 텍스트(FText())로 내용 지우기
void UCFMChatInput::OnChatInputTextCommitted(const FText& Text, ETextCommit::Type CommitMethod)
{
if (CommitMethod == ETextCommit::OnEnter)
{
APlayerController* OwningPlayerController = GetOwningPlayer();
if (IsValid(OwningPlayerController) == true)
{
ACFMPlayerController* OwningCFMPlayerController = Cast<ACFMPlayerController>(OwningPlayerController);
if (IsValid(OwningCFMPlayerController) == true)
{
OwningCFMPlayerController->SetChatMessageString(Text.ToString());
EditableTextBox_ChatInput->SetText(FText());
}
}
}
}
[ 스크럼 ]
더보기


< 오전 스크럼 >

< 오후 스크럼 >
