Google Play遊戲C++似乎並沒有爲我返回正確的朋友數據。Google Play遊戲C++ API - 無法以編程方式訪問朋友
這裏的基本思路是我們在遊戲中有一個頁面,顯示朋友分數列表。此外,當你玩遊戲時,它會在屏幕上顯示一個指標,當你接近和當你通過你的朋友的最好成績。
該遊戲是用Cocos2d-x編寫的,因此我們使用Google Play遊戲C++庫。 GPG認證成功,其他功能(如解鎖成就和實際提交分數)工作正常。順便說一句,如果Google的任何人都在閱讀,那麼網站上就有版本說明2.2版,但下載頁面只有2.1版,所以這就是我們正在使用的版本。
無論如何,我們有兩個設備與不同的Google Play帳戶是朋友。我們可以選擇打開默認的原生遊戲服務排行榜界面,如果我轉到社交排行榜,那麼這兩個設備都會從該頁面看到其他玩家的高分 - 所以看起來我們已經成功創建了這兩個帳戶的朋友。
不幸的是,我無法使用C++ API以編程方式獲取此朋友數據。
這裏是相關的代碼。
void GameCenterSession::fetchFriends()
{
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "Fetching friends!");
//GET INVITABLE FRIENDS
game_services_->Players().FetchInvitable(gpg::DataSource::CACHE_OR_NETWORK, [] (gpg::PlayerManager::FetchListResponse response) {
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "Got invitable friends response: %d", response.status);
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "Got invitable players info! Num players: %d", response.data.size());
if (gpg::IsSuccess(response.status)) {
//PROCESS PLAYERS
GameCenterSession::getInstance()->onPlayersInfoReceived(kRequestFriendsInfo, response.data);
}
});
//GET CONNECTED FRIENDS
game_services_->Players().FetchConnected(gpg::DataSource::CACHE_OR_NETWORK, [] (gpg::PlayerManager::FetchListResponse response) {
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "Got connected friends response: %d", response.status);
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "Got connected players info! Num players: %d", response.data.size());
});
}
void GameCenterSession::onPlayersInfoReceived(const int requestId, std::vector<gpg::Player> playersInfo) {
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "onPlayersInfoReceived num players: %d", playersInfo.size());
const gpg::ScorePage::ScorePageToken distanceToken = game_services_->Leaderboards().ScorePageToken(
kLeaderboardBestDistance,
gpg::LeaderboardStart::TOP_SCORES,
gpg::LeaderboardTimeSpan::ALL_TIME,
gpg::LeaderboardCollection::SOCIAL
);
//FETCH ALL TIME SOCIAL SCORES FOR DISTANCE
game_services_->Leaderboards().FetchScorePage(gpg::DataSource::CACHE_OR_NETWORK, distanceToken, 1000, [] (gpg::LeaderboardManager::FetchScorePageResponse response) {
if (gpg::IsSuccess(response.status) && response.data.Valid()) {
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "Got social leaderboard! Num players: %d", response.data.Entries().size());
gpg::ScorePage::Entry myEntry = gpg::ScorePage::Entry();
//search through and find my score!
for (auto score : response.data.Entries()) {
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "%s got distance %d", score.PlayerId().c_str(), score.Score().Value());
if (score.PlayerId().compare(GameCenterSession::getInstance()->myPlayer.Id()) == 0) {
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "Distance - Yup that's me");
myEntry = score;
} else {
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "Distance - It's another player!");
}
}
GameCenterSession::getInstance()->onScoresReceived(kRequestFriendsDistancesInfo, myEntry, response.data.Entries());
}
});
const gpg::ScorePage::ScorePageToken scoreToken = game_services_->Leaderboards().ScorePageToken(
kLeaderboardBestScore,
gpg::LeaderboardStart::TOP_SCORES,
gpg::LeaderboardTimeSpan::ALL_TIME,
gpg::LeaderboardCollection::SOCIAL
);
game_services_->Leaderboards().FetchScorePage(gpg::DataSource::CACHE_OR_NETWORK, scoreToken, 1000, [] (gpg::LeaderboardManager::FetchScorePageResponse response) {
if (gpg::IsSuccess(response.status) && response.data.Valid()) {
gpg::ScorePage::Entry myEntry = gpg::ScorePage::Entry();
//search through and find my score!
for (auto score : response.data.Entries()) {
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "%s got score %d", score.PlayerId().c_str(), score.Score().Value());
if (score.PlayerId().compare(GameCenterSession::getInstance()->myPlayer.Id()) == 0) {
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "Score - Yup that's me");
myEntry = score;
} else {
__android_log_print(ANDROID_LOG_INFO, "MyGame!", "Score - It's another player!");
}
}
GameCenterSession::getInstance()->onScoresReceived(kRequestFriendsScoresInfo, myEntry, response.data.Entries());
}
});
if (requestId == kRequestFriendsInfo)
{
friends = playersInfo;
}
}
查看代碼,您會看到我首先獲取好友列表並檢查列表中有多少玩家。我嘗試訪問兩個可邀請的朋友(通過文檔判斷這是我想要的),並與相關朋友(就像測試一樣)。我繼續抓住兩種不同的社交排行榜 - 距離和得分,並且我檢查了結果中有多少球員。這是我的日誌輸出。
//設備1
I/MyGame!(1510): Google Play Games authenticated successfully!
I/MyGame!(1510): Fetching friends!
I/MyGame!(1510): Got invitable friends response: 1
I/MyGame!(1510): Got invitable players info! Num players: 0
I/MyGame!(1510): onPlayersInfoReceived num players: 0
I/MyGame!(1510): Got connected friends response: 1
I/MyGame!(1510): Got connected players info! Num players: 0
I/MyGame!(1510): Got social leaderboard! Num players: 1
I/MyGame!(1510): g1703186947466536XXXX got distance 310
I/MyGame!(1510): Distance - Yup that's me
I/MyGame!(1510): g1703186947466536XXXX got score 2510
I/MyGame!(1510): Score - Yup that's me
//設備2
01-23 17:11:27.227 17187 17234 I MyGame!: Google Play Games authenticated successfully!
01-23 17:11:27.250 17187 17234 I MyGame!: Fetching friends!
01-23 17:11:27.451 17187 17234 I MyGame!: Got invitable friends response: 1
01-23 17:11:27.451 17187 17234 I MyGame!: Got invitable players info! Num players: 0
01-23 17:11:27.451 17187 17234 I MyGame!: onPlayersInfoReceived num players: 0
01-23 17:11:27.581 17187 17234 I MyGame!: Got connected friends response: 1
01-23 17:11:27.581 17187 17234 I MyGame!: Got connected players info! Num players: 0
01-23 17:11:27.973 17187 17234 I MyGame!: Got social leaderboard! Num players: 1
01-23 17:11:27.973 17187 17234 I MyGame!: g0152008166550356XXXX got distance 712
01-23 17:11:27.973 17187 17234 I MyGame!: Distance - Yup that's me
01-23 17:11:28.444 17187 17234 I MyGame!: g0152008166550356XXXX got score 2142
01-23 17:11:28.444 17187 17234 I MyGame!: Score - Yup that's me
正如你看到的,我的回調都返回成功的結果代碼。遺憾的是,這兩款設備都沒有返回任何朋友,社交排行榜回調只包含設備上的播放器,而不包括其朋友的分數。
我儘可能地遵循了文檔,並且我已經查看了代碼而不能找到任何問題,但如果它只是一個語義問題,我很樂意聽到我的錯誤。我在這裏做錯了什麼,或者C++ API本身有問題嗎?
在此先感謝。
感謝您的回覆。我看到你是這篇博客文章的作者,所以我懇請要求在相關文檔中添加一條說明,說明此功能不再有效。我們花了很多時間來實現和調試這些,我沒有看到任何提到這些功能在查看鏈接的博客文章之前不再有效。再次感謝。 – noodleben