Joining a Game
From the Main Menu, clicking the Find games button will start the script inside the MainMenu Widget Blueprint below.
The OnClicked Event here is tied to the Button specified as the "Find games" button on the Designer tab of the MainMenu Widget Blueprint. When fired, the game instance is Cast to MyGameInstance Blueprint which allows us to access it and call the Show Server List Event inside it.
The ShowServerListEvent starts off by running the TransitionToState Macro (1) which sets the game state to Sever List. From there, the IsValid (2) check occurs against the Server List Widget Blueprint variable, where if valid uses the Add to Viewport (5) node to display the Server List menu onscreen. Upon first run however, the Server List screen needs to be created using the Create Widget (3) node, then promoted to a variable (4) before displaying it onscreen. Once the screen has been set to displayed, the Set Input Mode UIOnly node sets the input method to only UI.
During (5) above and before (6) and actually displaying the Server List Menu, additional script is executed inside the ServerList Widget Blueprint which is used to populate the Server List menu with any active games. As seen below inside the Server List Widget Blueprint, the Event Construct node is used and fired off when the Widget Blueprint is called to be constructed (created) and immediately executes a created macro called RefreshListMacro.
The RefreshListMacro performs quite a few tasks, the first section of which is illustrated below.
Above when the macro is called, the first thing it does is set a Boolean variable called Refresh Button Enabled to False (1) which disables the Refresh Button and prevents the player from being able to press it. Next, a variable called Status Text is set to Searching... (2) and its Visibility is set to Visible (3) which allows the Widget holding the Status Text to be updated and displayed.
In (4) an array variable called Found Session Widgets, which is a collection of ServerRow Widgets used to populate the Server List, each one that exists is removed using the Remove Child (5) node. This is effectively removing any servers that are to be listed when the screen is displayed. In (6), the Found Session Widgets variable is also cleared removing any previously stored sessions, so that everything is wiped clean before performing a new search.
Next, the macro gets the game instance and uses the Cast To MyGameInstance node (1 below) in order to access whether Enable LAN is true or not (which is used when searching for sessions).
The Find Sessions node (2) is then used to discover any sessions that exist (which are output to the Results value). Also on this node, you can set the Max Results (number of search results to return). The results from the Find Sessions query are assigned and stored in an array variable called Found Sessions (3). A Branch is then performed (4) to check the Results, and if equal to 0, the True pin is executed (if not, the False is executed). If for any reason there is a failure when using the Find Sessions node, the OnFailure event is called and changes the Status Text that is displayed to Search failed (5), informing the user that the search for sessions did not complete.
Off of the Branch (4 in the previous image), if True (which means no sessions were found) the Status Text is set to No sessions found (7 below) and the Refresh Button Enabled variable is set to True (8) so that the player can press the Refresh button to attempt to search for sessions again.
If sessions are found, the Status Text Visibility is set to Collapsed (1) so that it is hidden from the player. A ForEachLoop node (2) is then used to take each of the Results found and, using the Create ServerRow node (3), create a Server Row Widget for each of them. These will be the individual rows of sessions found in the server list.
In (4), the Results are also added to the Found Session Widgets array before calling the Set Search Result Function inside the ServerRow Widget Blueprint (which used to generate the Server Name, Player Count, and Ping to be displayed). After setting the content to be displayed in the ServerRow Widget Blueprint with the Set Search Result node, the Add Child node (6) is used to add each of the ServerRow Widget Blueprints created to a Scrolling Servers scroll box that is displayed in the Server List menu.
After each of the sessions found has had a ServerRow widget created, populated and added to the Scrolling Servers widget, the ForEachLoop is completed and the Referesh Button Enabled variable is set to True (8).
After having run through the RefreshListMacro to search for sessions, any sessions found will be displayed in the Server List menu.
Clicking the Refresh button will run the RefreshListMacro again to search for sessions. Pressing the Back button will execute the ShowMainMenuEvent custom event (which is outlined in the Startup section).
Clicking on a server displayed in the server list will fire off the OnClicked event in the ServerRow Widget Blueprint (see below).
The OnClicked Event for a ServerRow Widget, gets the game instance and uses the Cast To MyGameInstance node in order to call the Join from Server List Event inside it. The SearchResult variable (which was generated as part of the RefreshListMacro) is passed to the MyGameInstance Blueprint as well and allows the player to attempt to join the server that they clicked on.
The JoinFromServerListEvent inside the MyGameInstance Blueprint (pictured below), when called first performs the ShowLoadingScreen custom event outlined in the Hosting a Game section.
While the loading screen is being displayed, the Join Session node is used to attempt to join the Search Result provided from the ServerRow Widget Blueprint the player clicked on to attempt to join. If successful, the player will join the server they selected and spawn into that game. If there are any errors, the DisplayErrorDialog is run and the Error Message of Failed to join session is displayed.