Is there a way to figure out how many PlayerSession objects were returned from a call to DescribePlayerSessions? (UE4)

Recently, I was looking through the DescribePlayerSessionsResult.h file in the GameLiftServerSDK plugin for unreal engine and noticed that the GetPlayerSessions method returns a pointer to an array of PlayerSession objects. However, in C/C++, it’s not possible to determine the size of an array from a pointer to one of its elements, so I was wondering if there was a way to get the number of PlayerSession objects returned from a call to the DescribePlayerSessions method without modifying the original plugin code? I noticed the GetNextToken, SetNextToken, and WithNextToken functions but I was not sure how they worked.

My use case is specifically for when a backfill request gets fulfilled successfully, and then periodically checking if there are any player sessions left in RESERVED state in the game session in order for the server to know when to make a new backfill request if needed.

Thanks in advance.

You will never be able to get all the player sessions that can be matched as this is a paginated API. It will limit the results per page to 10 or some max limit (you can control the page size between this). You would always have to iterate through all the pages to get the total number of player sessions via this API.

You will get a next token in the response to the DescribePlayerSessions. You need to need pass it (uising the SetNextToken, WithNextToken) on subsequent calls to get the next page of information.

However, its probably possible to modify the response to get the number of results actually returned in page request. I can take a quick look later today.

BTW You can check to see if the next token is NULL, if its then you have <= limit, if its not null you have == limit results. So you could somewhat hack your count without having to look at all the results but the assumption is, for this API, you care about the results too.

Hey Pip, thanks for the response and taking a look.

With regards to modifying the code to achieve this use case, I do believe that I can make a new method in the DescribePlayerSessionsResult.h file like so,

inline int GetPlayerSessionCount() {return m_playerSessions_count;}

I’m not entirely sure if this will work but I’ll try testing this out.

And I have thought about the last method you suggested, but like you said, next token will be null even if you have results returned, so it’s still difficult to know if no player sessions are returned for example.

This is probably out of my scope but have the developers of the GameLift Server SDK thought about using built-in Unreal Engine variable types such as TArray and FString instead of standard arrays and char[] for example? I noticed that in many places in the Server SDK plugin that there are sections of code wrapped in #ifdef GAMELIFT_USE_STD compiler directives for when users want to use the C++ standard library. However, and I could be wrong, many times Unreal devs prefer to not use std in favor of UE4’s standard library for portability reasons. So it’s strange that if you don’t use std, then the plugin won’t use variable types such as TArray, FString, etc. for example, because variables such as std::vector and std::string have useful capabilities that pointers to arrays and char arrays don’t.

Can confirm that adding this method to the DescribePlayerSessionsResult.h file works for this use case,

inline int GetPlayerSessionCount() {return m_playerSessions_count;}

However, I would still suggest, if possible, using more built-in UE4 variable types in the GameLiftServerSDK such as TArray and FString instead of regular arrays and char[] for example.