Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

iOS and MacOS / [TUTORIAL] Game Center and Leaderboards

Author
Message
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 3rd Oct 2012 01:13 Edited at: 3rd Oct 2012 02:11
This is the best way to integrate the GameCenter LeaderBoard to your AppGameKit game.
It will be really easy and fast, in only some lines, directly in your UntitledViewController without subclassing and without external classes.

1. Link Library
Go to the project build window and link libraries.
Press add libraries and chose GameKit.framework.
If when you add the library goes out of your Framework group (folder) drag it into the one you want and after drag again it from the folder to the link libraries window (this is a trick to do faster).

2. Importing library
We have to import it to our UntitledViewController.h adding this line to the header.
#import <GameKit/GameKit.h>

3. Adding the delegates
Usually when you open the UntitledViewController.h file you see this:



Now you have to add the delegate to the interface, to let the GameCenter know who control the window for the Leaderboards. So it will be in this way:


4. Adding the functions
Now as we declared the delegate, we can start to add the main functions to use in the files...


Now is the time to open the UntitledViewController.m and add the code for the functions.


When you see "My_Game" you need to change it with the Category Name you chose in the ITUNES CONNECT when you allow your app for the GameCenter.

In this code, i use this declaration to get a global application delegate variable, don't use SELF, it will not work as you expect;
appDelegate = (iphone_appAppDelegate *) [[UIApplication sharedApplication] delegate];

I also included the TopScore function to give you the possibility to understand how to retrieve the top 10 or the RANGE you want.

Now all you have to do is to call the [self AuthLocalPlayer] function to start it.

Trick to have the UntitledViewController in the C++ AGK
Add this line to your declaration in the AppGameKit template.c;
UntitledViewController *VC = [UntitledViewController alloc];

As you see you need only to alloc, as the init will be done from the UntitledViewController.m
From this moment you can use the Obj C functions also in the C++, like this:
[VC ShowLeaderboard];
or if you want to start the Game Center from C++ line in your agk::begin or your preferred function:
[VC AuthLocalPlayer];

How to submit a score


Example code to add Player struct in AppGameKit shared with Obj C
Open your template.h


Open your template.c


Retrieve the GameCenter Nick (add this in your AuthLocalPlayer after all is ok)


Anywhere in your code:



Remember:
1. You need agk::Sync() some times to change effects on the main view.
No problem if you use the loop control it for you.

2.You need to study a good #import or #include (if you prefer, i prefer import) timeline, probably if you have some errors is cause you mixed too times the same includes and import.
Give a look and try to do less files and avoid to write the same import too times.

If you wanna know more don't hesitate to ask!
Enjoy.
(Moderator can you delete the next post, i done a mistake sorry and thx)

Long life to Steve!
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 3rd Oct 2012 01:23 Edited at: 3rd Oct 2012 01:24


Long life to Steve!
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 3rd Oct 2012 09:22
GREAT post

We need more guides like this from people with knowledge or from TGC
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 3rd Oct 2012 16:37
Well i will add some new to help with simply things.
Most function in Obj C are described on internet as too hard, with subclassing, the adding of new files and it make the new user "crazy".

As we use AppGameKit, we have already a powerful tool, so i always try to get all work in UntitledViewController.h and .m
We don't need 1000 files, each of this with 5 lines.


Long life to Steve!
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 3rd Oct 2012 18:31
That's exactly what i was asking for. Having to include hundreds of files and going through each one to see what they actually do is tiresome!
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 3rd Oct 2012 19:21
Yeah, i added the Custom HUD tutorial too.
Next tutorial will be the iAD in few lines.

Have you tried the sharing of VC in the c++ to call the functions from template.c?
And the appDelegate method to be sure the delegate is the MAIN delegate?

If you start to use this you will find very easy to do all whenever you are.
If you have problems or errors let me see the errors.

Long life to Steve!
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 3rd Oct 2012 20:27 Edited at: 3rd Oct 2012 20:31
i have not tested anything yet.
i need to set up a test project in itunes and set up some leader boards and achievements first
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 5th Oct 2012 16:18
I am trying to implement this code. and I am stuck at 2 points:

1. Where can I find the Category Name to replace "My_Game"? Is this the same as the app name?

2. do i need to put this code anywhere?

appDelegate = (iphone_appAppDelegate *) [[UIApplication sharedApplication] delegate];

I am getting a couple of errors in UntitledViewController.m:

'appDelegate' was not declared in this scope
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 6th Oct 2012 06:23 Edited at: 6th Oct 2012 06:25
Ok....

1. The category "My_Name" is the ID you chose when activate GameCenter on the itunes connect.
After you enable it, you need to create a new leaderboard.
"My_Game" is the name id you give for the leaderboard.
Example : bj_card_scores

2. The trick of the appDelegate can be done in this way
This trick is used to avoid [release] and Singleton.

Go in the UntitledViewController.m

Before you see this: @implementation UntitledViewController
add this: iphone_appAppDelegate *appDelegate;

And if you wanna do the best add this in awakeFromNib function:
appDelegate = (iphone_appAppDelegate *) [[UIApplication sharedApplication] delegate];

now you can use it everywhere!!!
This trick make you use 1 Delegate in all the view.
Enjoy!

Long life to Steve!
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 6th Oct 2012 18:38
Gekko,

Thanks I will try the appdelegate fix and let you know.

For the Category "My_Name", I think I will have various leaderboards for different scores, so how will that work? Or can I create only 1 leaderboard and put in multiple scores in it?
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 6th Oct 2012 21:06
You can create all the leaderboards you want.
When you call the GKLeaderBoard you can chose one of them, is not a problem, because in the View that GameCenter will show there will be a menu to change the category.

Infact i have 2 leaderboards on my "Match Point : Ping Pong Online" app.
There are 2 leaderboards, 1 for ranking and 1 for the top scorer.

And usually i put in only one id. The GameCenter do the rest for you.

Long life to Steve!
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 7th Oct 2012 17:41
Still not working for me.

As soon as i add iphone_appAppDelegate *appDelegate;

I get this error: expected constructor, destructor, or type conversion before '*' token
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 8th Oct 2012 17:42 Edited at: 8th Oct 2012 17:48
As i described in the first post, you need to call in a perfect sequence all the .h files.
And never use #include, but #import.
The difference between import and include is that import don't include the file again if already included.

The iphone_AppDelegate is already declared in the core file, so you need only to get a good h loading sequence.
The default AppGameKit is not that good.

If you want i will post all the h calling from the first to last.

Long life to Steve!
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 8th Oct 2012 21:36
Gekko, if you can please post the .h calling sequence I would really be grateful.

Once I have managed to make it work I thin we should update your original post - Keep in mind that the beginner (like me) who does not know any Obj-C will get stuck immediately.

THANKS A LOT FOR YOUR GREAT HELP
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 10th Oct 2012 04:22
Ok BJ i started a new project while my graphics developer are working on a character.
Now i clean it at MAX and i delete all the WINDOWS parts and include some of my tricks.
I will post my template on the AppGameKit Product chat.

See you there in some hours.

Long life to Steve!
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 10th Oct 2012 09:21
Gekko, thanks a lot

The most important thing though is to point out which parts one must add to have a functioning Gamecenter.

Having a ready made template is not the final solution. For example with v108 the template has changed, so its important that we know what we must add to the standard agk template.
WanteD
11
Years of Service
User Offline
Joined: 29th Dec 2012
Location:
Posted: 29th Dec 2012 14:14
Hey xGEKKOx can you help me? I try to fallow tutorial but I stuck.. I got "Warning: Attempt to present <GKLeaderboardViewController: 0x1cd32140> on <GameCenterViewController: 0x1e0427b0> whose view is not in the window hierarchy!"
I'm new in development and can't figure out how to make it work.
p.s. sorry for my english : )
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 29th Dec 2012 16:50
Have you added the GKLeaderboardViewControllerDelegate in the .h ?

Be sure to have loaded the right Framework GameKit.
You can also look to my thread and download the template to see how to do.

If you need more don't hesitate to ask.

Long life to Steve!
WanteD
11
Years of Service
User Offline
Joined: 29th Dec 2012
Location:
Posted: 3rd Jan 2013 09:25
My GameCenterViewController.h looks:

most of my classes is .mm and I have ApplicationDelegate.h/.mm I'm new in cpp and it's really hard to understand some things.
Thanks for ur help.
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 5th Jan 2013 05:41
No is wrong.
You don't need GameCenterManagerDelegate.... is useless.
Maybe you are not using my template, i see too many files.
My Tutorial is done to use only 1 file for the controller and delegate.
Just to not have too many files, as i hate them.

Download my template, you will find it already implemented.

If you want it works without adding classes you need to follow the tutorial.

The tutorial in the 1st post is really easy to follow.


Long life to Steve!
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 5th Jan 2013 08:48
i still couldn't get the tutorial to work after all this time.

there are things missing and if you are a total beginner in obj-c like i am, it's hard to get things working!!!
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 5th Jan 2013 17:44
Sorry Bj, follow me.

GEKKO TEMPLATE
If you take this project and build it, it work?
Well now this project is the same thing as the tutorial!

Look at it and try to see if you now understand.


Long life to Steve!
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 5th Jan 2013 19:42
Gekko, I really appreciate your special template.

But all i want is the STANDARD AppGameKit template + gamecenter. Your template has a lot of other extra stuff
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 7th Jan 2013 02:50
Believe me, there is nothing special.
My template have the necessary, to avoid files and Mb.
Other Game Centers are enabled with the classes, mine not.
Apple changed the way to use it.

Long life to Steve!
WanteD
11
Years of Service
User Offline
Joined: 29th Dec 2012
Location:
Posted: 7th Jan 2013 09:51
Thank for your help xGEKKOx : )

Login to post a reply

Server time is: 2024-11-21 13:11:40
Your offset time is: 2024-11-21 13:11:40