UE5/UE5 MultiPlayerGame

MultiPlayerGame) 4. 스팀연결

PJNull 2022. 6. 2.
728x90
반응형

스팀연결

 

 

 

 

1.온라인 서브시스템 활성화

플러그인->온라인 서브시스템 스팀 활성화->UE재시작

 

 

 

 

 

2.코드수정

build.cs에 "OnlineSubsystem","OnlineSubsystemSteam"추가 

PublicDependencyModuleNames.AddRange(new string[] 
{ 
    "Core", 
    "CoreUObject", 
    "Engine", 
    "InputCore", 
    "HeadMountedDisplay"
});
//->서브시스템추가

PublicDependencyModuleNames.AddRange(new string[] 
{ 
    "Core", 
    "CoreUObject", 
    "Engine", 
    "InputCore", 
    "HeadMountedDisplay",
    "OnlineSubsystemSteam",
    "OnlineSubsystem" 
});

 

 

 

 

3.DefaultEngine.ini 수정

 

 

Online Subsystem Steam

An overview of Online Subsystem Steam, including how to set up your project for distribution on Valve's Steam platform.

docs.unrealengine.com

 

 

엔진 맨밑에 코드 추가한다.

[/Script/Engine.GameEngine]
+NetDriverDefinitions=(DefName="GameNetDriver",DriverClassName="OnlineSubsystemSteam.SteamNetDriver",DriverClassNameFallback="OnlineSubsystemUtils.IpNetDriver")

[OnlineSubsystem]
DefaultPlatformService=Steam

[OnlineSubsystemSteam]
bEnabled=true
SteamDevAppId=480

bInitServerOnClient=true

[/Script/OnlineSubsystemSteam.SteamNetDriver]
NetConnectionClassName="OnlineSubsystemSteam.SteamNetConnection"

 

 

※주의

; If using Sessions
; bInitServerOnClient=true

//언리얼 문서의 이부분을 그대로 치면 작동하지 않으므로 

bInitServerOnClient=true
//으로 고쳐적어야함

 

 

 

 

4.온라인 서브시스템 엑세스

 

 

 

IOnlineSubsystem

OnlineSubsystem - Series of interfaces to support communicating with various web/platform layer services

docs.unrealengine.com

 

//Character.h

#include "Interfaces/OnlineSessionInterface.h"
UCLASS(config=Game)

class _Character : public ACharacter
{
 public:

	IOnlineSessionPtr OnlineSessionInterface;
}
//Character.cpp



_Character::_Character()
{
	IOnlineSubsystem* OnlineSubsystem= IOnlineSubsystem::Get();
	if (OnlineSubsystem)
	{
		OnlineSessionInterface=OnlineSubsystem->GetSessionInterface();
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage
            (-1, 15.f, FColor::Red,
			  FString::Printf(TEXT("Online: %s"), 
              *OnlineSubsystem->GetSubsystemName().ToString())
            );
		}
	}

}

 

 

 

 

5.델레게이트와 콜백함수 설정

//Character.h

protected:

	UFUNCTION(BlueprintCallable)
		void CreateGameSession();
        
   	void OnCreateSessionComplete(FName SessionName,bool bWasSuccessful);

        
        
private:
	FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate;

 

 

//Character.cpp

_Character::_Character():
	CreateSessionCompleteDelegate(FOnCreateSessionCompleteDelegate::CreateUObject(this,&ThisClass::OnCreateSessionComplete))

{
    
}

 

 

 

 

6.세션만들기

 

 

FOnlineSessionSettings

Container for all settings describing a single online session

docs.unrealengine.com

 

 

//Character.cpp

#include "OnlineSessionSettings.h"


void _Character::CreateGameSession()
{
	if (!OnlineSessionInterface.IsValid())return;
	auto ExitSession=OnlineSessionInterface->GetNamedSession(NAME_GameSession);
	if (ExitSession != nullptr)OnlineSessionInterface->DestroySession(NAME_GameSession);


	OnlineSessionInterface->AddOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegate);
	
	TSharedPtr<FOnlineSessionSettings>SessionSettings=MakeShareable(new FOnlineSessionSettings());
	
        SessionSettings->bIsLANMatch = false;
	SessionSettings->NumPublicConnections = 4;
	SessionSettings->bAllowJoinInProgress = true;
	SessionSettings->bAllowJoinViaPresence = true;
	SessionSettings->bShouldAdvertise = true;
	SessionSettings->bUsesPresence = true;
	
    	const ULocalPlayer* LocalPlayer=GetWorld()->GetFirstLocalPlayerFromController();
	OnlineSessionInterface->CreateSession(*LocalPlayer->GetPreferredUniqueNetId(),NAME_GameSession,*SessionSettings);

}

 

 

 

 

7.세션 테스트

 

 

//Character.cpp

void _Character::OnCreateSessionComplete(FName SessionName, bool bWasSuccessful)
{
	if (bWasSuccessful)
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Blue,
				FString::Printf(TEXT("Success CreateSession:%s"),*SessionName.ToString()));
		}
	}
	else
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red,
				FString::Printf(TEXT("Faild CreateSession")));
		}
	}
}

 

 

 

빨강색:스팀연결 파랑색:세션생성

728x90
반응형

댓글