Example #1
0
    /// <summary>
    /// Finds lobbies based on given parameters using Epic Online Services.
    /// <para>You can get the found lobbies by subscribing to the <see cref="FindLobbiesSucceeded"/> event which gives you a list of <see cref="LobbyDetails"/>.</para>
    /// <para>This process may throw errors. You can get errors by subscribing to the <see cref="FindLobbiesFailed"/> event.</para>
    /// </summary>
    /// <param name="maxResults">The maximum amount of results to return.</param>
    /// <param name="lobbySearchSetParameterOptions">The parameters to search by. If left empty, then the search will use the default attribute attached to all the lobbies.</param>
    public virtual void FindLobbies(uint maxResults = 100, LobbySearchSetParameterOptions[] lobbySearchSetParameterOptions = null)
    {
        //create search handle and list of lobby details
        LobbySearch search = new LobbySearch();

        //set the search handle
        EOSSDKComponent.GetLobbyInterface().CreateLobbySearch(new CreateLobbySearchOptions {
            MaxResults = maxResults
        }, out search);

        //set search parameters
        if (lobbySearchSetParameterOptions != null)
        {
            foreach (LobbySearchSetParameterOptions searchOption in lobbySearchSetParameterOptions)
            {
                search.SetParameter(searchOption);
            }
        }
        else
        {
            search.SetParameter(new LobbySearchSetParameterOptions {
                ComparisonOp = ComparisonOp.Equal,
                Parameter    = new AttributeData {
                    Key = DefaultAttributeKey, Value = DefaultAttributeKey
                }
            });
        }

        //find lobbies
        search.Find(new LobbySearchFindOptions {
            LocalUserId = EOSSDKComponent.LocalUserProductId
        }, null, (LobbySearchFindCallbackInfo callback) => {
            //if the search was unsuccessful, invoke an error event and return
            if (callback.ResultCode != Result.Success)
            {
                FindLobbiesFailed?.Invoke("There was an error while finding lobbies. Error: " + callback.ResultCode);
                return;
            }

            foundLobbies.Clear();

            //for each lobby found, add data to details
            for (int i = 0; i < search.GetSearchResultCount(new LobbySearchGetSearchResultCountOptions {
            }); i++)
            {
                LobbyDetails lobbyInformation;
                search.CopySearchResultByIndex(new LobbySearchCopySearchResultByIndexOptions {
                    LobbyIndex = (uint)i
                }, out lobbyInformation);
                foundLobbies.Add(lobbyInformation);
            }

            //invoke event
            FindLobbiesSucceeded?.Invoke(foundLobbies);
        });
    }
        /// <summary>
        /// Short SetParameter
        /// </summary>
        /// <param name="search">LobbySearch</param>
        /// <param name="key">Key</param>
        /// <param name="value">Value</param>
        /// <param name="comparisonOp">Compare option</param>
        public static void SetParameter(this LobbySearch search, string key, AttributeDataValue value, ComparisonOp comparisonOp)
        {
            var attr = new AttributeData();

            attr.Key   = key;
            attr.Value = value;
            var paramOp = new LobbySearchSetParameterOptions
            {
                Parameter    = attr,
                ComparisonOp = comparisonOp,
            };
            var result = search.SetParameter(paramOp);

            if (result != Result.Success)
            {
                Debug.LogError($"error {DebugTools.GetClassMethodName()}:{result}");
            }
        }