레벨이 전환 될때, GameMode가 다르다면 데이터유지에 제약사항이 발생할 경우가 있다.
GameInstance로 할수도 있고.
메모리 또는 저장매체를 이용하여 Save & Load 방식으로 유지 할 수도 있다.
저장해야 되는 데이터 기준 = 초기화 되면 안되는 데이터
UUE11SaveGame* LoadGame = Cast<UUE11SaveGame>(UGameplayStatics::LoadGameFromSlot(TEXT("Save"), 0));
AUE11PlayerState* State = Cast<AUE11PlayerState>(GetPlayerState());
if (IsValid(LoadGame))
{
State->mPlayerInfo = LoadGame->mPlayerInfo;
State->mCameraZoomMin = LoadGame->mCameraZoomMin;
State->mCameraZoomMax = LoadGame->mCameraZoomMax;
}
else
{
//DataTable로 생성하여, 초기값 설정 후 불러오기
}
UUE11SaveGame* SaveGame Cast<UUE11SaveGame>(UGameplayStatics::CreateSaveGameObject(UUE11SaveGame::StaticClass()));
AUE11PlayerState* State = Cast<AUE11PlayerState>(GetPlayerState());
SaveGame->mPlayerInfo = State->mPlayerInfo;
SaveGame->mCameraZoomMin = State->mCameraZoomMin;
SaveGame->mCameraZoomMax = State->mCameraZoomMax;
UGameplayStatics::SaveGameToSlot(SaveGame, TEXT("Save"), 0);
FPaths:: 안에 다양한 경로가 지정되어있다.
FString FullPath = FPaths::ProjectSavedDir()+ TEXT("SaveGames/Save.txt");
FArchive* Writer = IFileManager::Get().CreateFileWriter(*FullPath);
if (Writer)
{
}
PublicDependencyModuleNames.AddRange(new string[]
{
"Core"
, "CoreUObject"
, "Engine"
, "InputCore"
, "AIModule"
, "GameplayTasks"
, "NavigationSystem"
, "UMG"
, "Niagara"
, "MovieScene"
, "LevelSequence"
, "MediaAssets"
, "Networking"
, "Sockets"
});
#include "Networking.h"
#include "Sockets.h"
#include "SocketSubsystem.h"
#include "HAL/Runnable.h"
#include "HAL/RunnableThread.h"
부모 클래스 : 없음
이름 : NetworkManager
경로 : Network/
부모 클래스 : 없음
이름 : NetworkSession
경로 : Network/
부모 클래스 : 없음
이름 : PacketStream
경로 : Network/
#pragma once
#include "../GameInfo.h"
/**
* Network Singleton으로 설계.
*/
class UE11_API CNetworkManager
{
private:
CNetworkManager();
~CNetworkManager();
public:
bool Init();
private:
static CNetworkManager* mInst;
public:
static CNetworkManager* GetInst()
{
if (!mInst)
mInst = new CNetworkManager;
return mInst;
}
static void DestroyInst()
{
if (mInst)
{
delete mInst;
mInst = nullptr;
}
}
};
#include "NetworkManager.h"
CNetworkManager* CNetworkManager::mInst = nullptr;
CNetworkManager::CNetworkManager()
{
}
CNetworkManager::~CNetworkManager()
{
}
bool CNetworkManager::Init()
{
return true;
}
#pragma once
#include "../GameInfo.h"
/**
*
*/
class UE11_API CNetworkSession
{
public:
CNetworkSession();
~CNetworkSession();
private:
FSocket* mSocket;
bool mConnect;
uint8 mReceivePacket[PACKET_SIZE];
uint8 mSendPacket[PACKET_SIZE];
public:
bool Connect(const FString& IPAddr, int32 Port);
void Close();
bool Receive(int32& PacketHeader, int32& Length, uint8* Packet);
bool Send(int32 PacketHeader, int32 Length, uint8* Packet);
};
#include "NetworkSession.h"
#include "PacketStream.h"
CNetworkSession::CNetworkSession() :
mSocket(nullptr),
mConnect(false),
mReceivePacket{},
mSendPacket{}
{
}
CNetworkSession::~CNetworkSession()
{
Close();
}
bool CNetworkSession::Connect(const FString& IPAddr, int32 Port)
{
// 소켓을 생성한다.
mSocket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(
NAME_Stream, TEXT("Default"), false);
// 문자열로 들어온 IP와 Port를 이용해서 주소 객체를 만들어준다.
FIPv4Address ip;
FIPv4Address::Parse(IPAddr, ip);
TSharedRef<FInternetAddr> Addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
Addr->SetIp(ip.Value);
Addr->SetPort(Port);
mConnect = mSocket->Connect(*Addr);
return true;
}
void CNetworkSession::Close()
{
if (mConnect)
{
mSocket->Close();
delete mSocket;
}
}
bool CNetworkSession::Receive(int32& PacketHeader, int32& Length,
uint8* Packet)
{
if (!mConnect)
return false;
bool Result = mSocket->Recv(mReceivePacket, PACKET_SIZE, Length,
ESocketReceiveFlags::None);
if (!Result || Length == 0)
return false;
CPacketStream stream;
stream.SetBuffer(mReceivePacket);
stream.GetData(&PacketHeader, 4);
stream.GetData(&Length, 4);
stream.GetData(Packet, Length);
return true;
}
bool CNetworkSession::Send(int32 PacketHeader, int32 Length,
uint8* Packet)
{
if (!mConnect)
return false;
CPacketStream stream;
stream.SetBuffer(mSendPacket);
stream.AddData(&PacketHeader, 4);
stream.AddData(&Length, 4);
stream.AddData(Packet, Length);
return mSocket->Send(mSendPacket, stream.GetLength(), Length);
}
#pragma once
#include "../GameInfo.h"
/**
*
*/
class UE11_API CPacketStream
{
public:
CPacketStream();
~CPacketStream();
private:
uint8* mBuffer;
int32 mLength;
public:
void SetBuffer(uint8* Buffer)
{
mBuffer = Buffer;
}
int32 GetLength()
{
return mLength;
}
public:
void AddData(void* Data, int32 Size);
void GetData(void* Data, int32 Size);
};
#include "PacketStream.h"
CPacketStream::CPacketStream() :
mBuffer(nullptr),
mLength(0)
{
}
CPacketStream::~CPacketStream()
{
}
void CPacketStream::AddData(void* Data, int32 Size)
{
FMemory::Memcpy(mBuffer + mLength, Data, Size);
mLength += Size;
}
void CPacketStream::GetData(void* Data, int32 Size)
{
FMemory::Memcpy(Data, mBuffer + mLength, Size);
mLength += Size;
}