https://github.com/vahabahmadvand/AngelScriptSessionExample
- Get
UScriptSessionSubsystemsubsystem, and bind events
SessionSubsystem = UScriptSessionSubsystem::Get();
if (SessionSubsystem != nullptr)
{
SessionSubsystem.OnCreateSessionCompleteEvent.AddUFunction(this, n"OnCreateSession");
SessionSubsystem.OnFindSessionsCompleteEvent.AddUFunction(this, n"OnFindSession");
}- Having two buttons for hosting and joining
- On Host button click, call
CreateSessionfunction.
if (System::IsValid(SessionSubsystem))
{
SessionSubsystem.CreateSession(NumPublicConnections, false);
}- In the
OnCreateSessionfunction, callServerTravelto load the map and listen for the other client to join.
UFUNCTION()
private void OnCreateSession(bool bSuccessful)
{
FString MapName = "ThirdPersonMap?listen";
if (bSuccessful)
{
GetWorld().ServerTravel(MapName, true, false);
}
else
{
Log("Failed to create session!");
}
}- On the Join button click, call
FindSessions
if (System::IsValid(SessionSubsystem))
{
SessionSubsystem.FindSessions(100, false);
}- In the
OnFindSessionfunction, iterate throughSessionResultssessions, then join the active session.- p.s. You can implement a listbox that includes all the sessions for the user to choose from.
UFUNCTION()
private void OnFindSession(const TArray<FScriptSessionResult>&in SessionResults, bool bWasSuccessful)
{
if (System::IsValid(SessionSubsystem))
{
for (auto Result : SessionResults)
{
SessionSubsystem.JoinGameSession(Result);
return;
}
}
if (!bWasSuccessful || SessionResults.Num() == 0)
{
Log("Failed to find session!");
}
}