UE5/UE5 MultiPlayerGame

MultiPlayerGame) 12. 세션 만들기

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

세션 만들기

 

 

로컬네트워크에서 했던것 처럼 CreateSession의 SessionInterface에 델레게이트 핸들에 델레게이트를 설정하고 만들어 두었던 델레게이트 핸들에 저장한다. 이렇게 하면 나중에 델레게이트 리스트에서 델레게이트를 삭제할수 있다. 그후 세션세팅을 설정해준다.

 

 

세션만들기 참조하기

 

MultiPlayerGame) 4. 스팀연결

스팀연결 1.온라인 서브시스템 활성화 플러그인->온라인 서브시스템 스팀 활성화->UE재시작 2.코드수정 build.cs에 "OnlineSubsystem","OnlineSubsystemSteam"추가 PublicDependencyModuleNames.AddRange(new str..

pjnull.tistory.com

 

 

 

 

 

1.CreateSession 구현

 

//MultiplayerSessionSubsystem.h


private:
	TSharedPtr<FOnlineSessionSettings>LastSessionsettings;
//MultiplayerSessionSubsystem.cpp

#include "OnlineSessionSettings.h"



void UMultiplayerSessionSubsystem::CreateSession(int32 NumPublicConnections, FString MatchType)
{
	CreateSessionCompleteDelegateHandle=SessionInterface->AddOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegate);
		
    LastSessionsettings = MakeShareable(new FOnlineSessionSettings());
	LastSessionsettings->bIsLANMatch=IOnlineSubsystem::Get()->GetSubsystemName()=="NULL"?true:false;
	LastSessionsettings->NumPublicConnections= NumPublicConnections;
	LastSessionsettings->bAllowJoinInProgress = true;
	LastSessionsettings->bAllowJoinViaPresence = true;
	LastSessionsettings->bShouldAdvertise = true;
	LastSessionsettings->bUsesPresence = true;
	LastSessionsettings->Set(FName("MatchType"), MatchType, EOnlineDataAdvertisementType::ViaOnlineServiceAndPing);
	LastSessionsettings->BuildUniqueId = 1;

    const ULocalPlayer* LocalPlayer=GetWorld()->GetFirstLocalPlayerFromController();
	if(!SessionInterface->CreateSession(*LocalPlayer->GetPreferredUniqueNetId(), NAME_GameSession, *LastSessionsettings))
	{
		SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegateHandle);
	
		MultiplayerOnCreateSessionComplete.Broadcast(false);
	}
}

 

 

2.로비 레벨로 이동하기

 

//Menu.cpp

void UMenu::HostBtnClick()
{
	if (MultiplayerSessionSubsystem)
	{
		MultiplayerSessionSubsystem->CreateSession(NumPublicConnections,FString("FreeForAll"));
		UWorld* World=GetWorld();
        if(World)
        {
        	World->ServerTravel("로비의 경로?listen");
        }
    
    	
	}
}

이렇게 하면 호스트가 방을 만들고 로비로 이동하게된다. 하지만 로비로 이동해도 움직이지 못한다. 왜냐하면 InputMode가 아직 UIOnly InputMode로 되어있기 때문이다.그렇기에 UI를 제거하고 InputMode를 GameOnly로 바꿔준다.

이는 레벨이 월드에서 제거될때 호출해준다.

//Menu.h


protected:

	virtual void OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld)override;
	//레벨 제거시 MenuTearDown호출
private:
	void MenuTearDown();//UI제거+InputMode변경
//Menu.cpp


void UMenu::MenuTearDown()
{
	RemoveFromParent();
	UWorld* World = GetWorld();
	if (World)
	{
		APlayerController* PlayerController = World->GetFirstPlayerController();
		if (PlayerController)
		{
			FInputModeGameOnly inputModeData;
			PlayerController->SetInputMode(inputModeData);
			PlayerController->SetShowMouseCursor(false);
		}
	}
}


void UMenu::OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld)
{
	MenuTearDown();
	Super::OnLevelRemovedFromWorld(InLevel,InWorld);

}

 

 

3. MenuSetup에 Input 추가 하기

 

현재까지 설정들을 모두 하드코딩하여 사용해왔다. 하지만 이런경우 유지보수가 다소 어려울수 있기 때문에 하드코딩한 것들을 변수로 바꿔주고 초기값으로 초기화 할 예정이다.

 

//Menu.h
   
   
public:
	UFUNCTION(BlueprintCallable)
		void MenuSetUp(int32 NumberOfPublicConnections=4,FString MatchOfType=FString(TEXT("FreeForAll")), FString LobbyPath=FString(TEXT("/Game/ThirdPersonCPP/Maps/Lobby")));

private:
	int32 NumPublicConnections{4};
	FString MatchType{TEXT("FreeForAll")};
//Menu.cpp

void UMenu::MenuSetUp(int32 NumberOfPublicConnections, FString MatchOfType,FString LobbyPath)
{
	NumPublicConnections = NumberOfPublicConnections;
	MatchType = MatchOfType;
}



void UMenu::HostBtnClick()
{
	if (MultiplayerSessionSubsystem)
	{
		MultiplayerSessionSubsystem->CreateSession(NumPublicConnections,MatchType);
	
	}
}
728x90
반응형

댓글