How to get the number of players in a session?

Hi, I’m stuck with this right now. I’m making a multiplayer game and I want the player who’s joining a game to know how many players are currently in the session. What I’m doing is when looking for sessions to join, I create a button for each of them so the player can click and join any session, but what I want is to hide or not create buttons for the sessions that are full, so that’s why I need the number.

The game is LAN only and it’s a listen server.

Any help would be awesome! :smiley:

Thanks for your time.

EDIT: I haven’t noticed this was in C++ section, not in Blueprints at first glance. :slight_smile:

Leaving answer here in case someone else has use for this. Apologies for wrong posting.

Type “get player array” in your GameState blueprint. It should give some info about connected players.

In a project I work on however, I created a (replicated) PlayerController array in GameMode and using Event OnPostLogin to get info for each connected player. I spawn a camera pawn each time this event is fired. I “push” the display name of the controller to a TextRender component that is a child of the camera pawn. Also I make sure that the last logged in controller (player) possesses the latest spawned camera.

Anyway, I am going out of topic fast here :slight_smile:

The thing is, I track connected players via a replicated PlayerController array that I’ve created in GameMode class.

Check out the screens, my “build” may not be the best way to accomplish this. Heck, it may have flaws even :slight_smile:
Also don’t forget to track logging out players (deleting them from array) via Event OnLogout in case you go this way.

1 Like

Thank you for your answer but I think this is not what I’m looking for. What I want something similar to what the MultiplayerShootout example has here:

Essentially if the session found is full, like in this example, I want to not create the button. The problem is that I’m handling all this through C++ and I can’t find the equivalent to this node basically:

84466-getcurrentplayers.png

All of my session handling code I got it from eXi’s tutorial here:
[A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums][3]

And modified some of the functions so I could do the button creation like this:

void UMyGameInstance::OnFindSessionsComplete(bool bWasSuccessful)
{
IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::Get();
if (OnlineSub)
{
IOnlineSessionPtr Sessions = OnlineSub->GetSessionInterface();
if (Sessions.IsValid())
{
Sessions->ClearOnFindSessionsCompleteDelegate_Handle(OnFindSessionsCompleteDelegateHandle);
if (!bWasSuccessful)
return;
if (SessionSearch->SearchResults.Num() > 0)
{
for (int32 SearchIdx = 0; SearchIdx < SessionSearch->SearchResults.Num(); SearchIdx++)
{
FString sessionName = SessionSearch->SearchResults[SearchIdx].Session.SessionSettings.Settings.FindRef(“SESSION_NAME”).Data.ToString();
FSessionSearchResult newResult;
newResult.name = sessionName;
newResult.searchResult = SessionSearch->SearchResults[SearchIdx];
SessionSearchResults.Add(newResult);
}
}
for (int32 i = 0; i < SessionSearchResults.Num(); ++i)
{
for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)
{
ABaS_MainMenuController* controller = Cast<ABaS_MainMenuController>(Iterator->Get());

    					UScrollBox* scrollBox = Cast<UScrollBox>(controller->joinableGamesHUD->GetWidgetFromName(FName(TEXT("JoinButtonArea"))));
    					scrollBox->AddChild(controller->CreateGameButton(SessionSearchResults[i], SessionSearchResults[i].name));
    				}
    			}
    		}
    	}
}

But I can’t find how to get the number of players

You can find number of players if you get the game mode, but it’ll only work on the server.

GetWorld()->GetAuthGameMode()->GetNumPlayers()

Well, if you have a C++ project, you can typically right-click a blueprint node and ‘go to code’.

However, if you simply hover over the input for Get Current Players, it says it takes a Blueprint Session Result struct.
Ctrl+Shift+F for that returns FBlueprintSessionResult found in FindSessionsCallbackProxy.h
Alternatively, you could Ctrl+Shift+F for GetCurrentPlayers, which takes you to the same file, a little further down. In the implementation file we find:

int32 UFindSessionsCallbackProxy::GetCurrentPlayers(const FBlueprintSessionResult& Result)
{
	return Result.OnlineResult.Session.SessionSettings.NumPublicConnections - Result.OnlineResult.Session.NumOpenPublicConnections;
}
1 Like

@anonymous_user_36527dde

"In a project I work on however, I
created a (replicated)
PlayerController array in GameMode "

just wondering why u need the array to be replicated???
it only will exist on server. then why need it to replicate?

int32 UFindSessionsCallbackProxy::GetCurrentPlayers(const FBlueprintSessionResult& Result)
{
return Result.OnlineResult.Session.SessionSettings.NumPublicConnections - Result.OnlineResult.Session.NumOpenPublicConnections;

}

https://github.com/EpicGames/UnrealEngine/blob/7d9919ac7bfd80b7483012eab342cb427d60e8c9/Engine/Plugins/Online/OnlineSubsystemUtils/Source/OnlineSubsystemUtils/Classes/FindSessionsCallbackProxy.h

https://github.com/EpicGames/UnrealEngine/blob/7d9919ac7bfd80b7483012eab342cb427d60e8c9/Engine/Plugins/Online/OnlineSubsystemUtils/Source/OnlineSubsystemUtils/Private/FindSessionsCallbackProxy.cpp

this is how I understand it

NumOpenPublicConnections gives u the number of available connections

NumPublicConnections gives u the max players

so subtract NumPublicConnections - NumOpenPublicConnections + add one for server if ur in LAN

to get the current players

int32 Num = Result.Session.SessionSettings.NumPublicConnections - Result.Session.NumOpenPublicConnections + 1;

1 Like