1

I was trying to add achievements to one of my games, I made a list and started adding them with image and description in itunes connect, but I can't find a tutorial written in swift, and with a simple function like unlockachievement and the achievement id passed inside that

In my game, inside the viewdidload of the game over screen i made this

if (achievement condition){
//unlock achievement ("achievementID")
}

Is this a correct way to add achievements?In this view i have many stats passed from previous view so if I put the unlockachievement function here the popup should appear at the end of the game without distracting the player

For the leaderboards I made a func that gets score and leaderboard, I want to do the same for achievements, how do I do it?Is it possible?

I also read about achievement progress in the game center view,is it possible to avoid it?Especially with hidden achievements that should show up only when conditions are met

GioB
  • 973
  • 3
  • 11
  • 17

1 Answers1

1

this is how I solved

func loadAchievementPercentages() {
    print("get % ach")
    GKAchievement.loadAchievementsWithCompletionHandler { (allAchievements, error) -> Void in

        if error != nil {
            print("GC could not load ach, error:\(error)")
        }
        else
        {
            //nil if no progress on any achiement
            if(allAchievements != nil) {
                for theAchiement in allAchievements! {

                    if let singleAchievement:GKAchievement = theAchiement {

                        self.gameCenterAchievements[singleAchievement.identifier!] = singleAchievement
                    }
                }
            }

            for(id,achievement) in self.gameCenterAchievements {
                print("\(id) - \(achievement.percentComplete)")
            }

        }
    }
}
func incrementCurrentPercentageOfAchievement (identifier:String, amount:Double) {

    if GKLocalPlayer.localPlayer().authenticated {

        var currentPercentFound:Bool = false

        if ( gameCenterAchievements.count != 0) {

            for (id,achievement) in gameCenterAchievements {

                if (id == identifier) {
                    //progress on the achievement found
                    currentPercentFound = true

                    var currentPercent:Double = achievement.percentComplete

                    currentPercent = currentPercent + amount

                    reportAchievement(identifier,percentComplete:currentPercent)

                    break
                }
            }
        }
        if (currentPercentFound == false) {
            //no progress on the achievement

            reportAchievement(identifier,percentComplete:amount)

        }
    }
}

func reportAchievement (identifier:String, percentComplete:Double) {

    let achievement = GKAchievement(identifier: identifier)

    achievement.percentComplete = percentComplete

    let achievementArray: [GKAchievement] = [achievement]

    GKAchievement.reportAchievements(achievementArray, withCompletionHandler: {

        error -> Void in

        if ( error != nil) {
            print(error)
        }

        else {

            print ("reported achievement with % complete of \(percentComplete)")

            self.gameCenterAchievements.removeAll()
            self.loadAchievementPercentages()
        }

    })
}

in the view did load I have this

if (conditions for the achievement)                           
    {

        incrementCurrentPercentageOfAchievement("achievementID", amount: 100)
    }

Tested now and it worked,now i want to do something different,I don't want to increment an achievement percentage but to set the percentage, for example:

Achievement - combo of 10 - Play a game - Combo of 8 and shows 80% progress on the achievement - Another game - Combo of 2

With the code I have now it should unlock the achievement because the percentage is cumulative, so I have to write another function to change the achievement progress percentage and to set it to 20 Now if in a game I met the requirements and unlock the achievement,and in the next game I don't get this condition again, the achievement is still unlocked, right?

GioB
  • 973
  • 3
  • 11
  • 17