使用 Steamworks.NET 包装器时不会触发回调

Callbacks are not getting triggered while using Steamworks.NET wrapper

我想四处走走并了解如何正确使用 Steamworks.NET C# 包装器,因为我想将它与我的 discord 机器人一起使用。现在,大多数功能都可以使用正常的回调方法。当我尝试使用具有相同回调接口的三个方法时,我碰壁了,其中 none 触发了回调。所以在 ISteamMatchmakinServers 接口中,我想使用 3 种方法 - PlayerDetails, PingServer, ServerRules 来获得你需要使用这些接口 ISteamMatchmakingPingResponse, ISteamMatchmakingPlayersResponse, ISteamMatchmakingRulesResponse 的响应。在 Steamworks 官方文档中它指出:

You must inherit from the ISteamMatchmakingRulesResponse object to receive this callback.

Callback interface for receiving responses after pinging an individual server.

If you are destructing an object that implements this interface then you should
call CancelServerQuery passing in the handle to the query which is in progress.
Failure to cancel in progress queries when destructing a callback handler may
result in a crash when a callback later occurs.

现在,如果我将 Unity 与其 Mono 脚本一起使用并将接口放在它继承方法的 OnEnable 方法中,这些接口就会被触发。

    public void OnEnable() {
        m_ServerListRequest = HServerListRequest.Invalid;
        m_ServerQuery = HServerQuery.Invalid;

        m_ServerListResponse = new ISteamMatchmakingServerListResponse(OnServerResponded, 
                                OnServerFailedToRespond, OnRefreshComplete);
        m_PingResponse = new ISteamMatchmakingPingResponse(OnServerResponded,
                                OnServerFailedToRespond);
        m_PlayersResponse = new ISteamMatchmakingPlayersResponse(OnAddPlayerToList, OnPlayersFailedToRespond,
                                OnPlayersRefreshComplete);
        m_RulesResponse = new ISteamMatchmakingRulesResponse(OnRulesResponded,
                             OnRulesFailedToRespond, OnRulesRefreshComplete);

    }

现在,我的想法是只有当用户使用 .server 命令时我才需要这些方法,所以我做的和上面一样,只是将继承放在命令调用的开头。然后我触发方法,得到响应,但这次没有调用回调,所以我无法访问信息。我的代码:

public class Commands : ModuleBase {
        CSteamID m_Lobby;
        private HServerQuery m_ServerQuery;
        private ISteamMatchmakingPingResponse m_PingResponse;
        private ISteamMatchmakingPlayersResponse m_PlayersResponse;
        private ISteamMatchmakingRulesResponse m_RulesResponse;

方法:

[Command("server")]
        public async Task Server(int serverNumber)
        {
            m_ServerListRequest = HServerListRequest.Invalid;
            m_ServerQuery = HServerQuery.Invalid;

            m_ServerListResponse = new ISteamMatchmakingServerListResponse(OnServerResponded, 
                                OnServerFailedToRespond, OnRefreshComplete);
            m_PingResponse = new ISteamMatchmakingPingResponse(OnServerResponded,
                                OnServerFailedToRespond);
            m_PlayersResponse = new ISteamMatchmakingPlayersResponse(OnAddPlayerToList, OnPlayersFailedToRespond,
                                OnPlayersRefreshComplete);
            m_RulesResponse = new ISteamMatchmakingRulesResponse(OnRulesResponded,
                             OnRulesFailedToRespond, OnRulesRefreshComplete);

            if (serverNumber == 422) {
                m_Lobby = (CSteamID)90130468311942151;
            }

回调在命令中class

private void OnServerResponded(HServerListRequest hRequest, int iServer) {
        Debug.Log("OnServerResponded: " + hRequest + " - " + iServer);
    }

    private void OnServerFailedToRespond(HServerListRequest hRequest, int iServer) {
        Debug.Log("OnServerFailedToRespond: " + hRequest + " - " + iServer);
    }

    private void OnRefreshComplete(HServerListRequest hRequest, EMatchMakingServerResponse response) {
        Debug.Log("OnRefreshComplete: " + hRequest + " - " + response);
    }

    // ISteamMatchmakingPingResponse
    private void OnServerResponded(gameserveritem_t gsi) {
        Debug.Log("OnServerResponded: " + gsi + "\n" + GameServerItemFormattedString(gsi));
    }

    private void OnServerFailedToRespond() {
        Debug.Log("OnServerFailedToRespond");
    }

    // ISteamMatchmakingPlayersResponse
    private void OnAddPlayerToList(string pchName, int nScore, float flTimePlayed) {
        Debug.Log("OnAddPlayerToList: " + pchName + " - " + nScore + " - " + flTimePlayed);
    }

    private void OnPlayersFailedToRespond() {
        Debug.Log("OnPlayersFailedToRespond");
    }

    private void OnPlayersRefreshComplete() {
        Debug.Log("OnPlayersRefreshComplete");
    }

    // ISteamMatchmakingRulesResponse
    private void OnRulesResponded(string pchRule, string pchValue) {
        Debug.Log("OnRulesResponded: " + pchRule + " - " + pchValue);
    }

    private void OnRulesFailedToRespond() {
        Debug.Log("OnRulesFailedToRespond");
    }

    private void OnRulesRefreshComplete() {
        Debug.Log("OnRulesRefreshComplete");
    }

通过创建另一个方法 运行 每 50 毫秒调用 SteamAPI.RunCallbacks(); 并且现在可以正确调用回调使其工作