이 가이드에서는 Play Games 서비스 c++ SDK 리더보드 API를 사용하여 시각적 리더보드를 만들고 플레이어 점수를 기록하고 점수를 이전 게임 세션의 플레이어 점수와 비교하는 방법을 보여줍니다. API는
LeaderboardsClient에서 확인할 수 있습니다.
시작하기 전에
아직 검토하지 않았다면 리더보드 게임 개념을 검토하는 것이 좋습니다.
리더보드 API를 사용하여 코딩을 시작하기 전에 다음을 진행합니다.
Google Play 서비스 SDK설정 가이드에 따라, Google Play Games 서비스을 사용하도록 앱을 설치하고 설정합니다.
Google Play Console 가이드의 안내에 따라, 게임에서 표시하거나 업데이트할 리더보드를 정의합니다.
리더보드 클라이언트 가져오기
리더보드 API를 사용하려면 먼저 게임에서
LeaderboardsClient 객체를 가져와야 합니다.
이렇게 하려면 PgsLeaderboardsClient_create()
메서드를 호출하고 활동을 전달하면 됩니다.
플레이어 점수 업데이트
플레이어의 점수가 변경 (예: 플레이어가 게임을 완료했을 때)되면 PgsLeaderboardsClient_submitScoreImmediate를 호출하여 리더보드의 점수를 업데이트할 수 있습니다. 리더보드 ID, 원시 점수 값, 선택적 점수 태그, 콜백 함수를 전달해야 합니다.
// Callback function to handle the result of submitting the score void OnScoreSubmitted(PgsStatusCode status_code, PgsScoreSubmissionData* score_submission_data, void* user_data) { if (status_code == PGS_STATUS_SUCCESS) { // Score submitted successfully // You can inspect score_submission_data for details // Remember to release the data when done: PgsScoreSubmissionData_Release(score_submission_data); } else { // Handle error } } // Function to submit the score void SubmitScore(PgsLeaderboardsClient* client, const char* leaderboard_id, int64_t score) { const char* score_tag = NULL; // Optional tag PgsLeaderboardsClient_submitScoreImmediate( client, leaderboard_id, score, score_tag, OnScoreSubmitted, NULL // user_data - optional context pointer ); } // Example usage: // Assuming 'my_leaderboard_id' is defined elsewhere, e.g., fetched from resources // SubmitScore(leaderboards_client, my_leaderboard_id, 1337);
c++ 코드 내에서 리더보드 ID를 상수 또는 리소스로 관리하는 것이 좋습니다.
리더보드 표시
특정 리더보드의 기본 리더보드 사용자 인터페이스를 표시하려면 PgsLeaderboardsClient_showLeaderboardUI를 호출합니다. 이 함수에는 클라이언트 핸들, 활동, 리더보드 ID, 기간 및 컬렉션, 콜백이 필요합니다.
// Callback function to handle the result of showing the UI void OnShowLeaderboardUI(PgsStatusCode status_code, bool success, void* user_data) { if (status_code == PGS_STATUS_SUCCESS && success) { // UI was shown successfully } else { // Handle error or failure to show UI } } // Function to show a specific leaderboard UI void ShowLeaderboard(PgsLeaderboardsClient* client, jobject activity, const char* leaderboard_id) { PgsLeaderboardsClient_showLeaderboardUI( client, activity, leaderboard_id, PGS_LEADERBOARD_TIME_SPAN_ALL_TIME, // Or PGS_LEADERBOARD_TIME_SPAN_DAILY, PGS_LEADERBOARD_TIME_SPAN_WEEKLY PGS_LEADERBOARD_COLLECTION_PUBLIC, // Or PGS_LEADERBOARD_COLLECTION_FRIENDS OnShowLeaderboardUI, NULL // user_data - optional context pointer ); } // Example usage: // ShowLeaderboard(leaderboards_client, android_activity, my_leaderboard_id);
이 함수는 UI를 표시합니다. activity 객체는 UI를 표시하기 위한 컨텍스트를 제공합니다.