コード例 #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);
        });
    }
コード例 #2
0
    /// <summary>
    /// Join the given lobby and get the data attached.
    /// <para>You can get the lobby's data by subscribing to the <see cref="JoinLobbySucceeded"/> event which gives you a list of <see cref="Attribute"/>.</para>
    /// <para>This process may throw errors. You can get errors by subscribing to the <see cref="JoinLobbyFailed"/> event.</para>
    /// </summary>
    /// <param name="lobbyToJoin"><see cref="LobbyDetails"/> of the lobby to join that is retrieved from the <see cref="FindLobbiesSucceeded"/> event.</param>
    /// <param name="attributeKeys">The keys to use to retrieve the data attached to the lobby. If you leave this empty, the host address attribute will still be read.</param>
    /// <param name="presenceEnabled">Use Epic's overlay to display information to others.</param>
    public virtual void JoinLobby(LobbyDetails lobbyToJoin, string[] attributeKeys = null, bool presenceEnabled = false)
    {
        //join lobby
        EOSSDKComponent.GetLobbyInterface().JoinLobby(new JoinLobbyOptions {
            LobbyDetailsHandle = lobbyToJoin, LocalUserId = EOSSDKComponent.LocalUserProductId, PresenceEnabled = presenceEnabled
        }, null, (JoinLobbyCallbackInfo callback) => {
            //if the result was not a success, invoke an error event and return
            if (callback.ResultCode != Result.Success)
            {
                JoinLobbyFailed?.Invoke("There was an error while joining a lobby. Error: " + callback.ResultCode);
                return;
            }

            lobbyData.Clear();

            Attribute hostAddress = new Attribute();
            lobbyToJoin.CopyAttributeByKey(new LobbyDetailsCopyAttributeByKeyOptions {
                AttrKey = hostAddressKey
            }, out hostAddress);
            lobbyData.Add(hostAddress);

            if (attributeKeys != null)
            {
                foreach (string key in attributeKeys)
                {
                    Attribute attribute = new Attribute();
                    lobbyToJoin.CopyAttributeByKey(new LobbyDetailsCopyAttributeByKeyOptions {
                        AttrKey = key
                    }, out attribute);
                    lobbyData.Add(attribute);
                }
            }

            LobbyDetails details;
            EOSSDKComponent.GetLobbyInterface().CopyLobbyDetailsHandle(new CopyLobbyDetailsHandleOptions {
                LobbyId = callback.LobbyId, LocalUserId = EOSSDKComponent.LocalUserProductId
            }, out details);

            ConnectedLobbyDetails = details;
            isLobbyOwner          = false;
            ConnectedToLobby      = true;
            currentLobbyId        = callback.LobbyId;

            //invoke event
            JoinLobbySucceeded?.Invoke(lobbyData);
        });
    }
コード例 #3
0
    public virtual void Start()
    {
        lobbyMemberStatusNotifyId = EOSSDKComponent.GetLobbyInterface().AddNotifyLobbyMemberStatusReceived(new AddNotifyLobbyMemberStatusReceivedOptions {
        }, null,
                                                                                                           (LobbyMemberStatusReceivedCallbackInfo callback) => {
            LobbyMemberStatusUpdated?.Invoke(callback);

            if (callback.CurrentStatus == LobbyMemberStatus.Closed)
            {
                LeaveLobby();
            }
        });

        lobbyAttributeUpdateNotifyId = EOSSDKComponent.GetLobbyInterface().AddNotifyLobbyUpdateReceived(new AddNotifyLobbyUpdateReceivedOptions {
        }, null,
                                                                                                        (LobbyUpdateReceivedCallbackInfo callback) => {
            LobbyAttributeUpdated?.Invoke(callback);
        });
    }
コード例 #4
0
    /// <summary>
    /// Leave the lobby that the user is connected to. If the creator of the lobby leaves, the lobby will be destroyed, and any client connected to the lobby will leave. If a member leaves, there will be no further action.
    /// <para>If the player was able to destroy or leave the lobby, the <see cref="LeaveLobbySucceeded"/> event will be invoked.</para>
    /// <para>This process may throw errors. You can errors by subscribing to the <see cref="LeaveLobbyFailed"/> event.</para>
    /// </summary>
    public virtual void LeaveLobby()
    {
        //if we are the owner of the lobby
        if (isLobbyOwner)
        {
            //Destroy lobby
            EOSSDKComponent.GetLobbyInterface().DestroyLobby(new DestroyLobbyOptions {
                LobbyId = currentLobbyId, LocalUserId = EOSSDKComponent.LocalUserProductId
            }, null, (DestroyLobbyCallbackInfo callback) => {
                //if the result was not a success, log error and return
                if (callback.ResultCode != Result.Success)
                {
                    LeaveLobbyFailed?.Invoke("There was an error while destroying the lobby. Error: " + callback.ResultCode);
                    return;
                }

                ConnectedToLobby = false;
                LeaveLobbySucceeded?.Invoke();
            });
        }
        //if we are a member of the lobby
        else
        {
            EOSSDKComponent.GetLobbyInterface().LeaveLobby(new LeaveLobbyOptions {
                LobbyId = currentLobbyId, LocalUserId = EOSSDKComponent.LocalUserProductId
            }, null, (LeaveLobbyCallbackInfo callback) => {
                //if the result was not a success, log error and return
                if (callback.ResultCode != Result.Success && callback.ResultCode != Result.NotFound)
                {
                    LeaveLobbyFailed?.Invoke("There was an error while leaving the lobby. Error: " + callback.ResultCode);
                    return;
                }

                ConnectedToLobby = false;
                LeaveLobbySucceeded?.Invoke();
            });
        }

        //when the player leaves the lobby, remove notifications
        //will be useless when not connected to lobby
        EOSSDKComponent.GetLobbyInterface().RemoveNotifyLobbyMemberStatusReceived(lobbyMemberStatusNotifyId);
        EOSSDKComponent.GetLobbyInterface().RemoveNotifyLobbyUpdateReceived(lobbyAttributeUpdateNotifyId);
    }
コード例 #5
0
    /// <summary>
    /// Update an attribute that is attached to the lobby.
    /// </summary>
    /// <param name="attribute">The new data to apply.</param>
    private void UpdateAttribute(AttributeData attribute)
    {
        LobbyModification modHandle = new LobbyModification();

        EOSSDKComponent.GetLobbyInterface().UpdateLobbyModification(new UpdateLobbyModificationOptions {
            LobbyId = currentLobbyId, LocalUserId = EOSSDKComponent.LocalUserProductId
        }, out modHandle);

        modHandle.AddAttribute(new LobbyModificationAddAttributeOptions {
            Attribute = attribute, Visibility = LobbyAttributeVisibility.Public
        });

        EOSSDKComponent.GetLobbyInterface().UpdateLobby(new UpdateLobbyOptions {
            LobbyModificationHandle = modHandle
        }, null, (UpdateLobbyCallbackInfo callback) => {
            if (callback.ResultCode != Result.Success)
            {
                AttributeUpdateFailed?.Invoke(attribute.Key, $"There was an error while updating attribute \"{ attribute.Key }\". Error: " + callback.ResultCode);
                return;
            }

            AttributeUpdateSucceeded?.Invoke(attribute.Key);
        });
    }
コード例 #6
0
    /// <summary>
    /// Remove an attribute attached to the lobby.
    /// </summary>
    /// <param name="key">The key of the attribute that will be removed.</param>
    public virtual void RemoveAttribute(string key)
    {
        LobbyModification modHandle = new LobbyModification();

        EOSSDKComponent.GetLobbyInterface().UpdateLobbyModification(new UpdateLobbyModificationOptions {
            LobbyId = currentLobbyId, LocalUserId = EOSSDKComponent.LocalUserProductId
        }, out modHandle);

        modHandle.RemoveAttribute(new LobbyModificationRemoveAttributeOptions {
            Key = key
        });

        EOSSDKComponent.GetLobbyInterface().UpdateLobby(new UpdateLobbyOptions {
            LobbyModificationHandle = modHandle
        }, null, (UpdateLobbyCallbackInfo callback) => {
            if (callback.ResultCode != Result.Success)
            {
                AttributeUpdateFailed?.Invoke(key, $"There was an error while removing attribute \"{ key }\". Error: " + callback.ResultCode);
                return;
            }

            AttributeUpdateSucceeded?.Invoke(key);
        });
    }
コード例 #7
0
    /// <summary>
    /// Creates a lobby based on given parameters using Epic Online Services.
    /// <para>You can get the data that was added to the lobby by subscribing to the <see cref="CreateLobbySucceeded"/> event which gives you a list of <see cref="Attribute"/>.</para>
    /// <para>This process may throw errors. You can get errors by subscribing to the <see cref="CreateLobbyFailed"/> event.</para>
    /// </summary>
    /// <param name="maxConnections">The maximum amount of connections the lobby allows.</param>
    /// <param name="permissionLevel">The restriction on the lobby to prevent unwanted people from joining.</param>
    /// <param name="presenceEnabled">Use Epic's overlay to display information to others.</param>
    /// <param name="lobbyData">Optional data that you can to the lobby. By default, there is an empty attribute for searching and an attribute which holds the host's network address.</param>
    public virtual void CreateLobby(uint maxConnections, LobbyPermissionLevel permissionLevel, bool presenceEnabled, AttributeData[] lobbyData = null)
    {
        EOSSDKComponent.GetLobbyInterface().CreateLobby(new CreateLobbyOptions {
            //lobby options
            LocalUserId     = EOSSDKComponent.LocalUserProductId,
            MaxLobbyMembers = maxConnections,
            PermissionLevel = permissionLevel,
            PresenceEnabled = presenceEnabled,
        }, null, (CreateLobbyCallbackInfo callback) => {
            List <Attribute> lobbyReturnData = new List <Attribute>();

            //if the result of CreateLobby is not successful, invoke an error event and return
            if (callback.ResultCode != Result.Success)
            {
                CreateLobbyFailed?.Invoke("There was an error while creating a lobby. Error: " + callback.ResultCode);
                return;
            }

            //create mod handle and lobby data
            LobbyModification modHandle = new LobbyModification();
            AttributeData defaultData   = new AttributeData {
                Key = DefaultAttributeKey, Value = DefaultAttributeKey
            };
            AttributeData hostAddressData = new AttributeData {
                Key = hostAddressKey, Value = EOSSDKComponent.LocalUserProductIdString
            };

            //set the mod handle
            EOSSDKComponent.GetLobbyInterface().UpdateLobbyModification(new UpdateLobbyModificationOptions {
                LobbyId = callback.LobbyId, LocalUserId = EOSSDKComponent.LocalUserProductId
            }, out modHandle);

            //add attributes
            modHandle.AddAttribute(new LobbyModificationAddAttributeOptions {
                Attribute = defaultData, Visibility = LobbyAttributeVisibility.Public
            });
            modHandle.AddAttribute(new LobbyModificationAddAttributeOptions {
                Attribute = hostAddressData, Visibility = LobbyAttributeVisibility.Public
            });

            //add user attributes
            if (lobbyData != null)
            {
                foreach (AttributeData data in lobbyData)
                {
                    modHandle.AddAttribute(new LobbyModificationAddAttributeOptions {
                        Attribute = data, Visibility = LobbyAttributeVisibility.Public
                    });
                    lobbyReturnData.Add(new Attribute {
                        Data = data, Visibility = LobbyAttributeVisibility.Public
                    });
                }
            }

            //update the lobby
            EOSSDKComponent.GetLobbyInterface().UpdateLobby(new UpdateLobbyOptions {
                LobbyModificationHandle = modHandle
            }, null, (UpdateLobbyCallbackInfo updateCallback) => {
                //if there was an error while updating the lobby, invoke an error event and return
                if (updateCallback.ResultCode != Result.Success)
                {
                    CreateLobbyFailed?.Invoke("There was an error while updating the lobby. Error: " + updateCallback.ResultCode);
                    return;
                }

                LobbyDetails details;
                EOSSDKComponent.GetLobbyInterface().CopyLobbyDetailsHandle(new CopyLobbyDetailsHandleOptions {
                    LobbyId = callback.LobbyId, LocalUserId = EOSSDKComponent.LocalUserProductId
                }, out details);

                ConnectedLobbyDetails = details;
                isLobbyOwner          = true;
                ConnectedToLobby      = true;
                currentLobbyId        = callback.LobbyId;

                //invoke event
                CreateLobbySucceeded?.Invoke(lobbyReturnData);
            });
        });
    }