UE5/UE5 MultiPlayerGame

MultiPlayerGame) 6. 세션에 참여

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

1.로비 레벨 만들기

 

 

먼저 새로운 레벨을 만든다.(파일->새 래벨->이름:Lobby)

그 후 캐릭터와 연결시킨다.

//Character.cpp

void _Character::OnCreateSessionComplete(FName SessionName, bool bWasSuccessful)
{
	if (bWasSuccessful)
		{
			UWorld* world = GetWorld();
			if (world)
				{
				world->ServerTravel(FString("로비레벨의 경로?listen"));
				}
         }
        
}

 

 

2.매치타입 특정하기

 

 

SessionSettings에서 추가 데이터를 지정할 수 있으므로 세션을 찾은 후 확인할 예정이다.

SessionSettings에서 Set을 사용하면 세션을 찾은 후 확인할 수 있는 세션과 관련된 키 값 쌍을 설정할 수 있다.

//Character.cpp

void _Character::CreateGameSession()
{
	SessionSettings->Set(
    		            FName("MatchType"),
                            FString("FreeForAll"),
                            EOnlineDataAdvertisementType::ViaOnlineServiceAndPing
                             );
}

 

 

3.매치타입 체크하기

 

 

이전에 만들어 놨던 OnFindSessionComplete함수에서 유저의 이름과 ID를 확인 할수 있었다. 이것을 사용하여 우리가 찾고 있는 데이터인지를 확인할 것이다.

 

//Character.cpp

void AmultiplayCharacter::OnFindSessionComplete(bool bWasSuccessful)
{
	for (auto Result : SessionSearch->SearchResults)
	{
		FString Id = Result.GetSessionIdStr();
		FString USer = Result.Session.OwningUserName;
		FString MatchType;
		
		Result.Session.SessionSettings.Get(FName("MatchType"),MatchType);
		if(MatchType==FString("FreeForAll"))
		{
			if (GEngine)
			{
				GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Yellow,
				FString::Printf(TEXT("Match Type:%s"),*MatchType));
			
			}
			OnlineSessionInterface->AddOnJoinSessionCompleteDelegate_Handle(JoinSessionCompleteDelegate);

			const ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController();
			OnlineSessionInterface->JoinSession(*LocalPlayer->GetPreferredUniqueNetId(),NAME_GameSession,Result);
		}
    
   	 }
}

 

 

4.IP주소 얻기

 

 

만약 매치타입이 맞았다면 Sessioninterface에서 JoinSession을 호출할것이다. 그를 위해서 델레게이트와 콜백을 설정해야된다.

//Character.h


protected:
	void OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result);
	//콜백함수 추가

private:
	FOnJoinSessionCompleteDelegate JoinSessionCompleteDelegate;
	//델레게이트 추가
//Character.cpp

_Character::AmultiplayCharacter() :
JoinSessionCompleteDelegate(FOnJoinSessionCompleteDelegate::CreateUObject(this,&ThisClass::OnJoinSessionComplete))
//델레게이트 설정
{
}



void _Character::OnFindSessionComplete(bool bWasSuccessful)
{
	for (auto Result : SessionSearch->SearchResults)
	{
		if(MatchType==FString("FreeForAll"))
		{
        	OnlineSessionInterface->AddOnJoinSessionCompleteDelegate_Handle(JoinSessionCompleteDelegate);
			const ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController();
			OnlineSessionInterface->JoinSession(*LocalPlayer->GetPreferredUniqueNetId(),NAME_GameSession,Result);
		
		}
    
   	 }
}


void _Character::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
{
	if (!OnlineSessionInterface.IsValid())return;

	FString Adress;
	if (OnlineSessionInterface->GetResolvedConnectString(NAME_GameSession, Adress))
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::White,
				FString::Printf(TEXT("Connect IP:%s"), *Adress));

		}
	}
}

 

 

5.세션에 참여하기

 

//Chracter.cpp


void _Character::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
{
	if (!OnlineSessionInterface.IsValid())return;

	if (OnlineSessionInterface->GetResolvedConnectString(NAME_GameSession, Adress))
	{
    //추가
    		APlayerController* PlayerController = GetGameInstance()->GetFirstLocalPlayerController();
		if (PlayerController)
		{
			PlayerController->ClientTravel(Adress,ETravelType::TRAVEL_Absolute);
		}
	}
    //추가
}
728x90
반응형

댓글