So lets update the SearchScreen.cs to display a bit more info like "Searching..." and "Unable to find a Session" and a "Back" link etc..
Lets move out the searching of a session from the Constructor into its own function so that we can call it from the menu when a user clicks search:
public SearchScreen(NetworkSessionType networkSessionType)
{
this.networkSessionType = networkSessionType;
Search();
}Search();
/// <summary>
/// Searches for available network sessions
/// </summary>
private void Search()
{
MenuEntries.Clear();
MenuEntries.Add("Searching...");
// Disable movement
this.isEnabled = false;
NetworkSessionProperties searchProperties = new NetworkSessionProperties();
if (this.networkSessionType == NetworkSessionType.PlayerMatch)
{
if (this.networkSessionType == NetworkSessionType.SystemLink)
{
}MenuEntries.Add("Searching...");
// Disable movement
this.isEnabled = false;
NetworkSessionProperties searchProperties = new NetworkSessionProperties();
if (this.networkSessionType == NetworkSessionType.PlayerMatch)
{
LIVESearch = NetworkSession.BeginFind(NetworkSessionType.PlayerMatch, maxLocalPlayers, searchProperties,
new AsyncCallback(SessionsFound), null);
}new AsyncCallback(SessionsFound), null);
if (this.networkSessionType == NetworkSessionType.SystemLink)
{
LIVESearch = NetworkSession.BeginFind(NetworkSessionType.SystemLink, maxLocalPlayers, searchProperties,
new AsyncCallback(SessionsFound), null);
}new AsyncCallback(SessionsFound), null);
/// <summary>
/// Responds to user menu selections.
/// </summary>
protected override void OnSelectEntry(int entryIndex)
{
switch (entryIndex)
{ case 0:
Search();
break;
case 1:
}{ case 0:
Search();
break;
// Exit
ScreenManager.AddScreen(new XboxLiveScreen(this.networkSessionType));
break;
ScreenManager.AddScreen(new XboxLiveScreen(this.networkSessionType));
break;
}
Modify the SessionFound callback function to re-enable the menu movement and also display menu entries if the available sessions is zero:
public void SessionsFound(IAsyncResult result)
{
// Re-enable movement in the menu
this.isEnabled = true;
if ((LIVESearch != null) && LIVESearch.IsCompleted)
{
}this.isEnabled = true;
if ((LIVESearch != null) && LIVESearch.IsCompleted)
{
availableSessions = NetworkSession.EndFind(result);
MenuEntries.Clear();
if (availableSessions != null)
{
LIVESearch = null;
}MenuEntries.Clear();
if (availableSessions != null)
{
foreach (AvailableNetworkSession availableSession in availableSessions)
{
// Check if no sessions were found
if (availableSessions.Count == 0)
{
}{
// Only show sessions where there are available slots
if (availableSession.CurrentGamerCount < 8)
{
// Limit the number of results to return
if (MenuEntries.Count >= 5)
{
}if (availableSession.CurrentGamerCount < 8)
{
MenuEntries.Add(availableSession.HostGamertag + " (" +
availableSession.CurrentGamerCount.ToString() + "/8)");
}availableSession.CurrentGamerCount.ToString() + "/8)");
// Limit the number of results to return
if (MenuEntries.Count >= 5)
{
break;
}// Check if no sessions were found
if (availableSessions.Count == 0)
{
MenuEntries.Add("No results found, try again?");
MenuEntries.Add("Back");
}MenuEntries.Add("Back");
LIVESearch = null;
Give it a try, im still unable to test with available sessions so let me know if you have a problem with that bit. We still need to make it that if you select an available session it will try join the session.
1 comment:
Thanks for the example code! Hadn't seen anything else that showed how to do it asynchronously.
Btw: random & unimportant, but unless I'm missing something, you could replace:
[code]
if (this.networkSessionType == NetworkSessionType.PlayerMatch)
{
LIVESearch = NetworkSession.BeginFind(NetworkSessionType.PlayerMatch, maxLocalPlayers, searchProperties,
new AsyncCallback(SessionsFound), null);
}
if (this.networkSessionType == NetworkSessionType.SystemLink)
{
LIVESearch = NetworkSession.BeginFind(NetworkSessionType.SystemLink, maxLocalPlayers, searchProperties,
new AsyncCallback(SessionsFound), null);
}
[/code]
With:
[code]
LIVESearch = NetworkSession.BeginFind(this.networkSessionType, maxLocalPlayers, searchProperties,
new AsyncCallback(SessionsFound), null);
[/code]
Thanks again for the code!
- @BlueLineGames
Post a Comment