Adding a Game Center Leaderboard to your game


  • Share on Pinterest

Compile Swift

Game Center Leaderboard Compile Swift Podcast image

Edited Transcription

Game Center leaderboards are a great way to increase player interaction and promote your game.

Welcome to the Compile Swift podcast. I'm your host as always Peter Witham, you can find myself and this If you are a Patreon member, you will be getting a special bonus video version of this where you'll get to see my screens. And if you're not, you can go to Patreon.com/compileswift and support the podcast there.

But either way, this information is going to work for you. We will discuss game center integration in your apps and games why I think it's a good idea and how you do that. Now, first of all, let's talk about why I think it's a good idea.

Anything that makes the users feel more connected to the experience with your games or apps, particularly games on this, is a bonus. Now, the good thing here is as well with the game center. Not only is it easy to integrate if you're doing something like SceneKit or SpriteKit, but For the demonstration of this one here, I will be talking about SpriteKit and how I integrated it into my endless hurdles game.

But also why it's an excellent way to spread the word. Among your users and invites friendly competition simultaneously, because game center takes care of many problems for you. And it makes it very simple to integrate. Like I say, with just a few lines of code. And the next thing you know, people are using game center and will see their friends playing. This game and they may be curious, or they may, say, Hey, I'm playing this game.

You should play this too. And we can compete in things like, you know, leaderboards. Achievements are all excellent techniques for getting people to feel like they want to play your game. For longer and over some time, things like special events and all those kinds of things. You can integrate those in here as well. So going ahead with this, let's start, there's two parts here.

Now, first of all, you need to set something up on the backend in the Apple portal. And like I said, if you are a Patreon member, you will see the video version of this, but I'm going to walk through this here and explain it as well for those who are just listening to the audio version. But if you go to the Apple portal and look at your app, and like I say, in my case, I'm using my Endless Hurdles game here,

So I've got one set up ready. You go to the services tab, and then there is this game center link on the services tab. That's going to come up and if you click on there and you bring it up,

in there, you can set things like challenges and leaderboards and achievements, which is, Like I say. Very attractive to me with this game, this game that I'd got. So I've set one up here, but I'll walk through it. Now, first of all, let's talk about leaderboards. So we've all seen them, right?

The high school boards. And why would you want to do that? Well, like I said, it's a friendly competition. This is an excellent way to have more folks playing your games more often and competing with each other. And there's a lot of options you can set in here. And it's straightforward to set up. You answer a few questions.

So what you would do, if you're starting on this, you will see something that says click to add a leaderboard. And then, once you do that, you get a few options. Since I've already got one set up, I will click on it. But it brings up a window you've you fill in these details? Now, the first thing I have set up is a recurring leaderboard.

Now, what that means is this gives me the options that I want from my game here. Now. That means I get the ability to have this automatically reset whenever. I decided I wanted that to happen. Now you, the other option is to have a persistent one that goes on. I've chosen the recurring one because we've all seen those games where you have this problem that someone gets to the top of the leaderboard, and they're there forever.

And I didn't want to have this problem. So by doing a recurring one, I can have it reset on my schedule that I set up, allowing everybody to get on the leaderboard. Now, as you go through this, there are straightforward options. The first thing is going on. You will want to do a leaderboard reference name, which is giving it a name. That is going to be easy for you to reference. And look up, you may have multiple boards, leader boards in the portal.

So it's just a name for you to recognize it by here. Now the leaderboard ID that you put in. Is important. That is the one you're going to reference in the code. So I would say choose wisely. Okay. Now you also get to have different format types for the scores. And, there are various options, decimals and times and everything else.

I've gone with integers on my one because I think it's the easiest. You score points the one with the highest points, your top of the board and an integer of course is perfect for that. But you may want to choose different options. They're all there to play with the score submission type.

You get two options. You can have the best score. And this is per player. So as I'm playing, my best score gets reported on the leaderboard. Or you can say, okay, go with the leaders, go with the players, most recent score. So, every time you score. And the game is over and or whenever you submit the score, that's the one that goes up for a leaderboard.

Honestly, it makes the most sense to have the best score. Right. And then you can enter a range, so you can essentially try to prevent some cheating, which is happening there. So you could say go from zero, and you know that no one can score over.

Say 200 points. You could put, you know, 205 points in there. The leaderboard will then ignore anything outside of that range. So that's a way if someone's trying to hack your game or something and try to send a false score. That is a way to cap that. That's a nice feature.

I don't, I don't feel like I needed it here, but it is a nice feature. Now, because this is a recurring leaderboard like I said, you can go in and say, okay, tell me about this recurring period, right? So in the portal, it'll ask you for a start date and time. You cannot set a time and date in the past.

If you try that, the portal will say, ” Hey, can't do that. That's in the past. Which if you're a developer. It causes a little bit of a complication at first. Because you're going to want to start working with this right away, so, you know, you'll have to start with the current date and time and say it, say one minute in the future, something like that.

And then the clock is ticking. Now as you're doing this as a developer and. You have never put one out before. That's fine because there's no leaderboard in the public space. Right? So you can mess with these start dates and times as much as you need to, as a developer, without hurting your players. Once one has been published, that's a little different. We'll talk about that. A little later here. But you set a start date and time that works for you.

As I said, if you're a developer actively writing code, set it for one or two minutes in the future to make the portal happy. Right. Then, you have the option to set a duration. So how long will this leaderboard last? Now, this is entirely up to you. You put in several days, minutes, and hours and choose a different one. But I went for seven days.

So, this is a one-week leaderboard. Okay. And then along with that restarts, every is another option you have to fill in. Uh, again, I've kept it simple for seven days. So. The duration is seven days. It'll restart every seven days. Okay. So in my case, for example, every Sunday night, At midnight. You could argue Monday morning, depending on which way you want to look at it. The board resets, and that happens automatically for me every seven days.

I never need to do anything after that now. The only other thing you need to put in here is language support. By default, you've got to put one in, right? So in my case, by default, I just put English. Again, the format is integer. And then you hit save. And that is it.

Once you've done that, it is up in the portal. And if you want to go back and edit that in the future, there are certain parts that you can edit.

And then there are some that you can't because you know, it's now cast in stone, but at least you can go back and like I say, work with these. So, that is how you set up a leaderboard on the portal side. Once it's in there, You will notice it's not live right by default. It will not be live. Until you publish once you have published your game, you send it off to Apple for review.

Along with that, you have to answer some questions. It'll you know, when you send for review it'll. It'll say, Hey, I noticed you're using a leaderboard in your code. And some game center services. Just like all other permissions you're going to go through and you answer the question.

It'll ask you which leader boards do you need to be published with that? Once you do that, you say which ones. You pass review, and you send that out there at that point. The status becomes live for that leaderboard. Now, hopefully, that makes sense.

Moving on from that. Achievements are very similar. Now, you can, again, click the plus as it tells you to add achievements. It's a similar process, right? You're going to set the language, a name, and then you're going also to create this list of achievements.

So I won't go into too many details there, but basically. That's how that works. Right. And then, once all that's all set up, that is all you need on the portal side. Like I said the critical part to remember. These are going to be the achievements and the leaderboard ID. That is how you will reference it back in your swift code.

Moving on to the app side of things. Now, are you going to be surprised? Held it'll code is involved. I was very pleasantly surprised by this.

So the first thing you need to do in your app is import gameplay kit. Now in my case, I decided that the game view controller. Since I'm using a SpriteKit based application here, I just went with the game view controller and imported gameplay kit there. Left it at that. Right. Then, use it wherever you want to use it in your application or game. This is where you will want to put the rest of the code.

Now, of course, you can architect this any way you want. I'm just going with a simple approach here. So, on my game, on the menu screen of the game. I put a button where you can view the leaderboard, right? And so I decided that's where the bulk of the code will be here.

So in my case, like I say, this is a SpriteKit based game. So, bear that in mind, right?

I created a variable where I will create my instance of my game center services. And you say, Hey, you know, set that to a new instance of game center services. Very straightforward. So in my case, I just called it game center services, simple as that right. Name of the variable. Then on my. Did move.

And for those who don't know the deed move function, In a SpriteKit based game is saying when you load up this scene and it is on the screen. I want you to do some things right as its preparing. And all I do is call it, and I say game center services. Authenticate local player. It's as simple as that. That is going to do an awful lot of things for us.

First of all, if a player is not. Logged into the game center. It's going to ask them, Hey. You want to log into game center? This game can use that. If they say no. That's it. End of story. They're not going to be bothered with it after that. Okay. So it's as simple as that, but bear that in mind. As you all building out your UI for these things, right? If they say yes, Apple has done a beautiful job of taking care of these things for us. It is going to use the game center—authentication system.

So it's going to bring up the screen. We don't have to build it. They log into game center and that's either going to be successful or not. Right. It's as simple as that, but Apple has handled everything for us. So we don't have to worry about anything else once that's done. At this point, we assume they're logged in.

Right. And like I say, when I say, assume I don't mean right. Write the code because, Hey, that'll probably be okay. Apple's taking care of everything for us. Other than that. That is all you need to do. Now, in my case, like I say, I've got a button. And when they click the button. Again, we want to use the game center services here and display the leaderboard. Again, this leaderboard is built by Apple.

We don't have to do anything here unless we want to complicate our lives, but why complicate your life?

So all I've done is created a variable when they click the button. It says, let VC equal game center services. Show weekly highest scoreboard, which is a function. That's it.

I'm just going to call that function.

And then after that, I will put this is a long one, folks. So, hang in there. This is one way it's advantageous to see the video. I'm going to present that view controller. Now in a SpriteKit game, like I say, you got to do some digging here, so I do self dot view, dark window dot roof view controller. Doc present and will say, Hey, what do you control or want to show. Well, I want to show the VC that I just created.

Right. Set animation to true completion to nil. That is it. That is all you've got to do to display—the scoreboard. The game center's going to take care of this. Apple has done a beautiful job. It will bring up the game center, leaderboard and show any data on there. And. All the, the things that a user would be used to seeing in game center is takes care of everything for us.

If they want to dismiss it, they can ignore it.

Like they usually do. And again, it will take care of everything for us and bring us back to our game. So that is all you need to do to display. A leaderboard. But how does the user get on the leaderboard? Well, let's talk about that.

So in my case, what I did. Is. When the game is over, the player. Levels completed or whatever appropriate moment feels suitable for you. I want to send this new high score to the school board. That's what you want to do. So in my case, like I say, at the end of the level,

I've set up a variable game center services to a new instance of game center services.

That is all I need to do. In there. And then, when the time comes.

I'm going to submit the score now. How do you do that well it's very straightforward? In my case game center services dot submit high school. It will expect an integer because that's how we set up the scoreboard. So I send it an integer. And game center will take care of everything else for us. Remember those rules that we set up where we said, Hey, only record and display their highest score.

So whatever value I send to game center, it will determine for me, this is a new value that I need to record, and if it is, it will do that.

And if it's not, it's just going to ignore it. And that is it, folks. That is all you need to do to display a leaderboard. Have it receive new schools. That's all you need to do.

Unless you want to, like I say, highly customize it or go crazy with it for some reason.

So that is all you need to do to display a leaderboard using game center and submit scores. In your game or your app?

This has been helpful to you, and I hope it has. You should try this out. Like I say there's lots of good reasons to use games and during your games, Especially for getting. Essentially, it's a free promotion of your game, from your players and having other players be interested in it as well.

Plus, it's a nice feature to list in the app store. You never know someone might see it and go, Hey, that's cool. Let's try that out. Or, Hey, anything that possibly gets Apple's attention to have your game. Potentially.

It was promoted by Apple as well. Right. So go with that. I hope this episode has been helpful. Like I said, if you're a Patreon member, head over there. You'll get the video version, but you can see all of this and the code. Suppose you're not a Patreon member. Hey, Patreon.com/compileswift.

Keep this podcast alive.

Greatly. Appreciate it. Thank you for that. Reach out to me, CompileSwift, on any of the networks. Hey, I'd love to discuss this with you if you're using it or have questions. Other than that, folks, I will see you in the next episode.

Subscribe / Follow The Podcast

Click here to see all available platforms

Listen on Apple Podcasts
Listen on Spotify