Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GameCenter Plugin Live! Leaderboards and Achievements!

Discussion in 'iOS and tvOS' started by prime31, Sep 6, 2010.

  1. wesrm

    wesrm

    Joined:
    May 20, 2009
    Posts:
    193
    Excellent! Leaving out the ( ) was the problem. What a silly mistake on my part : ) Thanks so much for the support! Working perfectly now.

    I'm so glad I picked up this plugin. It's absolute gold! It's been very easy to setup Game Center for my app. This plugin is worth every penny.

    Thanks!

    Wes
     
  2. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    Glad to hear it's all working well! I look forward to seeing your final game!
     
  3. wesrm

    wesrm

    Joined:
    May 20, 2009
    Posts:
    193
    Just one more question : )

    I couldn't get this to work with JavaScript. Can you please post an example of how the GameCenterEventHandler.reportScoreFinished works in C# and perhaps I can place this into a C# Script that I can call from my JS code? I don't know anything about C#.

    Thanks again for the help!

    -Wes
     
  4. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    So, I just ran a test and it looks like (at least in Unity 3) Javascript events work the same way as C#. Here is an example:

    Code (csharp):
    1. function Start()
    2. {
    3.     GameCenterManager.reportScoreFinished += reportScoreFinished;
    4. }
    5.  
    6. function reportScoreFinished()
    7. {
    8.     Debug.Log( "it finished man" );
    9. }
    Edit: I should add to this by saying you MUST remove event handlers when your GameObject is destroyed. If you don't, you will get an exception. Removing event handlers is identical to adding them except you do '-=' instead of '+='. A good place to remove event handlers is in the OnDisable() method.
     
  5. wesrm

    wesrm

    Joined:
    May 20, 2009
    Posts:
    193
    Thanks again for the help!

    I'm getting another error in Xcode. Currently, I am trying to setup a button press to submit the score and when function reportScoreFinished() is fired, I then load the next level.

    In Xcode, I get the following error and the reportScoreFinished is never called.

    Code (csharp):
    1. SendMessage: object GameCenterManager not found!
    I'm sure I'm missing something small here. I haven't done anything with GameCenterManager. My link to GameCenter is very simple as to just save a score to a leaderboard.

    Thanks!

    Wes
     
  6. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    That's an easy one. You need to have the GameCenterManager script attached to a game object in your scene. If you don't, native code has no one on the Unity side to send messages to.
     
  7. ezone

    ezone

    Joined:
    Mar 28, 2008
    Posts:
    331
    Just playing around with the GameCenter plugin and I noticed that

    Code (csharp):
    1. GameCenterBinding.isGameCenterAvailable()
    returns TRUE on an iPhone3G running 4.1 - is that correct? I thought iPhone3G didn't make the cut for Game Center...
     
  8. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    This is a tricky one, so I'll try to explain as clearly as possible. Here's what the Getting Started with Game Center document says:

    Here is a paraphrased quote from Allan Schaffer (Apple support employee):

    The Game Center plugin does exactly what Apple recommends. Apple strongly warns against any type of specific phone model or type detection to make sure that everything works as planned now and in the future. As long as you setup your game to properly handle authentication all should be good. If you want to go against Apples recommendation and do specific device checking you could do that from Unity as well.
     
  9. ezone

    ezone

    Joined:
    Mar 28, 2008
    Posts:
    331
    Ahh, makes sense. I'll just be sure to check that the player is authenticated.

    Fantastic work on all your plugins btw, has saved me hours of work already!
     
  10. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    Glad to hear the plugins saved you some time!
     
  11. JamesMobot

    JamesMobot

    Joined:
    Jul 8, 2010
    Posts:
    170
    I just updated to the plugin
    I noticed there are four game center prefabs that were used in your sample.
    but, really all i need to be doing is adding that one script to an object (per scene)?
     
  12. wesrm

    wesrm

    Joined:
    May 20, 2009
    Posts:
    193
    Awesome! Works great : ) Thanks so much for all of the support. This plugin rocks!

    -Wes
     
  13. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @RobotsVsWizards, all you actually need is the GameCenterManager to be in the scene. You can then set it to not destroy on load so that it remains there for the duration of your game. The other prefabs are for multiplayer.
     
  14. maglat

    maglat

    Joined:
    Jul 9, 2010
    Posts:
    111
    How can we do that?
     
  15. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    In either Awake or Start just call:

    Code (csharp):
    1. DontDestroyOnLoad( this );
     
  16. JamesMobot

    JamesMobot

    Joined:
    Jul 8, 2010
    Posts:
    170
    I probably missing something ridiculously obvious to a lot of people. So my apologies. I am sort of a noob at a lot of this but I managed to get open feint up and running within my first night of attempting so i do have "some" idea what Im doing.... sort of.

    I manage to build what i need for my gameplay very slowly so my understanding of the gamecenter side of the coding is limited

    I am reading the instructions on the plugin for my leader board and it says:


    "Leaderboards
    •Posting a score to a leaderboard requires a call to the GameCenterBinding.reportScore method"

    I am basically just taking a game that i had open feint and using the same places to add the game center plugin code rather than the OpenFeint plugin code.

    I have a variable called "score" which gets my integer but, what i dont understand is the category string. I am just pasting the leader board ID from itunes connect and I get unknown identifier. I do have the gamecenter script on an object.


    Code (csharp):
    1. GameCenterBinding.reportScore( score, ID_From_ITC );
    (ID_From_ITC of course being some letters and numbers)

    What part thing am i missing?
    I was assuming that the plugin would handle the identifier issue like the open feint plugin but, I'm guessing not?

    thanks!

    ...Also my script that is calling gamecenter is .js
     
  17. langju

    langju

    Joined:
    Jul 1, 2009
    Posts:
    207
    Do you have created a leaderboard in iTunes connect and configured your category ...?
    Do you use the sandbox ? Like recommended on the prime31 doc section ?
     
  18. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @RobotsVsWizards, what langju said is very, very important. You must make sure you are using the sandbox. Check out the first paragraph in the docs.
     
  19. JamesMobot

    JamesMobot

    Joined:
    Jul 8, 2010
    Posts:
    170
    Yeah,

    I have it set up in added the authenticate which works in the sand box so I know the plugin is working.

    I am able log into my sand box account and see my game and everything I set up for it.

    However when I add that code I showed above I just get an unknown identifier error where the string should be. I am only putting the name of my itc leader board Id in the string place. That is where I am guessing I am missing something.

    I can't even build and get into the sandbox with the error so it's kind of a dead end for the moment.
     
  20. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    Have you tried the sample scene that comes with the plugin? The GameCenterGUIManager script contains a really easy way to test this:

    - first open the Game Center app and log out. Always do this before testing.
    - open your game and authenticate (touch the Authenticate button). Be 100% sure you are using a Debug build! That means choose Debug from the dropdown in Xcode!
    - grab the leaderboard data (touch the Load Leaderboard Data button)
    - post the score (touch the Post Score button)

    What this will do is grab all your leaderboards and post a random score to the first leaderboard it finds. This is a good test to make sure everything is setup properly in iTunes Connect.
     
  21. JamesMobot

    JamesMobot

    Joined:
    Jul 8, 2010
    Posts:
    170
    Yeah, all that has been working too
    In fact I built your scene before I did anything just to see I it linked to my game

    I guess implementing it into my script was just a little more involved than I thought.
    I was just thinking the plugin did enough I would only need to add that one line like the open feint plugin.

    I guess I just needed some clarification.

    My itc end works and your plugin is working so Ive got what I need I think.
    Just a matter of tinkering now.

    Thanks again for putting this together so quickly!!
     
  22. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    Once everything is confirmed to be working all you should need to do is:

    - add the GameCenterManager prefab into your scene
    - call GameCenterBinding.authenticateLocalPlayer() as soon as your game launches

    That should be all it takes. From there, you should be able to post scores or achievements with one line of code eacy.
     
  23. langju

    langju

    Joined:
    Jul 1, 2009
    Posts:
    207
    I confirm that all is ok with the plugin to post score and achievement...(at least with the sandbox).

    do you use C# ?
     
  24. JamesMobot

    JamesMobot

    Joined:
    Jul 8, 2010
    Posts:
    170
    No as stated all my scripts are all .js

    I authenticated with a button that was already in my game and it works fine in the sandbox so i'm pretty sure it works the same.


    My controller is all written in .js and all my achievements and score posts are already in there from adding open feint a while back.


    Code (csharp):
    1. function gameOver ()
    2. winGUI.gameObject.active = true;
    3. gameOversound.play();
    4. GameCenterBinding.reportScore( score, anything_I_put_here_causes_an_error );
    5.  
    the variable is already set for "score" and seems to work fine. Its just the next part that causes the unknown identifier.

    I'm pretty sure that with what is working i should be able to figure it out by pulling enough stuff from the sample code. But, from what you guys are saying my code should work.
     
  25. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    Have you tried just putting a raw string in there? Something like:

    Code (csharp):
    1. GameCenterBinding.reportScore( 125, "category_id" );
     
  26. JamesMobot

    JamesMobot

    Joined:
    Jul 8, 2010
    Posts:
    170
    Ouch...
    I really just trudge through the code by reverse engineering as much as i can get my hands on.

    I guess all i needed to see was an exact example of the one line of code.

    All I needed was the quotations....
    now
    it is working perfect.
    I was able to post a separate score to each leader board in my game (easy, normal and hard mode)
    Apparently i'm ranked number one! go figure.

    I knew it was something really obvious....

    The plugin is quite amazing!
    Thanks again!
     
  27. langju

    langju

    Joined:
    Jul 1, 2009
    Posts:
    207
    Hi prime31 :)

    Can you describe the workflow to have a multiplayer game running ?(using the GC gui)
    I mean what is the order of command to launch 1 game session between 2 players for example.
    From creating a room,
    Waiting 1 player
    A player connect
    Launching the game(scene)
    Determining who begin (player 1) (meaning retrieving players)
    Do an action ( send to peer)
    It is now player 2 to play
    Do an action
    End game


    So what is findallactivity, and how to retrieves playersid(is it int, string ?) ? I do not understand very well.

    Thanks a lot.
     
  28. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @langju, quick and easy steps to connect 2 - 4 devices using the demo scene:

    - touch 'Authenticate' first (always! nothing in Game Center works until you are authenticated)
    - touch 'Show Matchmaker' to bring up the Apple GUI
    - touch 'Play Now' in the matchmaker gui

    Thanks all there is to it. The status on the bottom of the screen will show when you connect to a peer. You can then just press 'Send To All' to send a message to all the peers. It will appear on their screen below the status.

    Find all activity is only for programmatically connected games with no GUI. It will show the number of users currently waiting to play. Player ID's are a string. Player ID's will be sent to you via callbacks from native. If you watch the debug log in Xcode while playing with the demo scene you will see all the messages being sent back and forth along with player's connecting/disconnecting.

    Be sure also to check out the docs. This is now a multiplayer section in there with a lot of good information.
     
  29. langju

    langju

    Joined:
    Jul 1, 2009
    Posts:
    207
    Again

    Thanks for all !
    You can now go to sleep :)
    :D
     
  30. ezone

    ezone

    Joined:
    Mar 28, 2008
    Posts:
    331
    Is there an exposed method to get the authenticated playerID? Something like:

    Code (csharp):
    1. GameCenterBinding.playerId()
     
  31. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @ezone, currently you can only grab the player alias. Apple had some strict guidelines for playerId storage so I left it out. Do you have a use case where it would be useful?
     
  32. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    This and your other plugins are very nice additions!

    One thing stands out as desirable... prefer all your plugins followed standard Unity capitalization syntax;

    - methods are UpperCase()
    - member variables are .lowerCase.
    etc.

    Please fix this inconsistency sooner, rather than later, because right now accessing your code is the only exception in our projects; this inconsistency won't magically go away over time, only dig deeper.

    Great work just the same!
     
  33. ezone

    ezone

    Joined:
    Mar 28, 2008
    Posts:
    331
    We're planning to use the game center playerId as a key to store items that players have unlocked on a remote database - then if a game center player logs in using a different device they can have their game state/items pulled in from the server. We could probably use the player alias, but players can change it so playerId would be better.
     
  34. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @ezone, as long as you are careful to check it could work. From Apple's documentation:

    Code (csharp):
    1. Important: Do not make assumptions about the contents of the player identifier string. Its format and length are subject to change.
    I will add a playerId method into the plugin and release an update tonight as long as the limitation are known.
     
  35. ezone

    ezone

    Joined:
    Mar 28, 2008
    Posts:
    331
    Thanks! (you da man)

    BTW I hope you are earning enough thru the plugins to keep this phenomenal customer support going.
     
  36. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @ezone, update is live. You can use your original download link to grab it.

    I once worked for a CEO whose biggest concern was having the best customer support around in order to differentiate from the pack and provide more human service. I totally agree with the philosophy and strive to do the same.
     
  37. ezone

    ezone

    Joined:
    Mar 28, 2008
    Posts:
    331
    Awesome! Thanks! (can't believe you updated the online docs already too!)
     
  38. impla007

    impla007

    Joined:
    Jan 30, 2010
    Posts:
    57
    Hi Prime,

    I want to post to leaderboard from JavaScript. Before posting I need to initialize leaderbords array BUT , I didn't find the way to convert this to JS.



    Code (csharp):
    1. private List<GameCenterLeaderboard> leaderboards;
    2.    
    3.     void Start()
    4.     {
    5.         // use anonymous deleagates for this simple example for gathering data from GameCenter
    6.         GameCenterManager.categoriesLoaded += delegate( List<GameCenterLeaderboard> leaderboards )
    7.         {
    8.             this.leaderboards = leaderboards;
    9.         };
     
  39. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    Hi Prime31,

    Nice work! A few quick questions - what does your GK plugin do if no internet connection is available? Does it attempt to store and resubmit scores and achievements later using the NSCoding protocol as mentioned in the docs? I'm not sure whether that's just an Apple recommendation, or if they require it for GC apps. That's one of the more hairy parts of the docs (in addition to delegate handling on the main thread only) and examples for GameKit that have me strongly considering using your plugin.

    Thanks,

    -Gordon
     
  40. bumba

    bumba

    Joined:
    Oct 10, 2008
    Posts:
    358
    how hard it is to set up a multiplayer game (turn based with physics). What do i have to make in my unity code to get it working. Or is it automated via GameCenter? Maybe you can make a small example Project, something like tic tac toe. That would be awesome
     
  41. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @impla007, it is possible the generic list could be synonymous with an ArrayList. I avoid Javascript like the plague to avoid inconsistencies and missing features such as these but I'll do some testing when I am back near a computer this evening and see if I can figure it out.

    @getluky, I did not add auto resubmitting of scores and achievements in order to avoid issues with the auto send overwriting other possible successful sends. It seemed like handling and managing resends was best left to the specific game. You will get error messages when things go south and you can save out the information for a retry Unity side pretty easily.

    The plugin is pretty much a no brainer for the price. It basically costs less than an hour of any game developers wages. If you can code Game Center integration and make it cost less in man hours than the price of the plugin than you are some kind of god or your game development wages are less than minimum wage :D

    @bumba, physics based multiplayer is a very advanced topic and the GameKit plugin is probably not the best way to do it. You really need a dedicated server to run the physics simulation in order to keep things in sync. It is not at all trivial to implement and is possibly one of the most difficult multiplayer game types to create.
     
  42. bumba

    bumba

    Joined:
    Oct 10, 2008
    Posts:
    358
    also turn based? so that you only could see the other attempt like in worms?
     
  43. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @bumba, turn based gets easier but it is still most certainly not the easiest multiplayer path to go down. You could run the simulation on one device and then send updates to the other to be sure they are in sync.
     
  44. langju

    langju

    Joined:
    Jul 1, 2009
    Posts:
    207
    What could be done is to send players input at interval of x ms and use interpolation or extrapolation to calculates estimated movement each in an larger interval you send the true position to correct what was estimated .

    I don't know if it is clear sorry for my English.
     
  45. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    No reply? :(

     
  46. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @Joe @ ByDesign Games, all the plugins use the code guidelines and naming conventions from Cocoa. I am not a big fan of Microsoft's code guidelines for .NET (which is what Unity uses) and much prefer the more explicit, human and readable Cocoa guidelines. If it is really that big a problem to use a lowercase method name feel free to make simple wrappers to the *Binding classes with uppercase method names.
     
  47. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    @Prime31, thanks for answering my questions. I agree that it'll save me time regardless, so I just bought it!
     
  48. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    @getluky, congrats and thanks! you just saved yourself a lot of time but you do miss out on the fun of using Blocks! Man I love Blocks. I look forward to doing more coding with them.
     
  49. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    @Prime31 I'm gonna play around with it right now, and I do plan on doing more work directly in XCode in the near future. I hope you don't mind if I use your code as a reference for how to do things! :)
     
  50. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    Weird, I'm getting the following error when attempting to authenticate from the device on a debug build:

    "The requested operation could not be completed because this application is not recognized by Game Center"

    Any ideas?