2012-11-05 4 views
3

에 GREE c# API를 사용하여 게임을 만들고 있습니다. 리더 보드에서 모든 (사용자 이름, 점수)에 대해 Action을 실행하려고합니다. 여기액션에서 액션 호출

public static void LoadBestScoreFromFriends(Action<FriendAndScore> onComplete) 
{ 
    Gree.Leaderboard.LoadScores(LEADERBOARD_ID, 
     UnityEngine.SocialPlatforms.UserScope.FriendsOnly, 
     UnityEngine.SocialPlatforms.TimeScope.AllTime, 
     1, 
     100, 
     (IScore[] scores) => { 

     foreach (IScore iscore in scores) 
     { 
      // Since the username is needed, a second 
      // call to the API must be done 
      Gree.User.LoadUser(iscore.userID, 
      (Gree.User guser) => { 
       FriendAndScore friend = new FriendAndScore(); 
       friend.Name = guser.nickname; 
       friend.Score = "" + iscore.value; 

       // Run the action for every (nickname, score value) 
       onComplete(friend); 
      }, (string error) => { 
       Debug.Log("Couldn't fetch friend! error: " + error); 
      }); 
     } 
    }); 
} 

문제 iscore.value 항상 같은 점이다 :

코드는 다음과 같다. foreach의 마지막 요소입니다. 이는 foreach이 루프를 종료 한 후에 LoadUser()이 끝나기 때문에 의미가 있습니다.

거기서 전화를 걸려면 두 번째 방법을 만들려고했습니다. 같은 뭔가 :

private void GetTheUserAndCallTheAction(Action<FriendAndScore> onComplete, string userID, string score) 
{ 
    Gree.User.LoadUser(userID, 
      (Gree.User guser) => { 
       FriendAndScore friend = new FriendAndScore(); 
       friend.Name = guser.nickname; 
       friend.Score = score; 
       onComplete(friend); 
      }, (string error) => { 
       Debug.Log("Couldn't fetch friend! error: " + error); 
      }); 
} 

나는 내가 이것을 처리하는 더 나은 방법이 있는지 알고 싶어 C# 안돼서입니다.

답변

3

이것은 foreach 루프에서 람다 식을 구성하고 나중에이 람다를 실행할 때 캡처가 작동하는 방식입니다.

수정 - IScore iscore의 로컬 복사본을 선언 : 에릭 Lippert의에 의해

foreach (IScore eachScore in scores) 
{ 
    IScore iscore = eachScore; 

그리고 Closing over the loop variable...은 행동과 관련된 문제를 자세히 설명합니다.

관련 문제