我试图绕过并了解如何正确使用 Steamworks.NET C# 包装器,因为我想将它与我的不和谐机器人一起使用。现在,大多数函数都可以工作,它们使用普通的回调方法。当我尝试使用三个具有相同回调接口的方法时,我陷入了困境,其中没有一个触发回调。因此,在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;
}
回调在命令类中
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");
}